Skip to content

Commit b75133d

Browse files
philnashdkundel
authored andcommitted
Better error messages if the cli fails to create a directory. Fixes #14
1 parent c4f69ef commit b75133d

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

packages/create-twilio-function/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## Ongoing [](https://github.com/twilio-labs/create-twilio-function/compare/v1.0.1...master)
44

5-
...
5+
- Minor updates
6+
- Better error messages if the cli fails to create a directory. Fixes #14
67

78
## 1.0.1 (May 4, 2018) [](https://github.com/twilio-labs/create-twilio-function/compare/v1.0.0...v1.0.1)
89

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,24 @@ async function createTwilioFunction(config) {
2121
try {
2222
await createDirectory(config.path, config.name);
2323
} catch (e) {
24-
console.error(
25-
`A directory called '${
26-
config.name
27-
}' already exists. Please create your function in a new directory.`
28-
);
24+
switch (e.code) {
25+
case 'EEXIST':
26+
console.error(
27+
`A directory called '${
28+
config.name
29+
}' already exists. Please create your function in a new directory.`
30+
);
31+
break;
32+
case 'EACCES':
33+
console.error(
34+
`You do not have permission to create files or directories in the path '${
35+
config.path
36+
}'.`
37+
);
38+
break;
39+
default:
40+
console.error(e.message);
41+
}
2942
return;
3043
}
3144

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,35 @@ describe('createTwilioFunction', () => {
8888

8989
await createTwilioFunction({ name, path: './scratch' });
9090

91-
expect.assertions(3);
91+
expect.assertions(4);
9292

9393
expect(console.error).toHaveBeenCalledTimes(1);
94+
expect(console.error).toHaveBeenCalledWith(
95+
`A directory called '${name}' already exists. Please create your function in a new directory.`
96+
);
97+
expect(console.log).not.toHaveBeenCalled();
98+
99+
try {
100+
await stat(`./scratch/${name}/package.json`);
101+
} catch (e) {
102+
expect(e.toString()).toMatch('no such file or directory');
103+
}
104+
});
105+
106+
it("fails gracefully if it doesn't have permission to create directories", async () => {
107+
const name = 'test-function';
108+
const chmod = promisify(fs.chmod);
109+
await chmod('./scratch', 0o555);
110+
console.error = jest.fn();
111+
112+
await createTwilioFunction({ name, path: './scratch' });
113+
114+
expect.assertions(4);
115+
116+
expect(console.error).toHaveBeenCalledTimes(1);
117+
expect(console.error).toHaveBeenCalledWith(
118+
`You do not have permission to create files or directories in the path './scratch'.`
119+
);
94120
expect(console.log).not.toHaveBeenCalled();
95121

96122
try {

0 commit comments

Comments
 (0)