Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

Commit d9be9b3

Browse files
committed
Extract createGitignore from create-files
1 parent a6a7499 commit d9be9b3

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

src/create-twilio-function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const {
33
createDirectory,
44
createEnvFile,
55
createExampleFunction,
6-
createGitignore,
76
createPackageJSON
87
} = require('./create-twilio-function/create-files');
8+
const createGitignore = require('./create-twilio-function/create-gitignore');
99
const {
1010
installDependencies
1111
} = require('./create-twilio-function/install-dependencies');

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
const fs = require('fs');
2-
const path = require('path');
32
const { promisify } = require('util');
43
const mkdir = promisify(fs.mkdir);
54
const open = promisify(fs.open);
65
const write = promisify(fs.write);
7-
const readFile = promisify(fs.readFile);
86

97
function createDirectory(path, dirName) {
108
return mkdir(path + '/' + dirName);
@@ -50,16 +48,9 @@ AUTH_TOKEN=${authToken}`;
5048
return createFile(fullPath, content);
5149
}
5250

53-
async function createGitignore(dirPath) {
54-
const fullPath = `${dirPath}/.gitignore`;
55-
const content = await readFile(`${__dirname}/../../templates/.gitignore`);
56-
return createFile(fullPath, content);
57-
}
58-
5951
module.exports = {
6052
createDirectory,
6153
createPackageJSON,
6254
createExampleFunction,
63-
createEnvFile,
64-
createGitignore
55+
createEnvFile
6556
};

src/create-twilio-function/create-files.test.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const {
22
createPackageJSON,
33
createDirectory,
44
createExampleFunction,
5-
createEnvFile,
6-
createGitignore
5+
createEnvFile
76
} = require('./create-files');
87
const fs = require('fs');
98
const { promisify } = require('util');
@@ -110,25 +109,3 @@ describe('createEnvFile', () => {
110109
}
111110
});
112111
});
113-
114-
describe('createGitignore', () => {
115-
test('it creates a new .gitignore file', async () => {
116-
await createGitignore('./scratch');
117-
const file = await stat('./scratch/.gitignore');
118-
expect(file.isFile());
119-
const contents = await readFile('./scratch/.gitignore', {
120-
encoding: 'utf-8'
121-
});
122-
expect(contents).toMatch('*.log');
123-
});
124-
125-
test('it rejects if there is already a .gitignore file', async () => {
126-
fs.closeSync(fs.openSync('./scratch/.gitignore', 'w'));
127-
expect.assertions(1);
128-
try {
129-
await createGitignore('./scratch');
130-
} catch (e) {
131-
expect(e.toString()).toMatch('file already exists');
132-
}
133-
});
134-
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require('fs');
2+
const { promisify } = require('util');
3+
const readFile = promisify(fs.readFile);
4+
const open = promisify(fs.open);
5+
const write = promisify(fs.write);
6+
7+
async function createGitignore(dirPath) {
8+
const fullPath = `${dirPath}/.gitignore`;
9+
const content = await readFile(`${__dirname}/../../templates/.gitignore`);
10+
return open(fullPath, 'wx').then(fd => {
11+
return write(fd, content);
12+
});
13+
}
14+
15+
module.exports = createGitignore;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// const nock = require('nock');
2+
const createGitignore = require('./create-gitignore');
3+
const { promisify } = require('util');
4+
const rimraf = promisify(require('rimraf'));
5+
const fs = require('fs');
6+
const mkdir = promisify(fs.mkdir);
7+
const readFile = promisify(fs.readFile);
8+
const stat = promisify(fs.stat);
9+
10+
beforeAll(async () => {
11+
await rimraf('./scratch');
12+
});
13+
14+
beforeEach(async () => {
15+
await mkdir('./scratch');
16+
});
17+
18+
afterEach(async () => {
19+
await rimraf('./scratch');
20+
});
21+
22+
describe('createGitignore', () => {
23+
test('it creates a new .gitignore file', async () => {
24+
await createGitignore('./scratch');
25+
const file = await stat('./scratch/.gitignore');
26+
expect(file.isFile());
27+
const contents = await readFile('./scratch/.gitignore', {
28+
encoding: 'utf-8'
29+
});
30+
expect(contents).toMatch('*.log');
31+
});
32+
33+
test('it rejects if there is already a .gitignore file', async () => {
34+
fs.closeSync(fs.openSync('./scratch/.gitignore', 'w'));
35+
expect.assertions(1);
36+
try {
37+
await createGitignore('./scratch');
38+
} catch (e) {
39+
expect(e.toString()).toMatch('file already exists');
40+
}
41+
});
42+
});

0 commit comments

Comments
 (0)