UNPKG

4.96 kBJavaScriptView Raw
1var gulp = require('gulp'),
2 ts = require('gulp-typescript'),
3 mocha = require('gulp-mocha'),
4 runSequence = require('run-sequence'),
5 typedoc = require("gulp-typedoc"),
6 tslint = require("gulp-tslint"),
7 stylish = require('tslint-stylish'),
8 istanbul = require('gulp-istanbul'),
9 plumber = require('gulp-plumber'),
10 coveralls = require('gulp-coveralls');
11
12process.setMaxListeners(0);
13
14// gulp.task("doc", function() {
15// return gulp
16// .src([
17// "./src/**/*.ts"
18// ])
19// .pipe(typedoc({
20// module: "commonjs",
21// target: "es5",
22// out: "./docs/",
23// name: "Antfarm",
24// ignoreCompilerErrors: false,
25// includeDeclarations: false,
26// mode: "file"
27// }));
28// });
29
30gulp.task("tslint", function() {
31 gulp.src([
32 "!./src/**/*.d.ts",
33 "./src/**/*.ts"
34 ])
35 .pipe(tslint({
36 formatter: "verbose"
37 }))
38 .pipe(tslint.report(stylish, {
39 emitError: false,
40 sort: true,
41 bell: true
42 }));
43});
44
45var tsProject = ts.createProject(
46 'tsconfig.json',
47 {
48 declaration: true,
49 noExternalResolve: true,
50 module:'commonjs',
51 suppressImplicitAnyIndexErrors: false,
52 noEmitOnError: false
53 },
54 ts.reporter.defaultReporter()
55);
56
57/**
58 * handleMochaError
59 * ================
60 * Simple error handling specifically for mocha. Reports the latest error
61 * encountered, then uses `process.exit(1)` to force an exit from the gulp
62 * task and communicate that an error occurred (e.g. to Travis CI).
63 */
64var mochaErr;
65var handleMochaError = function (err) {
66 console.log('Mocha encountered an error, exiting with status 1');
67 console.log('Error:', err.message);
68 process.exit(1);
69};
70
71function handleError(err) {
72 console.log(err.toString());
73 this.emit('end');
74}
75
76gulp.task('test', function() {
77 return gulp.src('test/**/*.spec.js', {read: false})
78 // gulp-mocha needs filepaths so you can't have any plugins before it
79 .pipe(mocha({reporter: 'nyan'})
80 .on("error", handleError));
81});
82
83gulp.task('test-travis', function (cb) {
84 var mochaErr;
85 // Track src files that should be covered
86 gulp.src('test/**/*.spec.js')
87 .pipe(istanbul({ includeUntested: true })) // Covering files
88 .pipe(istanbul.hookRequire()) // Force `require` to return covered files
89 .on('finish', function() {
90 return gulp.src('test/**/*.spec.js', {read: false})
91 // gulp-mocha needs filepaths so you can't have any plugins before it
92
93 .pipe(mocha({reporter: 'spec'}))
94 .pipe(istanbul.writeReports())
95 /**
96 * Keep track of latest error on Mocha. Because a failed test counts
97 * as an error, the process should not be exited until end of tests.
98 */
99 .on('error', function(err) {
100 /**
101 * This intermediate log is useful for when mocha crashes (as opposed
102 * to a test failing), especially necessary for Travis CI reporting.
103 * Without these logs, Travis CI will not report anything meaningful
104 * if mocha crashes. Can be commented out if desired.
105 */
106 console.error('ERROR:', err.message);
107 console.error('Stack:', err.stack);
108 mochaErr = err;
109 })
110
111 /**
112 * The methods below are a hack to get gulp to exit after mocha tests
113 * finish. Without them, `gulp mocha` doesn't exit and Travis
114 * never finishes running the tests.
115 */
116 .on('end', function () {
117 if (mochaErr) return handleMochaError(mochaErr);
118 // Force mocha to exit, because gulp-mocha is stupid.
119 process.exit();
120 })
121
122 });
123});
124
125gulp.task('coveralls-travis', function () {
126 // if (!process.env.CI) return;
127 return gulp.src('./coverage/**/lcov.info')
128 .pipe(coveralls());
129});
130
131gulp.task('build', function() {
132 var tsResult = gulp.src([
133 './src/index.ts',
134 './src/**/*.ts',
135
136 './devtypes/**/*.ts',
137 ])
138 .pipe(ts(tsProject));
139 tsResult.dts.pipe(gulp.dest('./'));
140 return tsResult.js.pipe(gulp.dest('./'));
141});
142
143gulp.task('build-test', function(callback) {
144 runSequence(
145 'build',
146 'test',
147 'tslint',
148 // 'typedoc',
149 callback);
150});
151
152gulp.task('travis', function(callback) {
153 runSequence(
154 'build',
155 'test-travis',
156 callback);
157});
158
159gulp.task('watch', function(){
160 gulp.watch('src/**/*.ts', ["build-test"]);
161 gulp.watch('test/**/*.js', ["test"]);
162});
163
164
165gulp.task('default', ['build-test', 'watch']);