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

Commit d3eba1b

Browse files
committed
Better error messages if the cli fails to create a directory. Fixes #14
1 parent 931f6af commit d3eba1b

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
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

src/create-twilio-function.js

Lines changed: 18 additions & 5 deletions
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

tests/create-twilio-function.test.js

Lines changed: 27 additions & 1 deletion
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)