-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·84 lines (78 loc) · 2.83 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
#!/usr/bin/env node
const pm2 = require('pm2');
const filendir = require('filendir');
const path = require('path');
const pkg = require('./package.json');
const program = require('commander');
const inquirer = require('inquirer');
const config = require('./defaultconfig.js');
program
.version(pkg.version, '-v, --version');
program
.command('stop [name]')
.description('stop bridgends')
.action((name, cmd) => {
pm2.stop(name || 'index');
setTimeout(() => {
console.log('bridgends is stoped');
process.exit(2);
}, 2000);
});
program
.command('reset [name]')
.description('reset bridgends and remove caches')
.action((name, cmd) => {
// remove files
})
program
.command('start [name]')
.description('start new instance of bridgends')
.option('-t, --targets <targets>', 'set api targets', list => list.split(','))
.option('-f, --save-path [value]', 'Set files path')
.option('-p, --port [value]', 'set instance port')
.action((name, cmd) => {
const filePath = cmd.savePath || config.savePath;
try {
filendir.writeFileSync(path.join(filePath, 'test.tmp'));
} catch(e) {
console.log('you have no write access in ' + filePath + ', use -f option eg: -f /home/fingerpich/bfiles');
return;
}
(() => {
if (cmd.targets) {
console.log('targets: ' + cmd.targets);
return Promise.resolve(cmd.targets)
} else {
return inquirer.prompt({type: 'string', name: 'targets', message: 'you have to had an api target at least, enter targets(eg: http://192.168.10.20:4243/)'})
}
})().then((targets) => {
pm2.connect(function(err) {
if (err) {
console.error(err.message);
process.exit(2);
}
console.log('bridgends has started');
const port = cmd.port || config.port;
const args =
(cmd.savePath? ' --savePath ' + cmd.savePath : '') +
(cmd.port? ' --port ' + cmd.port : '') +
(targets? ' --targets ' + targets : '') +
(name? ' --name ' + name : '');
console.log(args)
console.log('open http://localhost:' + port + '/ in your browser');
pm2.start({
name,
script: __dirname + '/index.js',
args: args
}, function (err, apps) {
pm2.disconnect();
if (err) {
console.error(err.message);
}
});
});
});
});
program
.parse(process.argv);
if (program.args.length === 0) program.help();