1 | "use strict";
|
2 |
|
3 | const gulp = require('gulp'),
|
4 | path = require('path'),
|
5 | fs = require('fs'),
|
6 | _ = require('lodash'),
|
7 | gutil = require('gulp-util'),
|
8 | runSequence = require('run-sequence'),
|
9 | mocha = require('gulp-mocha'),
|
10 | babel = require('gulp-babel'),
|
11 | jshint = require('gulp-jshint'),
|
12 | spawnSync = require('child_process').spawnSync,
|
13 | clone = require('clone'),
|
14 | map = require('async').map;
|
15 |
|
16 | let jshintConfig = {};
|
17 |
|
18 | require.extensions['.json'](jshintConfig, './.jshintrc');
|
19 |
|
20 | let jshintServerConfig = jshintConfig.exports;
|
21 | let jshintClientConfig = clone(jshintServerConfig);
|
22 |
|
23 | jshintClientConfig.predef = jshintClientConfig.predef.client;
|
24 | jshintServerConfig.predef = jshintServerConfig.predef.server;
|
25 |
|
26 | let packageJson = require('./package');
|
27 |
|
28 | gulp.task('default', ['build-and-test']);
|
29 |
|
30 | const reserved = ['default', 'test'];
|
31 |
|
32 | const srcPath = 'src/**/*.js';
|
33 |
|
34 | jshint.client = jshint.bind(null, jshintClientConfig);
|
35 | jshint.server = jshint.bind(null, jshintServerConfig);
|
36 |
|
37 | function buildExtTask(testFramework) {
|
38 | let task = `node ${path.join('node_modules', testFramework, 'bin', testFramework)}`;
|
39 | if (arguments.length > 2) {
|
40 | task += ' ';
|
41 | task += [].slice.call(arguments, 2).join(' ');
|
42 | }
|
43 | return task;
|
44 | }
|
45 |
|
46 | gulp.task('build:tasks', function () {
|
47 | packageJson.scripts = {};
|
48 | _.forOwn(gulp.tasks, function (value, key, obj) {
|
49 | if (~reserved.indexOf(key)) return;
|
50 | packageJson.scripts[key] = `node ${path.join('node_modules', 'gulp', 'bin', 'gulp')} ${key}`;
|
51 | });
|
52 | packageJson.scripts.test = buildExtTask('mocha');
|
53 | fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 1));
|
54 | });
|
55 |
|
56 | gulp.task('build-and-test', function (cb) {
|
57 | runSequence(['build', 'test'], cb);
|
58 | });
|
59 |
|
60 | gulp.task('watch', ['default'], function () {
|
61 | return gulp.watch(srcPath, ['default']);
|
62 | });
|
63 |
|
64 | gulp.task('build', ['build:app']);
|
65 |
|
66 | gulp.task('build:app', function () {
|
67 | gulp.src('src/**/*.js')
|
68 | .pipe(babel({ presets: ['es2015'], plugins: ["transform-object-rest-spread"] }))
|
69 | .pipe(gulp.dest('dist'));
|
70 | });
|
71 |
|
72 | gulp.task('jshint', ['jshint:app']);
|
73 |
|
74 |
|
75 | gulp.task('jshint:app', function () {
|
76 | return gulp.src('./src/**/*.js')
|
77 | .pipe(jshint.server())
|
78 | .pipe(jshint.reporter('jshint-stylish'));
|
79 | });
|
80 |
|
81 | gulp.task('test', function () {
|
82 | return gulp.src('test/test.js', { read: false })
|
83 | .pipe(mocha({ reporter: 'nyan' }));
|
84 | });
|