-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
369 lines (345 loc) · 8.68 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
'use strict';
const path = require('path');
const program = require('commander');
const jdf = require('./lib/jdf');
const logger = require('jdf-log');
const upload = require('jdf-upload');
module.exports = {
init: function (argv) {
jdf.init();
initCommandWithArgs(argv);
},
setConfig: jdf.setConfig,
server: function(options, callback){
if(!options){
options = {};
}
jdf.init();
jdf.checkValidDir();
if(typeof(options) == 'function'){
callback = options;
options = {};
}
jdf.server(options, callback);
},
build: function(options, callback){
if(!options){
options = {};
}
jdf.init();
jdf.checkValidDir();
if(typeof(options) == 'function'){
callback = options;
options = {};
}
jdf.build(options, callback);
},
output: function(dir, options){
if(!dir){
dir = [];
}
if(!options){
options = {};
}
jdf.init();
jdf.checkValidDir();
jdf.output(dir, options);
},
upload: function(dir, options){
if(!dir){
dir = [];
}
if(!options){
options = {};
}
jdf.init();
jdf.checkValidDir();
upload(dir, options, jdf);
},
clean: function(){
jdf.clean();
},
exit: function(){
jdf.exit();
}
};
/**
* 把一级命令的options转化成二级命令的options
* @param fn
* @returns {Function}
*/
function mergeOptions(fn) {
return function () {
const args = Array.prototype.slice.call(arguments);
// commander.js会把options作为最后一个参数传递过来
const lastArgv = args[args.length - 1];
if (program.verbose) {
lastArgv.logLevel = 'verbose';
} else if (program.logLevel) {
lastArgv.logLevel = program.logLevel;
}
logger.level(lastArgv.logLevel);
fn.apply(null, args);
}
}
function initCommandWithArgs(argv) {
program
.version(require('./package.json').version)
.usage('[commands] [options]')
.option('-L, --logLevel [level]', `show more detail info. ['error', 'warn', 'info', 'verbose', 'debug', 'silly'] are candidates.`, 'info')
.option('-v, --verbose', `show verbose info. a shortcut of '--logLevel verbose'`);
// 所有命令入口初始化
initStandardDir();
initInstall();
initBuild();
initServer();
initOutput();
initUpload();
initWidget();
initLint();
initCompress();
initFormat();
initClean();
initUncaught();
program.parse(argv);
if (argv.length <= 2) {
program.help();
}
}
/**
* 初始化init命令
*/
function initStandardDir() {
program
.command('init [projectName]')
.description('create a new jdf project')
.option('-c, --current', 'make this directory as a jdf project')
.action(mergeOptions((projectName, options) => {
jdf.createStandardDir(projectName, options);
}))
.on('--help', function () {
outputHelp([
'$ jdf init',
'$ jdf init [projectName]'
]);
});
}
function initInstall(){
program
.command('install [componentName]')
.alias('i')
.description('download jdf component to local')
.option('-l, --list', 'list all components')
.action(mergeOptions((componentName, options) => {
jdf.install(componentName, options);
}))
.on('--help', function(){
outputHelp([
'$jdf install',
'$jdf install [componentName]',
'$jdf install --list'
]);
});
}
function initBuild() {
program
.command('build')
.alias('b')
.description('build project')
.option('-o, --open', 'auto open html/index.html')
.action(mergeOptions((options) => {
jdf.checkValidDir();
jdf.currentCommand = 'build';
jdf.build(options);
}))
.on('--help', function () {
outputHelp([
'$ jdf build',
'$ jdf build --open',
])
});
}
function initOutput() {
program
.command('output [dir...]')
.alias('o')
.description('output project')
.option('-d, --debug', 'uncompressed js,css,images for test')
.option('-p, --plain', 'output project by plain')
.action(mergeOptions((dir, options) => {
jdf.checkValidDir();
jdf.currentCommand = 'output';
jdf.output(dir, options);
}))
.on('--help', function () {
outputHelp([
'$ jdf output srcPath',
'$ jdf output --debug --backup srcPath',
'$ jdf output --plain'
]);
});
}
function initUpload() {
program
.command('upload [dir...]')
.alias('u')
.description('upload local resources to remote sever')
.option('-t, --type [name]', 'which transfer type to use (ftp|scp|http) [ftp]', 'http')
.option('-d, --debug', 'uncompressed js,css,images for test')
.option('-p, --plain', 'output project by plain')
.option('-P, --preview', 'upload html dir to preview server dir')
.action(mergeOptions((dir, options) => {
jdf.checkValidDir();
jdf.currentCommand = 'upload';
if(!options.preview){
options.static = true;
}
upload(dir, options, jdf);
}))
.on('--help', function () {
outputHelp([
'$ jdf upload',
'$ jdf upload srcPath',
'$ jdf upload --debug --preview srcPath'
]);
});
}
function initWidget() {
program
.command('widget')
.alias('w')
.description('create/install/preview/publish widgets')
.option('-c, --create <widgetName>', 'create a widget to local')
.option('-s, --smarty', 'create a smarty widget to local when use --create.')
.option('-P, --preview [widgetName]', 'preview all widgets or preview some specified widget')
.option('-l, --list', 'get widget list from server')
.option('-i, --install <widgetName>', 'install a widget to local')
.option('-p, --publish <widgetName>', 'publish a widget to server')
.option('-f, --force', 'force corver when publish or install widget')
.action(mergeOptions((options) => {
jdf.checkValidDir();
const widget = require('./lib/widget');
options.force = options.force || false;
if (options.preview) {
widget.preview(options.preview);
}
if (options.list) {
widget.list();
}
if (options.install) {
widget.install(options.install, options.force);
}
if (options.publish) {
widget.publish(options.publish, options.force);
}
if (options.create) {
widget.create(options.create, options.smarty);
}
}))
.on('--help', function () {
outputHelp([
'$ jdf widget --create widgetName',
'$ jdf widget --create widgetName --smarty',
'$ jdf widget --preview widgetName1,widgetName2',
'$ jdf widget --list',
'$ jdf widget --install ui-header --force',
'$ jdf widget --publish myWidget',
'$ jdf widget --force'
])
});
}
function initCompress() {
program
.command('compress <srcPath> [destPath]')
.alias('c')
.description('compress js/css (jdf compress input output)')
.action(mergeOptions((srcPath, destPath) => {
const compress = require('./lib/urlReplace');
compress.dir(srcPath, destPath);
}))
.on('--help', function () {
outputHelp([
'$ jdf compress ./js ./js-dest',
'$ jdf compress ./css'
])
});
}
function initClean() {
program
.command('clean')
.description('clean cache folder')
.action(function () {
jdf.clean();
})
.on('--help', function () {
outputHelp(['$ jdf clean']);
});
}
function initServer() {
program
.command('server')
.alias('s')
.option('-o, --open', 'auto open browser when server started successfully.')
.option('-w, --watch', 'watch html file change and reload browser automatically.')
.description('debug for online/RD debug')
.action(mergeOptions((options) => {
jdf.currentCommand = 'server';
jdf.server(options);
}))
.on('--help', function () {
outputHelp([
'$ jdf server',
'$ jdf server --open',
'$ jdf server --watch',
'$ jdf server -ow auto open and watch html file change'
]);
});
}
function initLint() {
program
.command('lint [dir|file]')
.alias('l')
.description('file lint')
.action(function (dir) {
const lint = require('./lib/fileLint');
const filename = (typeof dir === 'undefined') ? process.cwd() : dir;
lint.init(filename);
})
.on('--help', function () {
outputHelp([
'$ jdf lint file.js',
'$ jdf lint ./src'
]);
});
}
function initFormat() {
program
.command('format [dir|file]')
.alias('f')
.description('file formater')
.action(function (dir) {
const format = require('./lib/fileFormat');
const filename = (typeof dir === 'undefined') ? process.cwd() : dir;
format.init(filename);
})
.on('--help', function () {
outputHelp([
'$ jdf format file.js',
'$ jdf format ./src'
]);
});
}
function initUncaught() {
program
.command('*')
.action(function (env) {
console.log('jdf error, invalid option: ' + env + '\nType "jdf -h" for help.');
});
}
function outputHelp(helpItems) {
console.log(' Examples:');
console.log('');
helpItems.forEach(function (item) {
console.log(' ' + item);
})
}