forked from UniqueNetwork/unique-playgrounds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
47 lines (45 loc) · 1.26 KB
/
cli.js
File metadata and controls
47 lines (45 loc) · 1.26 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
const runPlayground = async (args) => {
let playground;
try {
playground = require(`./src/playgrounds/${args[1]}`);
}
catch(e) {
if (e.code === 'MODULE_NOT_FOUND' && e.requireStack.length < 2) {
console.log(`Playground '${args[1]}' could not be found. Does file 'src/playgrounds/${args[1]}.js' exist?`);
} else {
console.log(`Playground ${args[1]} could not be loaded: [ERROR]`, e.message.split('\n')[0]);
}
return;
}
if(args.indexOf('--help') > -1) {
console.log(playground.help);
return;
}
return await playground.main(args.slice(2));
}
const runCommand = async (args) => {
let command;
try {
const { CommandCls } = require(`./src/commands/${args[1]}`);
command = new CommandCls();
}
catch(e) {
console.log(`Command ${args[1]} not found`);
return;
}
return await command.execute(args.slice(2), `npm run -- command ${args[1]}`);
}
const main = async () => {
let args = process.argv.slice(2);
if(!args.length) return;
if(args[0] === 'run_playground' && args.length >= 2) {
return await runPlayground(args);
}
if(args[0] === 'run_command' && args.length >= 2) {
return await runCommand(args);
}
}
main().then(() => process.exit(0)).catch(e => {
console.error(e);
process.exit(1);
});