Skip to content

Commit a77d23d

Browse files
philnashdkundel
authored andcommitted
Merge branch 'dlemstra-add-eslint'
1 parent 77d6869 commit a77d23d

24 files changed

+333
-345
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
'extends': 'twilio',
3+
'plugins': ['jest'],
4+
'env': {
5+
'jest/globals': true
6+
},
7+
'parserOptions': {
8+
'ecmaVersion': 9,
9+
'sourceType': 'module',
10+
},
11+
'rules': {
12+
'no-console': 'off'
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto eol=lf
2+
*.js text

packages/create-twilio-function/.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ node_js:
55
os:
66
- linux
77
- windows
8+
script:
9+
- npm test
10+
- npm run lint

packages/create-twilio-function/package.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"bin": "./bin/create-twilio-function",
66
"main": "./src/create-twilio-function.js",
77
"scripts": {
8-
"test": "jest"
8+
"test": "jest",
9+
"lint": "eslint src tests",
10+
"lint:fix": "npm run lint -- --fix"
911
},
1012
"keywords": [
1113
"twilio",
@@ -23,6 +25,13 @@
2325
},
2426
"license": "MIT",
2527
"devDependencies": {
28+
"eslint": "^6.5.1",
29+
"eslint-config-twilio": "^1.13.2",
30+
"eslint-plugin-import": "^2.18.2",
31+
"eslint-plugin-jest": "^22.17.0",
32+
"eslint-plugin-node": "^10.0.0",
33+
"eslint-plugin-promise": "^4.2.1",
34+
"eslint-plugin-standard": "^4.0.1",
2635
"jest": "^24.5.0",
2736
"nock": "^11.3.4"
2837
},

packages/create-twilio-function/src/cli.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const yargs = require('yargs');
2-
const DefaultCommand = require('./command');
32
const ListTemplateCommand = require('twilio-run/dist/commands/list-templates');
43

4+
const DefaultCommand = require('./command');
5+
56
function cli(cwd) {
67
yargs.help();
78
yargs.alias('h', 'help');

packages/create-twilio-function/src/command.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,42 @@ const cliInfo = {
88
'account-sid': {
99
alias: 'a',
1010
describe: 'The Account SID for your Twilio account',
11-
type: 'string'
11+
type: 'string',
1212
},
1313
'auth-token': {
1414
alias: 't',
1515
describe: 'Your Twilio account Auth Token',
16-
type: 'string'
16+
type: 'string',
1717
},
1818
'skip-credentials': {
19-
describe:
20-
"Don't ask for Twilio account credentials or import them from the environment",
19+
describe: "Don't ask for Twilio account credentials or import them from the environment",
2120
type: 'boolean',
22-
default: false
21+
default: false,
2322
},
2423
'import-credentials': {
25-
describe:
26-
'Import credentials from the environment variables TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN',
24+
describe: 'Import credentials from the environment variables TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN',
2725
type: 'boolean',
28-
default: false
26+
default: false,
2927
},
3028
template: {
31-
describe:
32-
'Initialize your new project with a template from github.com/twilio-labs/function-templates',
33-
type: 'string'
34-
}
35-
}
29+
describe: 'Initialize your new project with a template from github.com/twilio-labs/function-templates',
30+
type: 'string',
31+
},
32+
},
3633
};
3734

38-
const builder = command => {
39-
command.positional('name', {
35+
function builder(cmd) {
36+
cmd.positional('name', {
4037
describe: 'Name of your project.',
41-
type: 'string'
38+
type: 'string',
4239
});
43-
command.options(cliInfo.options);
44-
};
40+
cmd.options(cliInfo.options);
41+
}
4542

4643
module.exports = {
4744
command,
4845
describe,
4946
handler,
5047
cliInfo,
51-
builder
48+
builder,
5249
};

packages/create-twilio-function/src/create-twilio-function.js

+24-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
const {
2-
promptForAccountDetails,
3-
promptForProjectName
4-
} = require('./create-twilio-function/prompt');
1+
const { promisify } = require('util');
2+
const path = require('path');
3+
4+
const ora = require('ora');
5+
const boxen = require('boxen');
6+
const rimraf = promisify(require('rimraf'));
7+
const { downloadTemplate } = require('twilio-run/dist/templating/actions');
8+
9+
const { promptForAccountDetails, promptForProjectName } = require('./create-twilio-function/prompt');
510
const validateProjectName = require('./create-twilio-function/validate-project-name');
611
const {
712
createDirectory,
813
createEnvFile,
914
createExampleFromTemplates,
1015
createPackageJSON,
11-
createNvmrcFile
16+
createNvmrcFile,
1217
} = require('./create-twilio-function/create-files');
1318
const createGitignore = require('./create-twilio-function/create-gitignore');
1419
const importCredentials = require('./create-twilio-function/import-credentials');
15-
const {
16-
installDependencies
17-
} = require('./create-twilio-function/install-dependencies');
20+
const { installDependencies } = require('./create-twilio-function/install-dependencies');
1821
const successMessage = require('./create-twilio-function/success-message');
19-
const ora = require('ora');
20-
const boxen = require('boxen');
21-
const { downloadTemplate } = require('twilio-run/dist/templating/actions');
22-
const { promisify } = require('util');
23-
const rimraf = promisify(require('rimraf'));
24-
const path = require('path');
2522

2623
async function cleanUpAndExit(projectDir, spinner, errorMessage) {
2724
spinner.fail(errorMessage);
@@ -48,13 +45,11 @@ async function createTwilioFunction(config) {
4845
switch (e.code) {
4946
case 'EEXIST':
5047
spinner.fail(
51-
`A directory called '${config.name}' already exists. Please create your function in a new directory.`
48+
`A directory called '${config.name}' already exists. Please create your function in a new directory.`,
5249
);
5350
break;
5451
case 'EACCES':
55-
spinner.fail(
56-
`You do not have permission to create files or directories in the path '${config.path}'.`
57-
);
52+
spinner.fail(`You do not have permission to create files or directories in the path '${config.path}'.`);
5853
break;
5954
default:
6055
spinner.fail(e.message);
@@ -68,14 +63,17 @@ async function createTwilioFunction(config) {
6863
if (Object.keys(accountDetails).length === 0) {
6964
accountDetails = await promptForAccountDetails(config);
7065
}
71-
config = { ...accountDetails, ...config };
66+
config = {
67+
...accountDetails,
68+
...config,
69+
};
7270

7371
// Scaffold project
7472
spinner.start('Creating project directories and files');
7573

7674
await createEnvFile(projectDir, {
7775
accountSid: config.accountSid,
78-
authToken: config.authToken
76+
authToken: config.authToken,
7977
});
8078
await createNvmrcFile(projectDir);
8179
await createPackageJSON(projectDir, config.name);
@@ -88,11 +86,7 @@ async function createTwilioFunction(config) {
8886
await downloadTemplate(config.template, '', projectDir);
8987
spinner.succeed();
9088
} catch (err) {
91-
await cleanUpAndExit(
92-
projectDir,
93-
spinner,
94-
`The template "${config.template}" doesn't exist`
95-
);
89+
await cleanUpAndExit(projectDir, spinner, `The template "${config.template}" doesn't exist`);
9690
return;
9791
}
9892
} else {
@@ -118,14 +112,17 @@ async function createTwilioFunction(config) {
118112
} catch (err) {
119113
spinner.fail();
120114
console.log(
121-
`There was an error installing the dependencies, but your project is otherwise complete in ./${config.name}`
115+
`There was an error installing the dependencies, but your project is otherwise complete in ./${config.name}`,
122116
);
123117
}
124118

125119
// Success message
126120

127121
console.log(
128-
boxen(await successMessage(config), { padding: 1, borderStyle: 'round' })
122+
boxen(await successMessage(config), {
123+
padding: 1,
124+
borderStyle: 'round',
125+
}),
129126
);
130127
}
131128

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const versions = require('./versions');
21
const fs = require('fs');
2+
const path = require('path');
33
const { promisify } = require('util');
4+
5+
const versions = require('./versions');
6+
47
const mkdir = promisify(fs.mkdir);
58
const writeFile = promisify(fs.writeFile);
69
const readdir = promisify(fs.readdir);
710
const copyFile = promisify(fs.copyFile);
811
const { COPYFILE_EXCL } = fs.constants;
912
const stat = promisify(fs.stat);
10-
const path = require('path');
1113

1214
function createDirectory(pathName, dirName) {
1315
return mkdir(path.join(pathName, dirName));
@@ -21,21 +23,19 @@ function createPackageJSON(pathName, name) {
2123
const fullPath = path.join(pathName, 'package.json');
2224
const packageJSON = JSON.stringify(
2325
{
24-
name: name,
26+
name,
2527
version: '0.0.0',
2628
private: true,
2729
scripts: {
2830
test: 'echo "Error: no test specified" && exit 1',
2931
start: 'twilio-run',
30-
deploy: 'twilio-run deploy'
32+
deploy: 'twilio-run deploy',
3133
},
32-
devDependencies: {
33-
'twilio-run': versions.twilioRun
34-
},
35-
engines: { node: versions.node }
34+
devDependencies: { 'twilio-run': versions.twilioRun },
35+
engines: { node: versions.node },
3636
},
3737
null,
38-
2
38+
2,
3939
);
4040
return createFile(fullPath, packageJSON);
4141
}
@@ -44,28 +44,21 @@ function copyRecursively(src, dest) {
4444
return readdir(src).then(children => {
4545
return Promise.all(
4646
children.map(child =>
47-
stat(path.join(src, child)).then(stat => {
48-
if (stat.isDirectory()) {
47+
stat(path.join(src, child)).then(stats => {
48+
if (stats.isDirectory()) {
4949
return mkdir(path.join(dest, child)).then(() =>
50-
copyRecursively(path.join(src, child), path.join(dest, child))
50+
copyRecursively(path.join(src, child), path.join(dest, child)),
5151
);
5252
}
53-
return copyFile(
54-
path.join(src, child),
55-
path.join(dest, child),
56-
COPYFILE_EXCL
57-
);
58-
})
59-
)
53+
return copyFile(path.join(src, child), path.join(dest, child), COPYFILE_EXCL);
54+
}),
55+
),
6056
);
6157
});
6258
}
6359

6460
function createExampleFromTemplates(pathName) {
65-
return copyRecursively(
66-
path.join(__dirname, '..', '..', 'templates'),
67-
pathName
68-
);
61+
return copyRecursively(path.join(__dirname, '..', '..', 'templates'), pathName);
6962
}
7063

7164
function createEnvFile(pathName, { accountSid, authToken }) {
@@ -86,5 +79,5 @@ module.exports = {
8679
createPackageJSON,
8780
createExampleFromTemplates,
8881
createEnvFile,
89-
createNvmrcFile
82+
createNvmrcFile,
9083
};

packages/create-twilio-function/src/create-twilio-function/create-gitignore.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
const fs = require('fs');
2+
const path = require('path');
23
const { promisify } = require('util');
4+
35
const writeGitignore = promisify(require('gitignore').writeFile);
6+
47
const open = promisify(fs.open);
5-
const path = require('path');
68

79
function createGitignore(dirPath) {
810
const fullPath = path.join(dirPath, '.gitignore');
911
return open(fullPath, 'wx').then(fd => {
10-
const stream = fs.createWriteStream(null, { fd: fd });
11-
return writeGitignore({ type: 'Node', file: stream });
12+
const stream = fs.createWriteStream(null, { fd });
13+
return writeGitignore({
14+
type: 'Node',
15+
file: stream,
16+
});
1217
});
1318
}
1419

packages/create-twilio-function/src/create-twilio-function/import-credentials.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,31 @@ const inquirer = require('inquirer');
33
const questions = [
44
{
55
type: 'confirm',
6-
name: 'importCredentials',
7-
message:
8-
'Your account credentials have been found in your environment variables. Import them?',
9-
default: true
10-
}
6+
name: 'importedCredentials',
7+
message: 'Your account credentials have been found in your environment variables. Import them?',
8+
default: true,
9+
},
1110
];
1211

1312
async function importCredentials(config) {
14-
if (config.skipCredentials) return {};
13+
if (config.skipCredentials) {
14+
return {};
15+
}
1516

1617
const credentials = {
1718
accountSid: process.env.TWILIO_ACCOUNT_SID,
18-
authToken: process.env.TWILIO_AUTH_TOKEN
19+
authToken: process.env.TWILIO_AUTH_TOKEN,
1920
};
20-
if (
21-
typeof credentials.accountSid === 'undefined' &&
22-
typeof credentials.authToken === 'undefined'
23-
) {
21+
if (typeof credentials.accountSid === 'undefined' && typeof credentials.authToken === 'undefined') {
2422
return {};
2523
}
2624

27-
if (config.importCredentials) {
25+
if (config.importedCredentials) {
2826
return credentials;
2927
}
3028

31-
const { importCredentials } = await inquirer.prompt(questions);
32-
return importCredentials ? credentials : {};
29+
const { importedCredentials } = await inquirer.prompt(questions);
30+
return importedCredentials ? credentials : {};
3331
}
3432

3533
module.exports = importCredentials;

0 commit comments

Comments
 (0)