UNPKG

8.89 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const program = require('commander');
5const jdf = require('./lib/jdf');
6const logger = require('jdf-log');
7const upload = require('jdf-upload');
8
9module.exports = {
10 init: function (argv) {
11 jdf.init();
12 initCommandWithArgs(argv);
13 },
14 setConfig: jdf.setConfig,
15 server: function(options, callback){
16 if(!options){
17 options = {};
18 }
19 jdf.init();
20 jdf.checkValidDir();
21 if(typeof(options) == 'function'){
22 callback = options;
23 options = {};
24 }
25 jdf.server(options, callback);
26 },
27 build: function(options, callback){
28 if(!options){
29 options = {};
30 }
31 jdf.init();
32 jdf.checkValidDir();
33 if(typeof(options) == 'function'){
34 callback = options;
35 options = {};
36 }
37 jdf.build(options, callback);
38 },
39 output: function(dir, options){
40 if(!dir){
41 dir = [];
42 }
43 if(!options){
44 options = {};
45 }
46 jdf.init();
47 jdf.checkValidDir();
48 jdf.output(dir, options);
49 },
50 upload: function(dir, options){
51 if(!dir){
52 dir = [];
53 }
54 if(!options){
55 options = {};
56 }
57 jdf.init();
58 jdf.checkValidDir();
59 upload(dir, options, jdf);
60 },
61 clean: function(){
62 jdf.clean();
63 },
64 exit: function(){
65 jdf.exit();
66 }
67};
68
69/**
70 * 把一级命令的options转化成二级命令的options
71 * @param fn
72 * @returns {Function}
73 */
74function mergeOptions(fn) {
75 return function () {
76 const args = Array.prototype.slice.call(arguments);
77 // commander.js会把options作为最后一个参数传递过来
78 const lastArgv = args[args.length - 1];
79 if (program.verbose) {
80 lastArgv.logLevel = 'verbose';
81 } else if (program.logLevel) {
82 lastArgv.logLevel = program.logLevel;
83 }
84 logger.level(lastArgv.logLevel);
85 fn.apply(null, args);
86 }
87}
88
89function initCommandWithArgs(argv) {
90 program
91 .version(require('./package.json').version)
92 .usage('[commands] [options]')
93 .option('-L, --logLevel [level]', `show more detail info. ['error', 'warn', 'info', 'verbose', 'debug', 'silly'] are candidates.`, 'info')
94 .option('-v, --verbose', `show verbose info. a shortcut of '--logLevel verbose'`);
95
96 // 所有命令入口初始化
97 initStandardDir();
98 initInstall();
99 initBuild();
100 initServer();
101 initOutput();
102 initUpload();
103 initWidget();
104 initLint();
105 initCompress();
106 initFormat();
107 initClean();
108 initUncaught();
109
110 program.parse(argv);
111
112 if (argv.length <= 2) {
113 program.help();
114 }
115}
116/**
117 * 初始化init命令
118 */
119function initStandardDir() {
120 program
121 .command('init [projectName]')
122 .description('create a new jdf project')
123 .option('-c, --current', 'make this directory as a jdf project')
124 .action(mergeOptions((projectName, options) => {
125 jdf.createStandardDir(projectName, options);
126 }))
127 .on('--help', function () {
128 outputHelp([
129 '$ jdf init',
130 '$ jdf init [projectName]'
131 ]);
132 });
133}
134
135function initInstall(){
136 program
137 .command('install [componentName]')
138 .alias('i')
139 .description('download jdf component to local')
140 .option('-l, --list', 'list all components')
141 .action(mergeOptions((componentName, options) => {
142 jdf.install(componentName, options);
143 }))
144 .on('--help', function(){
145 outputHelp([
146 '$jdf install',
147 '$jdf install [componentName]',
148 '$jdf install --list'
149 ]);
150 });
151}
152
153function initBuild() {
154 program
155 .command('build')
156 .alias('b')
157 .description('build project')
158 .option('-o, --open', 'auto open html/index.html')
159 .action(mergeOptions((options) => {
160 jdf.checkValidDir();
161 jdf.currentCommand = 'build';
162 jdf.build(options);
163 }))
164 .on('--help', function () {
165 outputHelp([
166 '$ jdf build',
167 '$ jdf build --open',
168 ])
169 });
170}
171
172function initOutput() {
173 program
174 .command('output [dir...]')
175 .alias('o')
176 .description('output project')
177 .option('-d, --debug', 'uncompressed js,css,images for test')
178 .option('-p, --plain', 'output project by plain')
179 .action(mergeOptions((dir, options) => {
180 jdf.checkValidDir();
181 jdf.currentCommand = 'output';
182 jdf.output(dir, options);
183 }))
184 .on('--help', function () {
185 outputHelp([
186 '$ jdf output srcPath',
187 '$ jdf output --debug --backup srcPath',
188 '$ jdf output --plain'
189 ]);
190 });
191}
192
193function initUpload() {
194 program
195 .command('upload [dir...]')
196 .alias('u')
197 .description('upload local resources to remote sever')
198 .option('-t, --type [name]', 'which transfer type to use (ftp|scp|http) [ftp]', 'http')
199 .option('-d, --debug', 'uncompressed js,css,images for test')
200 .option('-p, --plain', 'output project by plain')
201 .option('-P, --preview', 'upload html dir to preview server dir')
202 .action(mergeOptions((dir, options) => {
203 jdf.checkValidDir();
204 jdf.currentCommand = 'upload';
205
206 if(!options.preview){
207 options.static = true;
208 }
209 upload(dir, options, jdf);
210 }))
211 .on('--help', function () {
212 outputHelp([
213 '$ jdf upload',
214 '$ jdf upload srcPath',
215 '$ jdf upload --debug --preview srcPath'
216 ]);
217 });
218}
219
220function initWidget() {
221 program
222 .command('widget')
223 .alias('w')
224 .description('create/install/preview/publish widgets')
225 .option('-c, --create <widgetName>', 'create a widget to local')
226 .option('-s, --smarty', 'create a smarty widget to local when use --create.')
227 .option('-P, --preview [widgetName]', 'preview all widgets or preview some specified widget')
228 .option('-l, --list', 'get widget list from server')
229 .option('-i, --install <widgetName>', 'install a widget to local')
230 .option('-p, --publish <widgetName>', 'publish a widget to server')
231 .option('-f, --force', 'force corver when publish or install widget')
232 .action(mergeOptions((options) => {
233 jdf.checkValidDir();
234 const widget = require('./lib/widget');
235 options.force = options.force || false;
236 if (options.preview) {
237 widget.preview(options.preview);
238 }
239
240 if (options.list) {
241 widget.list();
242 }
243
244 if (options.install) {
245 widget.install(options.install, options.force);
246 }
247
248 if (options.publish) {
249 widget.publish(options.publish, options.force);
250 }
251
252 if (options.create) {
253 widget.create(options.create, options.smarty);
254 }
255 }))
256 .on('--help', function () {
257 outputHelp([
258 '$ jdf widget --create widgetName',
259 '$ jdf widget --create widgetName --smarty',
260 '$ jdf widget --preview widgetName1,widgetName2',
261 '$ jdf widget --list',
262 '$ jdf widget --install ui-header --force',
263 '$ jdf widget --publish myWidget',
264 '$ jdf widget --force'
265 ])
266 });
267}
268
269function initCompress() {
270 program
271 .command('compress <srcPath> [destPath]')
272 .alias('c')
273 .description('compress js/css (jdf compress input output)')
274 .action(mergeOptions((srcPath, destPath) => {
275 const compress = require('./lib/urlReplace');
276 compress.dir(srcPath, destPath);
277 }))
278 .on('--help', function () {
279 outputHelp([
280 '$ jdf compress ./js ./js-dest',
281 '$ jdf compress ./css'
282 ])
283 });
284}
285
286function initClean() {
287 program
288 .command('clean')
289 .description('clean cache folder')
290 .action(function () {
291 jdf.clean();
292 })
293 .on('--help', function () {
294 outputHelp(['$ jdf clean']);
295 });
296}
297
298function initServer() {
299 program
300 .command('server')
301 .alias('s')
302 .option('-o, --open', 'auto open browser when server started successfully.')
303 .option('-w, --watch', 'watch html file change and reload browser automatically.')
304 .description('debug for online/RD debug')
305 .action(mergeOptions((options) => {
306 jdf.currentCommand = 'server';
307 jdf.server(options);
308 }))
309 .on('--help', function () {
310 outputHelp([
311 '$ jdf server',
312 '$ jdf server --open',
313 '$ jdf server --watch',
314 '$ jdf server -ow auto open and watch html file change'
315 ]);
316 });
317}
318
319function initLint() {
320 program
321 .command('lint [dir|file]')
322 .alias('l')
323 .description('file lint')
324 .action(function (dir) {
325 const lint = require('./lib/fileLint');
326 const filename = (typeof dir === 'undefined') ? process.cwd() : dir;
327 lint.init(filename);
328 })
329 .on('--help', function () {
330 outputHelp([
331 '$ jdf lint file.js',
332 '$ jdf lint ./src'
333 ]);
334 });
335}
336
337function initFormat() {
338 program
339 .command('format [dir|file]')
340 .alias('f')
341 .description('file formater')
342 .action(function (dir) {
343 const format = require('./lib/fileFormat');
344 const filename = (typeof dir === 'undefined') ? process.cwd() : dir;
345 format.init(filename);
346 })
347 .on('--help', function () {
348 outputHelp([
349 '$ jdf format file.js',
350 '$ jdf format ./src'
351 ]);
352 });
353}
354
355function initUncaught() {
356 program
357 .command('*')
358 .action(function (env) {
359 console.log('jdf error, invalid option: ' + env + '\nType "jdf -h" for help.');
360 });
361}
362
363function outputHelp(helpItems) {
364 console.log(' Examples:');
365 console.log('');
366 helpItems.forEach(function (item) {
367 console.log(' ' + item);
368 })
369}