-
Notifications
You must be signed in to change notification settings - Fork 79
/
cli.js
113 lines (85 loc) · 2.95 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const fs = require('fs');
const path = require('path');
const os = require('os');
const chalk = require('chalk');
const init = require('./init');
const bootstrap = require('./bootstrap');
const modules = require('./modules');
const version = require('../package.json').version;
const examples = [
['Generate json file with minimal options', '$ blinksocks init --minimal'],
['Start blinksocks client', '$ blinksocks --config blinksocks.client.json'],
['Start blinksocks server', '$ blinksocks --config blinksocks.server.json'],
['List all built-in presets', '$ blinksocks --list-presets']
];
const usage = `
${chalk.bold.underline(`blinksocks v${version}`)}
Usage: blinksocks [command] [options] ...
Commands:
init generate configuration pair
Options:
-h, --help output usage information
-v, --version output blinksocks version
-c, --config file json file with configuration
-m, --minimal generate minimal json files
-w, --write overwrite previous json files
--list-presets list all built-in presets
Examples:
${examples.map(([description, example]) => ` ${chalk.gray('-')} ${description}${os.EOL} ${chalk.blue(example)}`).join(os.EOL)}
About & Help: ${chalk.underline('https://github.com/blinksocks/blinksocks')}
`;
const argv = process.argv;
const options = argv.slice(2);
function hasOption(opt) {
return options.indexOf(opt) !== -1;
}
function getOptionValue(opt) {
const index = options.indexOf(opt);
if (index !== -1) {
return options[index + 1];
}
return undefined;
}
function main() {
if (argv.length < 3) {
return console.log(usage);
}
// try to treat the first argument as conf
const opt0 = options[0];
const maybeConfPath = path.resolve(process.cwd(), opt0);
if (fs.existsSync(maybeConfPath) && path.extname(maybeConfPath) === '.json') {
return bootstrap(maybeConfPath, modules);
}
// parse Commands
if (options[0] === 'init') {
const isMinimal = hasOption('-m') || hasOption('--minimal');
const isOverwrite = hasOption('-w') || hasOption('--write');
return init({ isMinimal, isOverwrite });
}
// parse options
if (hasOption('-h') || hasOption('--help')) {
return console.log(usage);
}
if (hasOption('-v') || hasOption('--version')) {
return console.log(version);
}
if (hasOption('-c') || hasOption('--config')) {
let configPath = getOptionValue('-c') || getOptionValue('--config');
if (configPath === undefined) {
return console.log(chalk.red('config file must be provided'));
}
configPath = path.resolve(process.cwd(), configPath);
if (!fs.existsSync(configPath)) {
return console.log(chalk.red('config file is not found'));
}
return bootstrap(configPath, modules);
}
if (hasOption('--list-presets')) {
const { builtInPresetMap } = modules;
console.log(Object.keys(builtInPresetMap).join(os.EOL));
return;
}
// other cases
console.log(usage);
}
main();