-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
356 lines (323 loc) · 11.7 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
require('./lib/nodeSearchPath');
var gulp = require('gulp');
var Promise = require('bluebird');
var Path = require('path');
var gutil = require('gulp-util');
var jshint = require('gulp-jshint');
var jscs = require('gulp-jscs');
var bump = require('gulp-bump');
var through = require('through2');
var del = require('del');
var jscs = require('gulp-jscs');
var vps = require('vinyl-paths');
var File = require('vinyl');
var Q = require('q');
Q.longStackSupport = true;
var _ = require('lodash');
var chalk = require('chalk');
var fs = require('fs');
var runSequence = require('run-sequence');
var cli = require('shelljs-nodecli');
var findPackageJson = require('./lib/gulp/findPackageJson');
var packageLintableSrc = require('./lib/gulp/packageLintableSrc');
var watchPackages = require('./lib/gulp/watchPackages');
var recipeManager = require('./lib/gulp/recipeManager');
var PackageInstall = require('./lib/gulp/packageInstallMgr');
var argv = require('yargs').usage('Usage: $0 <command> [-b <bundle>] [-p package]\n' +
'$0 link [-r <recipe folder>] [-d <src folder>]')
.command('build', 'build everything from scratch, including install-recipe, link, npm install, compile')
.command('clean', 'cleanup build environment like dist/static folder, cache, recipe package.json, even those private modules in node_modules folder')
.command('clean:dist', 'only cleanup dist/static folder, do not cleanup private packages in node_modules')
.command('compile', 'link recipe, compile package into static browser bundles')
.command('lint', 'source code style check')
.command('install-recipe', 'link newly changed package.json files to recipe folder and `npm install` them, this makes sure all dependencies being installed')
.command('watch', 'automatically rebuild specific bundle file when changes on browser packages source code is detected, if you change any package.json or add/remove packages, you need to restart watch command')
.command('link', 'link newly changed package.json files to recipe folder')
.command('build-prod', 'disable config.local.yaml, build for production environment')
.command('publish', 'npm publish every pakages in source code folder including all mapped recipes')
.command('unpublish', 'npm unpublish every pakages in source code folder including all mapped recipes of version number in current source code')
.command('bump', '[-v major|minor|patch|prerelease] bump version number of all package.json, useful to call this before publishing packages, default is increasing patch number by 1')
.command('flatten-recipe', 'flattern NPM v2 nodule_modules structure, install-recipe comamnd will execute this command')
.command('test', '[-p <package-short-name>] [-f <spec-file-path>] run Jasmine test for specific or all packages')
.describe('b', '<bundle-name> if used with command `compile` or `build`, it will only compile specific bundle, which is more efficient')
.alias('b', 'bundle')
.describe('p', '<package-short-name> if used with command `compile`, `build`, `lint`, it will only build and check style on specific package, which is more efficient')
.alias('p', 'package')
.describe('only-js', 'only rebuild JS bundles')
.describe('only-css', 'only rebuild CSS bundles')
.describe('d', '<src foldr> if used with command `link`, it will link packages from specific folder instead of `srcDir` configured in config.yaml')
.describe('r', '<recipe foldr> if used with command `link`, it will link packages only to specific recipe folder instead of `recipeFolder` configured in config.yaml')
.describe('v', 'major | minor | patch | prerelease, used with `bump`')
.describe('f', '<file-path> command `gulp test -f specFile1 [-f specFile2] ...`')
.alias('f', 'file')
.demand(1)
.help('h').alias('h', 'help')
.argv;
var config = require('./lib/config');
require('log4js').configure(Path.join(__dirname, 'log4js.json'));
var packageInstaller = PackageInstall();
//var IS_NPM2 = _.startsWith(shell.exec('npm -v').output, '2.');
gulp.task('default', function() {
gutil.log('please individually execute gulp [task]');
gutil.log('\tbuild clean, link, compile [-b <bundle> ...], bump, publish');
});
gulp.task('clean:dependency', function() {
var dirs = [];
_.each(config().packageScopes, function(packageScope) {
var npmFolder = Path.resolve('node_modules', '@' + packageScope);
gutil.log('delete ' + npmFolder);
dirs.push(npmFolder);
});
return del(dirs).then(gutil.log, gutil.log);
});
gulp.task('clean:recipe', function() {
recipeManager.clean();
});
gulp.task('clean:dist', function() {
return del([config().staticDir, config().destDir]);
});
gulp.task('clean', ['clean:dist', 'clean:dependency', 'clean:recipe']);
gulp.task('build', (cb)=> {
_npmInstallCurrFolder()
.then( ()=> {
runSequence('install-recipe', 'compile', cb);
});
});
gulp.task('build-prod', ['clean:dist'], (cb)=> {
_npmInstallCurrFolder()
.then( ()=> {
config.disableLocal();
runSequence('install-recipe', 'compile', cb);
});
});
gulp.task('compile-prod', (cb)=> {
config.disableLocal();
runSequence('compile', cb);
});
function _npmInstallCurrFolder() {
return new Promise(function(resolve, reject) {
if (!config().dependencyMode) {
// Use asynchronous `ShellJS.exec()` for long-lived process,
// due to ShellJS's high CPU usage issue
cli.exec('npm', 'install', function(code, output) {
if (code === 0) {
resolve();
} else {
reject(output);
}
});
} else {
resolve();
}
});
}
gulp.task('install-recipe', ['link'], function(cb) {
//var lookingForDeps = configuredVendors();
var prom = Promise.resolve();
if (config().dependencyMode) {
prom = prom
.then(function() {
var currPkJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
var savedVer = currPkJson.dependencies ? currPkJson.dependencies['@dr/internal-recipe'] :
(currPkJson.devDependencies ? currPkJson.devDependencies['@dr/internal-recipe'] : false);
if (savedVer) {
return packageInstaller.installRecipeAsync('@dr/internal-recipe@' + savedVer);
} else {
return packageInstaller.installRecipeAsync(config().internalRecipeFolderPath);
}
});
}
prom = prom.then(() => {
return new Promise((resolve, reject) => {
process.nextTick(() => {
// Put it in process.nextTick to hopefully solve a windows install random 'EPERM' error.
flattenRecipe();
resolve();
});
});
});
var srcDirs = [];
recipeManager.eachRecipeSrc(function(src, recipe) {
srcDirs.push(src);
});
prom = prom.then(()=> {
return packageInstaller.scanSrcDepsAsync(srcDirs);
}).then( () => {
return packageInstaller.installDependsAsync();
}).then(() => cb());
});
gulp.task('flatten-recipe', flattenRecipe);
function flattenRecipe() {
packageInstaller.flattenInstalledRecipes();
return null;
}
gulp.task('check-dep', function() {
var mgr = new PackageInstall();
var srcDirs = [];
recipeManager.eachRecipeSrc(function(src, recipe) {
srcDirs.push(src);
});
mgr.scanSrcDepsAsync(srcDirs)
.then(_.bind(mgr.printDep, mgr));
});
gulp.task('lint', function() {
var i = 0;
return gulp.src(['*.js', 'lib/**/*.js']
.concat(packageLintableSrc(argv.p)))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'))
.pipe(require('through2').obj(function(file, en, next) {
gutil.log(++i + ' ' + file.path);
next(null, file);
}))
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'));
});
/**
* link src/ ** /package.json from node_modules folder
*/
gulp.task('link', function() {
return recipeManager.link();
});
gulp.task('compile', ['link', 'flatten-recipe'], function(cb) {
runSequence('compile:dev', cb);
});
gulp.task('compile:dev', function(cb) {
require('./lib/packageMgr/packageCompiler')(argv)
.then(() => cb());
});
gulp.task('watch', function() {
watchPackages(argv.p, argv);
});
/**
* TODO: bump dependencies version
*/
gulp.task('bump', function(cb) {
var srcDirs = [];
var recipes = [];
recipeManager.eachRecipeSrc(function(src, recipe) {
srcDirs.push(src);
recipes.push(recipe);
});
var stream = gulp.src(srcDirs)
.pipe(findPackageJson())
.pipe(through.obj(function(file, enc, next) {
file.contents = new Buffer(fs.readFileSync(file.path, 'utf8'));
next(null, file);
}, function(next) {
var self = this;
recipes.forEach(function(recipe) {
self.push(new File({
base: config().rootPath,
path: Path.resolve(recipe, 'package.json'),
contents: new Buffer(fs.readFileSync(Path.resolve(recipe, 'package.json'), 'utf8'))
}));
});
var packageJsonPath = Path.resolve(config().rootPath, 'package.json');
this.push(new File({
base: config().rootPath,
path: packageJsonPath,
contents: new Buffer(fs.readFileSync(packageJsonPath, 'utf8'))
}));
next();
})).pipe(through.obj(function(file, enc, next) {
file.base = config().rootPath;
gutil.log('bump: ' + file.path);
next(null, file);
}))
.pipe(bumpVersion())
.pipe(gulp.dest(config().rootPath));
stream.on('error', function(err) {
cb(err);
})
.on('end', function() {
runSequence('link', function(err) {
if (err) {
return cb(err);
}
cb();
});
});
});
gulp.task('publish', function() {
var srcDirs = [];
recipeManager.eachRecipeSrc(function(src, recipe) {
srcDirs.push(src);
});
return gulp.src(srcDirs)
.pipe(findPackageJson())
.on('error', gutil.log)
.pipe(vps(function(paths) {
var data = JSON.parse(fs.readFileSync(paths, 'utf8'));
gutil.log('publish ' + data.name + '@' + data.version);
return new Promise(function(resolve, reject) {
cli.exec('npm', 'publish', Path.dirname(paths), {silent: true},
function(code, output) {
resolve(code);
});
});
})).on('end', function() {
recipeManager.eachRecipeSrc(function(src, recipeDir) {
var data = JSON.parse(fs.readFileSync(Path.join(recipeDir, 'package.json'), 'utf8'));
gutil.log('publish ' + data.name + '@' + data.version);
cli.exec('npm', 'publish', recipeDir, {silent: true});
});
var data = JSON.parse(fs.readFileSync('package.json', 'utf8'));
gutil.log('publish ' + data.name + '@' + data.version);
cli.exec('npm', 'publish', process.cwd(), {silent: true});
});
});
gulp.task('unpublish', function() {
var srcDirs = [];
recipeManager.eachRecipeSrc(function(src, recipe) {
srcDirs.push(src);
});
return gulp.src(srcDirs)
.pipe(findPackageJson())
.on('error', gutil.log)
.pipe(vps(function(paths) {
var data = JSON.parse(fs.readFileSync(paths, 'utf8'));
gutil.log('unpublish ' + data.name + '@' + data.version);
return new Promise(function(resolve, reject) {
cli.exec('npm', 'unpublish', data.name + '@' + data.version, {silent: false},
function(code, output) {
resolve(code);
});
});
})).on('end', function() {
recipeManager.eachRecipeSrc(function(src, recipeDir) {
var data = JSON.parse(fs.readFileSync(Path.join(recipeDir, 'package.json'), 'utf8'));
gutil.log('unpublish ' + data.name + '@' + data.version);
cli.exec('npm', 'unpublish', data.name + '@' + data.version, {silent: false});
});
var data = JSON.parse(fs.readFileSync('package.json', 'utf8'));
gutil.log('unpublish ' + data.name + '@' + data.version);
cli.exec('npm', 'unpublish', data.name + '@' + data.version, {silent: false});
});
});
gulp.task('test', function() {
require('./lib/gulp/runTest')(argv);
});
function bumpVersion() {
var type = 'patch';
if (argv.v) {
if (!{major: 1, minor: 1, patch: 1, prerelease: 1}.hasOwnProperty(argv.v)) {
gutil.log(chalk.red('expecting bump type is one of "major|minor|patch|prerelease", but get: ' + argv.v));
throw new Error('Invalid -v parameter');
}
type = argv.v;
}
return bump({
type: type
});
}
uncaughtException();
function uncaughtException() {
process.removeAllListeners('uncaughtException');
process.on('uncaughtException', function(err) {
// handle the error safely
gutil.log(chalk.red('Uncaught exception: '), err, err.stack);
});
}