-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
140 lines (114 loc) · 4.51 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
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
feather.cli.name = 'feather2';
feather.cli.info = feather.util.readJSON(__dirname + '/package.json');
feather.require.prefixes.unshift('feather', 'feather2');
feather.set('modules.commands', ['init', 'release', 'server', 'install', 'inspect']);
feather.cli.version = function(){
var string = feather.util.read(__dirname + '/vendor/icon', true);
console.log(string.replace('{version}', ('Version: ' + feather.cli.info.version).bold.red));
}
var old = feather.cli.run;
//override run
feather.cli.run = function(argv, env){
var first = argv[2], action = argv._[0];
feather._argv = argv;
if(['release', 'server', 'init', 'install', 'inspect'].indexOf(action) > -1){
switch(action){
case 'server':
if(!argv.type){
argv.type = feather.config.get('server.type');
}
old(argv, env);
break;
case 'install':
old(argv, env);
break;
case 'release':
if(argv.h || argv.help){
feather.cli.help('release [media name]', {
'-h, --help': 'print this help message',
'-d, --dest <path>': 'release output destination',
'-l, --lint': 'with lint',
'-w, --watch': 'monitor the changes of project',
'-L, --live': 'automatically reload your browser',
'-c, --clean': 'clean compile cache',
'-u, --unique': 'use unique compile caching',
'-r, --root <path>': 'specify project root',
'-f, --file <filename>': 'specify the file path of feather\'s config file',
'--no-color': 'disable colored output',
'--verbose': 'enable verbose mode'
});
break;
}
if(!env.configPath){
feather.log.error('Not found feather\'s config file! Please confirm it\'s a valid feather project');
}
var dest = argv.d || argv.dest;
if(typeof dest != 'string'){
dest = argv.dest = 'preview';
feather.isPreviewMode = true;
delete argv.d;
}
feather.config.set('cli', {
dest: dest,
isPreviewMode: !!feather.isPreviewMode,
watch: !!(argv.w || argv.watch),
media: argv._[1] || 'dev'
});
require('./conf-loaded.js');
old(argv, env);
break;
default:
old(argv, env);
}
}else{
if(argv.version || argv.v){
feather.cli.version();
}else{
feather.cli.help(null, {
'-h, --help': 'print this help message',
'-v, --version': 'print product version and exit'
});
}
}
};
feather.cli.help = function(cmdName, options, commands){
var strs = [
'',
' Usage: ' + feather.cli.name + ' ' + (cmdName ? cmdName : '<command>')
];
var _ = feather.util, util = require('util');
if(!cmdName){
commands = {};
feather.media().get('modules.commands', []).forEach(function(name){
var cmd = feather.require('command', name);
name = cmd.name || name;
name = feather.util.pad(name, 12);
commands[name] = cmd.desc || '';
});
}
options = options || {};
commands = commands || {};
var optionsKeys = Object.keys(options);
var commandsKeys = Object.keys(commands);
var maxWidth;
if(commandsKeys.length){
maxWidth = commandsKeys.reduce(function(prev, curr){
return curr.length > prev ? curr.length : prev;
}, 0) + 4;
strs.push(null, ' Commands:', null);
commandsKeys.forEach(function(key){
strs.push(util.format(' %s %s', _.pad(key, maxWidth), commands[key]));
});
}
if(optionsKeys.length){
maxWidth = optionsKeys.reduce(function(prev, curr){
return curr.length > prev ? curr.length : prev;
}, 0) + 4;
strs.push(null, ' Options:', null);
optionsKeys.forEach(function(key){
strs.push(util.format(' %s %s', _.pad(key, maxWidth), options[key]));
});
strs.push(null);
}
console.log(strs.join('\n'));
};