-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulp-begin.js
More file actions
557 lines (535 loc) · 16.3 KB
/
gulp-begin.js
File metadata and controls
557 lines (535 loc) · 16.3 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
* # gulp-begin
* @module introduction
*/
/**
* # License
* [The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html)
*
* Copyright (c) 2016 Beg.in
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* @module license
*/
'use strict';
var cp = require('child_process');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var merge = require('merge2');
//var del = require('del');
var livereload = require('tiny-lr')();
var gp = require('gulp-load-plugins')({
rename: {
'gulp-cssnano': 'cssmin',
'gulp-ng-annotate': 'ngAnnotate',
'gulp-angular-templatecache': 'ngTemplates'
}
});
/**
* ## Using `gulp-begin`
*
* Here's an example
* ```javascript
* var begin = require('gulp-begin');
* var gulp = require('gulp');
*
* gulp.task('default', ['test']);
*
* begin(gulp, {
* test: {
* main: 'test/myTestRunner.js'
* },
* exclude: ['server'],
* warnExclusions: true
* });
* ```
*
* ### `begin(gulp, options)`
* Augment gulp using `gulp-begin` (via `begin`) and passing an `option` object.
*
* #### `options.exclude`
* An array of task names to exclude from `gulp`.
*
* #### `options.only`
* An array of task names that will _only_ be used. All other (default) tasks
* will be excluded. This field will be disregared if `options.exclude` is
* truthy.
*
* #### `options.warnExclusions`
* If this field is set to a truthy value, `gulp-begin` will warn if a `gulp`
* command is run with an excluded task.
*
* @module usage
*/
/**
* ## Build
* ### Requirements
* - [Postgresql](http://www.postgresql.org/)
*
* #### Setup
* ```bash
* $ npm run setup
* $ npm install
* ```
* This installs the following globally:
* - [Gulp](http://gulpjs.com/)
* - [Bower](http://bower.io/)
* - [Node Foreman](http://strongloop.github.io/node-foreman/)
* - [Node Inspector](https://github.com/node-inspector/node-inspector)
*
* Add a file called `.env` to the root of the project with the following contents:
* ```json
* {
* "node": {
* "env": "dev"
* }
* }
* ```
* You can now run the development server by running the following commands:
* ```bash
* $ npm start
* ```
*
* - You can now visit [http://localhost:8081/](http://localhost:8081/) to view changes live.
*
* #### Directories
* - `src/client` - clientside html, scripts, and styles
* - `src/server` - serverside scripts and sql queries
*
* #### Serverside runtime
* - Uses dependency injection from [Nodep](http://nodep.org)
*
* ### Running the test suite
* #### Single Run:
* ```bash
* $ gulp test
* ```
* #### Continuous testing when files are changed:
* ```bash
* $ gulp autotest
* ```
* ### Generating README.md
* ```bash
* $ gulp docs
* ```
* ### Generating CHANGELOG.md
* ```bash
* $ gulp changelog
* ```
* ### Notes
* - jshint is part of the test suite and should be kept clean
* - Commits should have high test coverage
* - Docs should be kept up to date
* - Additions should come with documentation
* - commit messages should follow [Angular conventional format](https://github.com/stevemao/conventional-changelog-angular/blob/master/convention.md)
* @module build
*/
module.exports = function(gulp, options) {
var log = function() {
var args = _.map(arguments, arg => gp.util.colors.green(arg));
args.unshift(gp.util.colors.green('[server]'));
gp.util.log.apply(gp.util, args);
};
options = options || {};
options = _.defaultsDeep(options, {
root: process.cwd(),
port: process.env.PORT,
server: {
cwd: 'src/server',
main: 'index.js',
watch: [
path.join(_.get(options, 'server.cwd', 'src/server'), '**/*.js'),
'*.js'
]
},
client: {
lib: 'bower_components',
cwd: 'src/client',
dest: 'public',
html: {
src: ['*.html']
},
scripts: {
cwd: 'scripts',
dest: 'app.min.js',
lib: [],
src: [
'*.js',
'*/**/*.js'
]
},
styles: {
cwd: 'styles',
dest: 'app.min.css',
include: {
lib: [],
src: ['styles']
},
src: ['styles.scss']
},
templates: {
cwd: 'views',
src: ['**/*.html']
},
images: {
cwd: 'images',
src: ['**/*.png', '**/*.jpg']
},
svgs: {
cwd: 'images',
src: ['**/*.svg']
}
},
test: {
main: 'spec.js',
watch: ['test/**/*.js']
}
});
var buildFiles = function(scope, type) {
var arr = [];
if(type === 'lib') {
arr.push(options.client.lib);
} else {
if(options.client.cwd) {
arr.push(options.client.cwd);
}
if(scope.cwd) {
arr.push(scope.cwd);
}
}
return scope[type].map(file =>
path.join.apply(path, arr.concat([file]))
);
};
var buildSrc = scope => buildFiles(scope, 'src');
var buildLib = scope => buildFiles(scope, 'lib');
var files = {
src: {
html: buildSrc(options.client.html),
templates: buildSrc(options.client.templates),
scripts: buildSrc(options.client.scripts),
styles: {
main: buildSrc(options.client.styles),
include: options.client.styles.include.src.map(file =>
path.join(options.client.cwd, file)
),
},
images: buildSrc(options.client.images),
svgs: buildSrc(options.client.svgs)
},
lib: {
scripts: buildLib(options.client.scripts),
styles: {
include: options.client.styles.include.lib.map(file =>
path.join(options.client.lib, file)
)
}
}
};
var name = task => options.prefix ? `${options.prefix}_${task}` : task;
/**
* ## Default Server
*
* `TODO`
* @module default-server
*/
var server = function() {
gulp.watch(files.src.html, [name('html')]);
gulp.watch(_.flatten([
files.src.scripts,
files.src.templates,
files.src.svgs,
files.lib.scripts
]), [name('scripts')]);
gulp.watch(_.flatten([
files.src.styles.main,
files.src.styles.include,
files.lib.styles.include
]), [name('styles')]);
gulp.watch(files.src.images, [name('images')]);
var reloadable = function(file, cb) {
gulp.watch(file, function(event) {
if(event.path.indexOf(file) === -1) {
return;
}
cb();
});
};
reloadable('gulpfile.js', function() {
log('gulpfile.js changed, reloading server...');
process.exit(0);
});
reloadable('package.json', function() {
log('package.json changed, installing packages...');
cp.execSync('npm install', {stdio: 'inherit'});
cp.execSync('npm prune', {stdio: 'inherit'});
process.exit(0);
});
reloadable('bower.json', function() {
log('bower.json changed, installing packages...');
cp.execSync('bower install', {stdio: 'inherit'});
cp.spawn('gulp' + (process.platform === 'win32' ? '.cmd' : ''), [name('build')], {stdio: 'inherit'}).on('close', function(code) {
process.exit(code);
});
});
gp.nodemon({
script: options.server.main,
watch: options.server.watch
}).on('restart', function() {
log('app restarted!');
}).on('crash', function() {
log('app crashed!');
}).on('quit', function() {
log('app exited!');
process.exit();
});
/*
process.on('SIGINT', function() {
nodemon.on('exit', function() {
log('app exited!');
process.exit();
});
});
*/
log('app started on port', options.port);
livereload.listen(35729);
gulp.watch(path.join(options.client.dest, '**/*'), function(event){
log('livereload initiated');
setTimeout(function() {
livereload.changed({
body: {
files: [path.relative('' + options.port, event.path)]
}
});
}, 1000);
});
};
/** ## Default Tasks
*
* ### `html`
*
* This task will minify HTML in `files/src/html` and place the minified
* version in the client destination directory.
*
* ### `jshint`
*
* This task runs the `jshint` linter on source files and reports the
* results in a stylish manner.
*
* ### `scripts`
* `TODO`
*
* ### `styles`
* `TODO`
*
* ### `images`
* Minifies images (PNGs and JPGs).
*
* ### `build`
* A general task that performs the following tasks
* - `html`
* - `styles`
* - `scripts`
* - `images`
*
* ### `server`
* `TODO`
*
* ### `demon`
* `TODO`
*
* ### `dev`
* `TODO`
*
* ### `test`
* `TODO`
*
* ### `autotest`
* `TODO`
*
* ### `docs`
* `TODO`
*
* ### `changelog`
* `TODO`
*
* @module default-tasks
*/
var defaultTasks = {
[name('html')]: [() => {
return gulp.src(files.src.html)
.pipe(gp.htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(options.client.dest));
}],
[name('jshint')]: [() => {
return gulp.src(_.flatten([
files.src.scripts,
options.server.watch,
options.test.watch
]))
.pipe(gp.jshint())
.pipe(gp.jshint.reporter(require('jshint-stylish')));
//.pipe(jshint.reporter('fail'));
}],
[name('scripts')]: [
[name('jshint')],
() => {
var src = merge(
gulp.src(files.lib.scripts)
.pipe(gp.concat('libs.js')),
gulp.src(files.src.templates)
.pipe(gp.htmlmin({collapseWhitespace: true}))
.pipe(gp.ngTemplates({
standalone: true
})),
gulp.src(files.src.svgs)
.pipe(gp.imagemin({
svgoPlugins: [{
cleanupIDs: false
}]
}))
.pipe(gp.ngTemplates({
standalone: true,
filename: 'svgs.js',
module: 'svgs'
})),
gulp.src(files.src.scripts)
.pipe(gp.sourcemaps.init())
.pipe(gp.babel())
.pipe(gp.ngAnnotate({
'single_quotes': true
}))
);
// src = src.pipe(gp.debug({title: 'scripts'}));
// src = src.pipe(gp.plumber({errorHandler: true}));
return src
.pipe(gp.concat(options.client.scripts.dest))
.pipe(gp.uglify())
.pipe(gp.sourcemaps.write('.'))
.pipe(gulp.dest(path.join(options.client.dest, options.client.scripts.cwd)));
}],
[name('styles')]: [() => {
var src = gulp.src(files.src.styles.main);
// src = src.pipe(gp.debug({title: 'styles'}));
return src
.pipe(gp.sourcemaps.init({loadMaps: true}))
.pipe(gp.plumber())
.pipe(gp.sass({
includePaths: _.flatten([
files.lib.styles.include,
files.src.styles.include
])
}))
.pipe(gp.autoprefixer())
.pipe(gp.concat(options.client.styles.dest))
.pipe(gp.cssmin())
.pipe(gp.sourcemaps.write('.'))
.pipe(gulp.dest(path.join(options.client.dest, options.client.styles.cwd)));
}],
[name('images')]: [() => {
return gulp.src(files.src.images)
.pipe(gp.imagemin())
.pipe(gulp.dest(path.join(options.client.dest, options.client.images.cwd)));
}],
[name('build')]: [[
name('html'),
name('styles'),
name('scripts'),
name('images')
]],
[name('server')]: [
[name('build')],
server
],
[name('demon')]: [
server
],
[name('dev')]: [() => {
cp.execSync('npm install', {stdio: 'inherit'});
var spawnChild = function() {
cp.spawn('gulp' + (process.platform === 'win32' ? '.cmd' : ''),
[name('demon')], {stdio: 'inherit'}).on('close', (code) => {
if(code === 0) {
spawnChild();
}
});
};
spawnChild();
}],
[name('test')]: [
[name('jshint')],
() => {
return gulp.src(options.server.watch)
.pipe(gp.istanbul())
.pipe(gp.istanbul.hookRequire())
.on('finish', function() {
gulp.src(options.test.main)
.pipe(gp.mocha())
.pipe(gp.istanbul.writeReports());
});
}
],
[name('autotest')]: [() => {
gulp.watch(_.flatten([options.server.watch, options.test.watch]), ['test']);
}],
[name('docs')]: [() => {
return gulp.src(_.flatten([['gulpfile.js'],
options.server.watch, options.test.watch, files.src.scripts]))
.pipe(gp.concat('README.md'))
.pipe(gp.jsdocToMarkdown({
template: fs.readFileSync('./docs.hbs', 'utf8')
})).pipe(gulp.dest('.'));
}],
[name('changelog')]: [() => {
return gulp.src('CHANGELOG.md')
.pipe(gp.conventionalChangelog({
preset: 'angular'
})).pipe(gulp.dest('.'));
}]
};
var excludedTasks;
if (options.exclude) {
excludedTasks = options.exclude;
} else if (options.only) {
var allDefaultTaskNames = _.keys(defaultTasks);
var onlyDefaultTaskNames = _.map(options.only, name);
excludedTasks = _.difference(allDefaultTaskNames, onlyDefaultTaskNames);
} else {
excludedTasks = [];
}
var task = function(taskName) {
var isExcluded = _.includes(excludedTasks, taskName)
|| _.includes(excludedTasks, name(taskName));
if (isExcluded) {
if (options.warnExclusions) {
gulp.task(taskName, () => {
console.warn(`Task <${taskName}> has been explicity excluded!`);
});
}
} else {
gulp.task.apply(gulp, arguments);
}
};
// now, register all of the default tasks
_.forEach(defaultTasks, (defaultTaskArgs, defaultTaskName) => {
var argArr = [defaultTaskName].concat(defaultTaskArgs);
task.apply(null, argArr);
});
};