-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathask.js
More file actions
53 lines (51 loc) · 1.17 KB
/
ask.js
File metadata and controls
53 lines (51 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const os = require('os');
const fs = require('fs');
const path = require('path');
const { Input } = require('enquirer');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const shouldCancel = require('cli-should-cancel');
const { Store } = require('data-store');
module.exports = async ({ name, message, hint, initial }) => {
let history = false;
if (
!initial &&
name !== `name` &&
name !== `command` &&
name !== `description`
) {
history = {
autosave: true,
store: new Store({
path: path.join(
os.homedir(),
`.history/create-node-cli/${name}.json`
)
})
};
}
const [err, response] = await to(
new Input({
name,
message,
hint,
initial,
history,
validate(value, state) {
if (state && state.name === `command`) return true;
if (state && state.name === `name`) {
if (fs.existsSync(value)) {
return `Directory already exists: ./${value}`;
} else {
return !value ? `Please add a value.` : true;
}
}
return !value ? `Please add a value.` : true;
}
})
.on(`cancel`, () => shouldCancel())
.run()
);
handleError(`INPUT`, err);
return response;
};