Skip to content

Commit 81b8e3e

Browse files
philnashdkundel
authored andcommitted
Merge branch 'more-refactor'
1 parent 8649d2b commit 81b8e3e

File tree

6 files changed

+141
-107
lines changed

6 files changed

+141
-107
lines changed

packages/create-twilio-function/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"license": "MIT",
2525
"devDependencies": {
2626
"jest": "^24.5.0",
27-
"nock": "^10.0.6"
27+
"nock": "^11.3.4"
2828
},
2929
"dependencies": {
3030
"boxen": "^3.0.0",

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,12 @@ async function createTwilioFunction(config) {
4848
switch (e.code) {
4949
case 'EEXIST':
5050
spinner.fail(
51-
`A directory called '${
52-
config.name
53-
}' already exists. Please create your function in a new directory.`
51+
`A directory called '${config.name}' already exists. Please create your function in a new directory.`
5452
);
5553
break;
5654
case 'EACCES':
5755
spinner.fail(
58-
`You do not have permission to create files or directories in the path '${
59-
config.path
60-
}'.`
56+
`You do not have permission to create files or directories in the path '${config.path}'.`
6157
);
6258
break;
6359
default:
@@ -122,9 +118,7 @@ async function createTwilioFunction(config) {
122118
} catch (err) {
123119
spinner.fail();
124120
console.log(
125-
`There was an error installing the dependencies, but your project is otherwise complete in ./${
126-
config.name
127-
}`
121+
`There was an error installing the dependencies, but your project is otherwise complete in ./${config.name}`
128122
);
129123
}
130124

packages/create-twilio-function/tests/create-files.test.js

+43-31
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,34 @@ const mkdir = promisify(fs.mkdir);
1313
const readFile = promisify(fs.readFile);
1414
const stat = promisify(fs.stat);
1515
const readdir = promisify(fs.readdir);
16+
const path = require('path');
17+
18+
const scratchDir = path.join(process.cwd(), 'scratch');
1619

1720
beforeAll(async () => {
18-
await rimraf('./scratch');
21+
await rimraf(scratchDir);
1922
});
2023

2124
beforeEach(async () => {
22-
await mkdir('./scratch');
25+
await mkdir(scratchDir);
2326
});
2427

2528
afterEach(async () => {
26-
await rimraf('./scratch');
29+
await rimraf(scratchDir);
2730
});
2831

2932
describe('createDirectory', () => {
3033
test('it creates a new directory with the project name', async () => {
31-
await createDirectory('./scratch', 'test-project');
32-
const dir = await stat('./scratch/test-project');
34+
await createDirectory(scratchDir, 'test-project');
35+
const dir = await stat(path.join(scratchDir, 'test-project'));
3336
expect(dir.isDirectory());
3437
});
3538

3639
test('it throws an error if the directory exists', async () => {
37-
await mkdir('./scratch/test-project');
40+
await mkdir(path.join(scratchDir, 'test-project'));
3841
expect.assertions(1);
3942
try {
40-
await createDirectory('./scratch', 'test-project');
43+
await createDirectory(scratchDir, 'test-project');
4144
} catch (e) {
4245
expect(e.toString()).toMatch('EEXIST');
4346
}
@@ -46,10 +49,12 @@ describe('createDirectory', () => {
4649

4750
describe('createPackageJSON', () => {
4851
test('it creates a new package.json file with the name of the project', async () => {
49-
await createPackageJSON('./scratch', 'project-name');
50-
const file = await stat('./scratch/package.json');
52+
await createPackageJSON(scratchDir, 'project-name');
53+
const file = await stat(path.join(scratchDir, 'package.json'));
5154
expect(file.isFile());
52-
const packageJSON = JSON.parse(await readFile('./scratch/package.json'));
55+
const packageJSON = JSON.parse(
56+
await readFile(path.join(scratchDir, 'package.json'))
57+
);
5358
expect(packageJSON.name).toEqual('project-name');
5459
expect(packageJSON.engines.node).toEqual(versions.node);
5560
expect(packageJSON.devDependencies['twilio-run']).toEqual(
@@ -58,38 +63,41 @@ describe('createPackageJSON', () => {
5863
});
5964

6065
test('it rejects if there is already a package.json', async () => {
61-
fs.closeSync(fs.openSync('./scratch/package.json', 'w'));
66+
fs.closeSync(fs.openSync(path.join(scratchDir, 'package.json'), 'w'));
6267
expect.assertions(1);
6368
try {
64-
await createPackageJSON('./scratch', 'project-name');
69+
await createPackageJSON(scratchDir, 'project-name');
6570
} catch (e) {
6671
expect(e.toString()).toMatch('file already exists');
6772
}
6873
});
6974
});
7075

7176
describe('createExampleFromTemplates', () => {
77+
const templatesDir = path.join(process.cwd(), 'templates');
7278
test('it creates functions and assets directories', async () => {
73-
await createExampleFromTemplates('./scratch');
79+
await createExampleFromTemplates(scratchDir);
7480

75-
const dirs = await readdir('./scratch');
76-
const templateDirs = await readdir('./templates');
77-
expect(dirs).toEqual(templateDirs);
81+
const dirs = await readdir(scratchDir);
82+
const templateDirContents = await readdir(templatesDir);
83+
expect(dirs).toEqual(templateDirContents);
7884
});
7985

8086
test('it copies the functions from the templates/functions directory', async () => {
81-
await createExampleFromTemplates('./scratch');
87+
await createExampleFromTemplates(scratchDir);
8288

83-
const functions = await readdir('./scratch/functions');
84-
const templateFunctions = await readdir('./templates/functions');
89+
const functions = await readdir(path.join(scratchDir, 'functions'));
90+
const templateFunctions = await readdir(
91+
path.join(templatesDir, 'functions')
92+
);
8593
expect(functions).toEqual(templateFunctions);
8694
});
8795

8896
test('it rejects if there is already a functions directory', async () => {
89-
await mkdir('./scratch/functions');
97+
await mkdir(path.join(scratchDir, 'functions'));
9098
expect.assertions(1);
9199
try {
92-
await createExampleFromTemplates('./scratch');
100+
await createExampleFromTemplates(scratchDir);
93101
} catch (e) {
94102
expect(e.toString()).toMatch('file already exists');
95103
}
@@ -98,22 +106,24 @@ describe('createExampleFromTemplates', () => {
98106

99107
describe('createEnvFile', () => {
100108
test('it creates a new .env file', async () => {
101-
await createEnvFile('./scratch', {
109+
await createEnvFile(scratchDir, {
102110
accountSid: 'AC123',
103111
authToken: 'qwerty123456'
104112
});
105-
const file = await stat('./scratch/.env');
113+
const file = await stat(path.join(scratchDir, '.env'));
106114
expect(file.isFile());
107-
const contents = await readFile('./scratch/.env', { encoding: 'utf-8' });
115+
const contents = await readFile(path.join(scratchDir, '.env'), {
116+
encoding: 'utf-8'
117+
});
108118
expect(contents).toMatch('ACCOUNT_SID=AC123');
109119
expect(contents).toMatch('AUTH_TOKEN=qwerty123456');
110120
});
111121

112122
test('it rejects if there is already an .env file', async () => {
113-
fs.closeSync(fs.openSync('./scratch/.env', 'w'));
123+
fs.closeSync(fs.openSync(path.join(scratchDir, '.env'), 'w'));
114124
expect.assertions(1);
115125
try {
116-
await createEnvFile('./scratch', {
126+
await createEnvFile(scratchDir, {
117127
accountSid: 'AC123',
118128
authToken: 'qwerty123456'
119129
});
@@ -125,18 +135,20 @@ describe('createEnvFile', () => {
125135

126136
describe('createNvmrcFile', () => {
127137
test('it creates a new .nvmrc file', async () => {
128-
await createNvmrcFile('./scratch');
129-
const file = await stat('./scratch/.nvmrc');
138+
await createNvmrcFile(scratchDir);
139+
const file = await stat(path.join(scratchDir, '.nvmrc'));
130140
expect(file.isFile());
131-
const contents = await readFile('./scratch/.nvmrc', { encoding: 'utf-8' });
141+
const contents = await readFile(path.join(scratchDir, '.nvmrc'), {
142+
encoding: 'utf-8'
143+
});
132144
expect(contents).toMatch(versions.node);
133145
});
134146

135147
test('it rejects if there is already an .nvmrc file', async () => {
136-
fs.closeSync(fs.openSync('./scratch/.nvmrc', 'w'));
148+
fs.closeSync(fs.openSync(path.join(scratchDir, '.nvmrc'), 'w'));
137149
expect.assertions(1);
138150
try {
139-
await createNvmrcFile('./scratch');
151+
await createNvmrcFile(scratchDir);
140152
} catch (e) {
141153
expect(e.toString()).toMatch('file already exists');
142154
}

packages/create-twilio-function/tests/create-gitignore.test.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ const fs = require('fs');
66
const mkdir = promisify(fs.mkdir);
77
const readFile = promisify(fs.readFile);
88
const stat = promisify(fs.stat);
9+
const path = require('path');
10+
11+
const scratchDir = path.join(process.cwd(), 'scratch');
912

1013
beforeAll(async () => {
11-
await rimraf('./scratch');
14+
await rimraf(scratchDir);
1215
nock.disableNetConnect();
1316
});
1417

@@ -17,34 +20,34 @@ afterAll(() => {
1720
});
1821

1922
beforeEach(async () => {
20-
await mkdir('./scratch');
23+
await mkdir(scratchDir);
2124
nock('https://raw.githubusercontent.com')
2225
.get('/github/gitignore/master/Node.gitignore')
2326
.reply(200, '*.log\n.env');
2427
});
2528

2629
afterEach(async () => {
27-
await rimraf('./scratch');
30+
await rimraf(scratchDir);
2831
nock.cleanAll();
2932
});
3033

3134
describe('createGitignore', () => {
3235
test('it creates a new .gitignore file', async () => {
33-
await createGitignore('./scratch');
34-
const file = await stat('./scratch/.gitignore');
36+
await createGitignore(scratchDir);
37+
const file = await stat(path.join(scratchDir, '.gitignore'));
3538
expect(file.isFile());
36-
const contents = await readFile('./scratch/.gitignore', {
39+
const contents = await readFile(path.join(scratchDir, '.gitignore'), {
3740
encoding: 'utf-8'
3841
});
3942
expect(contents).toMatch('*.log');
4043
expect(contents).toMatch('.env');
4144
});
4245

4346
test('it rejects if there is already a .gitignore file', async () => {
44-
fs.closeSync(fs.openSync('./scratch/.gitignore', 'w'));
47+
fs.closeSync(fs.openSync(path.join(scratchDir, '.gitignore'), 'w'));
4548
expect.assertions(1);
4649
try {
47-
await createGitignore('./scratch');
50+
await createGitignore(scratchDir);
4851
} catch (e) {
4952
expect(e.toString()).toMatch('file already exists');
5053
}

0 commit comments

Comments
 (0)