-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtank.js
174 lines (158 loc) · 4.94 KB
/
tank.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
/**
* Created by kyriosli on 2014/6/18.
*
* Usage:
* tank [command] [path] [args]
*/
global.info = function (msg) {
console.info('\x1b[42mINFO\x1b[0m ' + msg);
};
global.warn = function (msg) {
console.warn('\x1b[43mWARN\x1b[0m ' + msg);
};
global.fatal = function (msg) {
console.error('\x1b[41mFATAL\x1b[0m ' + msg);
throw 'exiting';
};
process.on('uncaughtException', function (e) {
if (typeof e !== 'string') {
process.stderr.write(e.stack || e.message);
} else {
process.stderr.on('drain', function () {
process.exit(1);
});
}
});
var commands = require('./src/command'),
cmd = process.argv[2] || 'help';
if (!commands[cmd]) {
console.error('Unrecognized command: ' + cmd);
return commands.help.action();
}
if (commands[cmd].alias) {
cmd = commands[cmd].alias;
}
if (commands[cmd].noExt) {
return commands[cmd].action();
}
var appinfo = {path: process.cwd()},
cmdinfo = commands[cmd],
addons = {},
env = process.env, args = {};
// scan for all addons
initAddons();
// check for all arguments
parseArgs();
addons = Object.keys(addons);
if (addons.length) {
env.tankjs_addons = addons.join(',');
}
// run command!
commands[cmd].action(appinfo, env, args);
// BEGIN method definition
function initAddons() {
require('./src/scan')(function (dir, name) {
// console.log('check', prefix, name);
var arr = require(dir + '/' + name + '/package.json').args;
if (!arr)
return;
Object.keys(arr).forEach(function (label) {
var arg = arr[label];
arg.addon = name;
arg.commands.forEach(function (cmd) {
(commands[cmd].args || (commands[cmd].args = {})) [label] = arg;
});
// load default module
if (arg.defaults && cmdinfo.args && cmdinfo.args[label]) {
addons[name] = true;
if (arg.bind_env) {
env[arg.bind_env] = arg.defaults;
// console.log('set env', arg.bind_env, arg.defaults);
}
}
});
});
}
function parseArgs() {
var foundAppInfo = false;
for (var arr = process.argv, L = arr.length, i = 3; i < L; i++) {
var argi = arr[i], label, name, value;
if (argi[0] !== '-') { // appinfo
if (!foundAppInfo) {
var fs = require('fs');
if (cmd === 'add') {
if (/^\w+$/.test(argi)) {
argi = 'rapid-' + argi;
} else if (!/^rapid-\w+$/.test(argi)) {
fatal('bad module name');
}
appinfo = {
modules: [argi]
};
} else if (/^\d+$/.test(argi)) { // pid
appinfo.pid = +argi;
} else if (/^app\w+$/.test(argi)) { // id
appinfo.id = argi;
} else if (!fs.existsSync(argi)) {
fatal('app path not found: `' + argi + '`');
} else {
appinfo = {
cwd: process.cwd(),
path: fs.realpathSync(argi)
};
}
foundAppInfo = true;
} else {
if (cmd === 'add') {
if (/^\w+$/.test(argi)) {
argi = 'rapid-' + argi;
} else if (!/^rapid-\w+$/.test(argi)) {
fatal('bad module name');
}
appinfo.modules.push(argi);
} else {
warn('ingoring unknown argument: `' + argi + '`');
}
}
continue;
}
if (argi.length === 1) { // -
// WTF??
continue;
}
if (argi[1] === '-') { // --name=value
var idx = argi.indexOf('=', 2);
if (idx === -1) { // --name
name = argi.substr(2);
value = true;
} else {
name = argi.substring(2, idx);
value = argi.substr(idx + 1);
argi = argi.substr(0, idx);
}
} else { // -kv
name = argi[1];
if (argi.length > 2) {
value = argi.substr(2);
argi = argi.substr(0, 2);
} else { // -k
value = true;
}
}
// console.log(argi, name, value);
var arg = cmdinfo.args && cmdinfo.args[argi];
if (!arg) {
warn('ingoring unknown parameter ' + argi);
} else {
if (arg.addon)
addons[arg.addon] = true;
// console.log(arg);
if (arg.bind_env) {
env[arg.bind_env] = value;
// console.log('set env', arg.bind_env, value);
}
args[name] = value;
}
}
}