-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgulpfile.js
73 lines (63 loc) · 1.87 KB
/
gulpfile.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
var gulp = require('gulp');
var argv = require('yargs').argv;
var async = require('async');
var colors = require('colors');
var fs = require('fs-extra');
var path = require('path');
var common = require('./module/common.js');
var Git = require('./module/git.js');
var Development = require('./module/development.js');
var Production = require('./module/production.js');
var util = require('./module/util.js');
process.on('exit', code => {
code !== 0 && console.error(colors.red('发布失败'));
});
function getProject() {
return {
repo: argv.repo,
branch: argv.branch || 'master',
dist: argv.dist,
publicPath: argv.publicPath
};
}
gulp.task('before', done => {
if(!argv.repo) {
return done('请输入仓库地址,参数--repo');
}
var timer;
var project = getProject();
var git = new Git(argv.repo);
function clone(cb) {
var src = common.getCwd(argv.repo, 'src');
try {
fs.accessSync(path.join(src, '.git'));
cb();
} catch (err) {
git.clone(cb);
}
}
var tasks = [
clone,
git.checkout.bind(git, '.'), // 确保本地仓库是干净的
git.exec.bind(git, 'fetch', ['--all']),
git.checkout.bind(git, project.branch),
git.exec.bind(git, 'pull')
];
async.series(tasks, err => {
clearTimeout(timer);
err && console.error(colors.red(err));
done(err);
});
timer = setTimeout(() => done('发布超时,请稍后重试'), 30 * 60 * 1000);
});
gulp.task('clean', done => {
util.clean(common.getCwd(argv.repo, 'build'))(done);
});
gulp.task('development', gulp.series('before', 'clean', function main(done) {
var dev = new Development(getProject());
dev.run(argv.commit || 'HEAD', done);
}));
gulp.task('production', gulp.series('before', 'clean', function main(done) {
var p = new Production(getProject());
p.run(argv.commit || 'HEAD', done);
}));