-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.js
153 lines (146 loc) · 4.18 KB
/
create.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
const { getMessage, getBin } = require('./helpers');
const updateNotifier = require('update-notifier');
const exec = require('child_process').execSync;
const packageJson = require('./package.json');
const Configstore = require('configstore');
const isFolder = require('is-directory');
const inquirer = require('inquirer');
const mkdirp = require('mkdirp');
const fs = require('fs');
require('colors');
const config = new Configstore(packageJson.name);
if (
!(
config.has('language') ||
config.has('authorName') ||
config.has('commandsFolder')
)
) {
console.log(
"Vous devez configurer ce paquet avant de l'utiliser. Exécutez ".red +
'config-cccmd'.white,
);
console.log(
'You must configure this package before using it. Execute '.red +
'config-cccmd'.white,
);
return;
}
const user = exec('whoami').toString().trim();
console.log();
console.log(getMessage('welcomeCreate').blue);
main();
async function main() {
if (!isFolder.sync(config.get('commandsFolder')))
return console.log(
getMessage('noCommandsDir').replace('PATH', config.get('commandsFolder'))
.red,
);
const {
cmdName: name,
cmdDesc: desc,
cmdLanguage: lang,
} = await inquirer.prompt([
{
type: 'input',
name: 'cmdName',
message: getMessage('cmdName'),
validate(val) {
let exists;
try {
fs.lstatSync(getBin() + val);
err = true;
} catch {}
if (val === '') return getMessage('emptyCmdName');
else if (val.includes(' ')) return getMessage('cmdNameWithSpace');
else if (/[!@#$%^&*(),.?":{}|<>/]/.test(val))
return getMessage('cmdNameWithSymbols');
else if (
exists ||
fs.existsSync('/usr/bin/' + val) ||
fs.existsSync('/bin/' + val) ||
fs.existsSync(getBin() + val) ||
fs.existsSync(config.get('commandsFolder') + val)
)
return getMessage('cmdAlreadyExists');
else return true;
},
},
{
type: 'input',
name: 'cmdDesc',
message: getMessage('cmdDesc'),
validate(val) {
if (val === '') return getMessage('emptyCmdDesc');
else return true;
},
},
{
type: 'list',
name: 'cmdLanguage',
message: getMessage('cmdLanguage'),
choices: [
{
name: 'JS',
value: 'js',
},
{
name: 'BASH / ZSH',
value: 'bash',
},
],
},
]);
const author = config.get('authorName');
switch (lang) {
case 'js':
console.log(getMessage('creatingCommand').green);
try {
const indexJS = `#!/usr/bin/env node\nrequire('colors');\n`;
const packageJSON = {
name,
version: '1.0.0',
description: desc,
main: 'index.js',
author,
license: 'ISC',
};
const dirPath = `${config.get('commandsFolder')}${name}/`;
mkdirp.sync(dirPath);
fs.writeFileSync(dirPath + 'index.js', indexJS);
fs.writeFileSync(dirPath + 'package.json', JSON.stringify(packageJSON));
exec(
`cd ${dirPath} && npm install colors && chmod +x index.js && sudo ln -s ${dirPath}index.js ${getBin()}${name}`,
);
console.log(getMessage('cmdCreated').replace('NAME', name).green);
} catch {
console.log(getMessage('cmdError').red);
}
break;
case 'bash':
console.log(getMessage('creatingCommand').green);
try {
const dirPath = config.get('commandsFolder');
mkdirp.sync(dirPath);
fs.writeFileSync(
dirPath + name,
`# ${name} (${desc}) ${getMessage('by')} ${author}`,
);
exec(
`cd ${dirPath} && chmod +x ${name} && sudo ln -s ${dirPath}${name} ${getBin()}${name}`,
);
console.log(getMessage('cmdCreated').replace('NAME', name).green);
} catch (err) {
console.log(err, getMessage('cmdError').red);
}
break;
}
const notifier = updateNotifier({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 24,
});
if (notifier.update) {
console.log(getMessage('updateAvailable').green.bold);
}
}