UNPKG

2.33 kBJavaScriptView Raw
1"use strict";
2
3const 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
16let jshintConfig = {};
17
18require.extensions['.json'](jshintConfig, './.jshintrc');
19
20let jshintServerConfig = jshintConfig.exports;
21let jshintClientConfig = clone(jshintServerConfig);
22
23jshintClientConfig.predef = jshintClientConfig.predef.client;
24jshintServerConfig.predef = jshintServerConfig.predef.server;
25
26let packageJson = require('./package');
27
28gulp.task('default', ['build-and-test']);
29
30const reserved = ['default', 'test'];
31
32const srcPath = 'src/**/*.js';
33
34jshint.client = jshint.bind(null, jshintClientConfig);
35jshint.server = jshint.bind(null, jshintServerConfig);
36
37function 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
46gulp.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
56gulp.task('build-and-test', function (cb) {
57 runSequence(['build', 'test'], cb);
58});
59
60gulp.task('watch', ['default'], function () {
61 return gulp.watch(srcPath, ['default']);
62});
63
64gulp.task('build', ['build:app']);
65
66gulp.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
72gulp.task('jshint', ['jshint:app']);
73
74
75gulp.task('jshint:app', function () {
76 return gulp.src('./src/**/*.js')
77 .pipe(jshint.server())
78 .pipe(jshint.reporter('jshint-stylish'));
79});
80
81gulp.task('test', function () {
82 return gulp.src('test/test.js', { read: false })
83 .pipe(mocha({ reporter: 'nyan' }));
84});