-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
93 lines (86 loc) 路 1.96 KB
/
cli.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
/* eslint-disable promise/prefer-await-to-then */
'use strict';
const meow = require('meow');
const logSymbols = require('log-symbols');
const updateNotifier = require('update-notifier');
const isOnline = require('is-online');
const Conf = require('conf');
const ui = require('./ui');
const generator = require('./generator');
const config = new Conf();
const cli = meow(
`
Usage
$ ni <package-name>
Options
--dir <dir-name> Place files in a new directory
Examples
$ ni
$ ni package-name
$ ni package-name --dir
$ ni package-name --dir dir-name
`,
{
flags: {
dir: {
type: 'string',
},
},
}
);
Promise.resolve()
.then(() => {
return isOnline({timeout: 1000});
})
.then(online => {
if (online) {
updateNotifier({pkg: cli.pkg}).notify();
} else {
console.warn(`${logSymbols.warning} You are offline 馃敶 !`);
}
return online;
})
.then(online => {
return {
offline: !online,
name: cli.input.length > 0 ? cli.input[0] : undefined,
directory: cli.flags.dir,
};
})
.then(options => {
return Object.assign(
{
author: config.get('author'),
email: config.get('email'),
website: config.get('website'),
},
options
);
})
.then(async options => {
return Object.assign(options, await ui(options));
})
.then(options => {
if (!options.confirm) {
console.log(`${logSymbols.info} Ok, bye 馃憢 .`);
process.exit(0);
}
return options;
})
.then(options => {
config.set('author', options.author);
config.set('email', options.email);
config.set('website', options.website);
return options;
})
.then(options => {
return generator(options);
})
.then(pkg => {
console.log(`${logSymbols.info} ${pkg.name}@${pkg.version} created 馃帀 .`);
})
.catch(err => {
console.error(`\n${logSymbols.error} ${err.message}`);
process.exit(1);
});