-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (70 loc) · 2.91 KB
/
index.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
const fs = require('fs');
const path = require('path');
const resolveUp = require('resolve-up');
module.exports = (program, client) => {
program
.command('gw:dev')
.description('GateWay.js development mode')
.option('-d, --cwd [cwd]', 'Service startup directory', process.cwd())
.option('-n, --max [max]', 'Maximum number of service processes started', 0)
.option('-p, --port [port]', 'Service startup port', 8080)
.option('-s, --socket', 'Whether to start websocket', false)
.action(client.require('./lib/development'));
program
.command('gw:start')
.description('GateWay.js production mode')
.option('-d, --cwd [cwd]', 'Service startup directory', process.cwd())
.option('-n, --max [max]', 'Maximum number of service processes started', 0)
.option('-p, --port [port]', 'Service startup port', 8080)
.option('-s, --socket', 'Whether to start websocket', false)
.action(client.require('./lib/production'));
program
.command('gw:restart')
.description('GateWay.js production restart mode')
.action(client.require('./lib/restart'));
program
.command('gw:stop')
.description('GateWay.js production stop mode')
.action(client.require('./lib/stop'));
program
.command('gw <path>')
.description('create a new gw file by type')
.option('-c, --controller', 'create a new `Controller` file')
.option('-m, --middleware', 'create a new `Middleware` file')
.option('-s, --service', 'create a new `Service` file')
.option('-t, --decorate', 'create a new `Decorate` file')
.option('-o, --micro', 'create a new `Micro` file')
.option('-w, --websocket', 'create a new `Websocket` file')
.action(client.require('./lib/file'));
program
.command('gw:new [project]')
.description('create a new gw project or plugin')
.option('-p, --plugin', 'create new plugin mode')
.action(client.require('./lib/create'));
program
.command('gw:setup <plugins...>')
.description('setup plugins into project')
.option('-r, --registry <host>', 'which host can been choosed?')
.action(client.require('./lib/setup'));
let plugins = [];
const packageFilePath = path.resolve(process.cwd(), 'package.json');
if (fs.existsSync(packageFilePath)) {
const pkg = require(packageFilePath);
if (pkg.plugins) {
plugins = Object.keys(pkg.plugins);
}
}
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
const modulePaths = resolveUp(plugin);
if (!modulePaths.length) continue;
const modulePath = modulePaths[0];
const execFile = path.resolve(modulePath, 'commander.js');
if (!fs.existsSync(execFile)) continue;
const moduleExports = require(execFile);
if (typeof moduleExports === 'function' && moduleExports.__IS_CLI_PLUGIN__) {
moduleExports(program, new client.constructor(modulePath, client.util, client.pkg));
}
}
}
module.exports.__IS_CLI_PLUGIN__ = true;