-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslushfile.js
86 lines (81 loc) · 2.8 KB
/
slushfile.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
var gulp = require('gulp'),
conflict = require('gulp-conflict'),
debug = require('gulp-debug'),
ora = require('ora'),
yarnInstall = require('yarn-install'),
npmInstall = require('npm-install-package'),
configPkg = require('./config/package.json'),
_ = require('lodash'),
fs = require('fs'),
path = require('path'),
exists = require('global-module-exists'),
inquirer = require('inquirer');
var cfgPath = './webpack/';
function copyTask(dir, to) {
return new Promise(function(resolve) {
gulp.src(dir)
.pipe(debug())
.pipe(conflict(to))
.pipe(gulp.dest(to))
.on('end', function () {
resolve();
}).resume();
});
}
function mergePackage(source, target) {
var sourceBuffer = fs.readFileSync(source, {encoding: 'utf-8'});
var targetBuffer = fs.readFileSync(target, {encoding: 'utf-8'});
var buffer = _.merge(JSON.parse(targetBuffer), JSON.parse(sourceBuffer));
fs.writeFileSync(target, JSON.stringify(buffer, null, 2));
}
gulp.task('copy files', function (done) {
const prompts = [{
type: 'confirm',
name: 'dll',
message: '是否使用 dllPlugin?'
}]
inquirer.prompt(prompts).then(function (answers) {
const tasks = []
const defaultCopyFiles = [
__dirname + '/config/**',
'!' + __dirname + '/**/package.json',
'!' + __dirname + '/**/node_modules/**'
];
if (answers.dll) {
defaultCopyFiles.push('!' + __dirname + '/config/webpack.config.js', '!' + __dirname + '/config/index.html'); // 使用dll的webpack.config.js;
tasks.push(copyTask([__dirname + '/dll/**', '!' + __dirname + '/**/package.json', '!' + __dirname + '/**/node_modules/**'], cfgPath))
}
tasks.push(copyTask(defaultCopyFiles, cfgPath));
Promise.all(tasks).then(() => {
var configPkgPath = path.resolve(__dirname, './config/package.json');
var pkgPath = path.resolve(process.cwd(), 'package.json');
mergePackage(configPkgPath, pkgPath);
if (answers.dll) {
var dllPkgPath = path.resolve(__dirname, './dll/package.json');
mergePackage(dllPkgPath, pkgPath);
}
}).then(function() {
const dependencies = Object.keys(configPkg.devDependencies).map(function(_module) {
return _module + '@' + configPkg.devDependencies[_module];
});
if (exists('yarn')) {
yarnInstall({
deps: dependencies,
dev: true
})
done();
} else {
var spinner = ora('npm installing packages, please waiting...').start();
npmInstall(dependencies, {saveDev: true, cache: true}, function (err) {
if (err) {
spinner.fail('packages install err: ' + err.message);
throw err;
}
spinner.text = 'packages install Complete';
spinner.stop();
done();
})
}
});
})
})