-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
129 lines (107 loc) · 3.54 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
var _ = fis.util;
var path = require('path');
var watch = require('./lib/watch.js');
var release = require('./lib/release.js');
var deploy = require('./lib/deploy.js');
var livereload = require('./lib/livereload.js');
var time = require('./lib/time.js');
var checkIgnore = require('./lib/checkignore.js');
exports.name = 'release [media name]';
exports.desc = 'build and deploy your project';
exports.options = {
'-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 `fis-conf.js`',
'--no-color': 'disable colored output',
'--verbose': 'enable verbose mode'
};
exports.run = function(argv, cli, env) {
// 显示帮助信息
if (argv.h || argv.help) {
return cli.help(exports.name, exports.options);
}
validate(argv);
// normalize options
var options = {
dest: argv.dest || argv.d || 'preview',
watch: !!(argv.watch || argv.w),
live: !!(argv.live || argv.L),
clean: !!(argv.clean || argv.c),
unique: !!(argv.unique || argv.u),
useLint: !!(argv.lint || argv.l),
verbose: !!argv.verbose
};
// enable watch automatically when live is enabled.
options.live && (options.watch = true);
var app = require('./lib/chains.js')();
app.use(function(options, next) {
// clear cache?
if (options.clean) {
time(function() {
fis.cache.clean('compile');
});
} else if (env.configPath) {
// fis-conf 失效?
var cache = fis.cache(env.configPath, 'conf');
if(!cache.revert()){
cache.save();
time(function() {
fis.cache.clean('compile');
});
}
}
next(null, options);
});
// watch it?
options.watch && app.use(watch);
app.use(release);
// 处理 livereload 脚本
app.use(livereload.handleReloadComment);
// deliver
app.use(function(info, next) {
fis.log.debug('deploy start');
deploy(info, function(error) {
fis.log.debug('deploy end');
next(error, info);
});
});
options.live && app.use(livereload.checkReload);
// output fix
if (_.is(options['dest'], 'String')) {
var root = fis.project.getProjectPath();
var dest = path.resolve(root, options['dest']);
var isInRoot = dest.indexOf(root) === 0;
if (isInRoot && !checkIgnore(dest.substring(root.length)) && _.exists(dest)) {
fis.log.warn('skip `output` directory: ' + dest);
// maybe fis.set('project.ignore', 'node_modules/**');
if (!_.is(fis.get('project.ignore'), 'Array')) {
fis.set('project.ignore', [fis.get('project.ignore')]);
}
fis.set(
'project.ignore',
fis.get('project.ignore').concat(
[dest.replace(root + '/', '') + '/**']
)
);
}
}
// run it.
app.run(options);
};
function validate(argv) {
if (argv._.length > 2) {
fis.log.error('Unregconized `%s`, please run `%s release --help`', argv._.slice(2).join(' '), fis.cli.name);
}
var allowed = ['_', 'dest', 'd', 'lint', 'l', 'watch', 'w', 'live', 'L', 'clean', 'c', 'unique', 'u', 'verbose', 'color', 'root', 'r', 'f', 'file', 'child-flag'];
Object.keys(argv).forEach(function(k) {
if (!~allowed.indexOf(k)) {
fis.log.error('The option `%s` is unregconized, please run `%s release --help`', k, fis.cli.name);
}
});
}