-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
224 lines (176 loc) · 5.61 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
const path = require("path");
// Allow use of TS files.
require('ts-node').register({project: path.join(__dirname, "tsconfig.json")});
const fs = require("fs");
const gulp = require("gulp");
const stripJsonComments = require("strip-json-comments");
const del = require("del");
const _ = require("lodash");
const spawn = require("./devLib/spawn").spawn;
const Deferred = require("./devLib/deferred").Deferred;
////////////////////////////////////////////////////////////////////////////////
// Default
////////////////////////////////////////////////////////////////////////////////
gulp.task("default", () => {
const usage = [
"Gulp tasks",
" clean - Delete built and temporary files",
" tslint - Run TSLint on source files",
" ut - Run unit tests",
" build - Run TSLint, unit tests, and compile TypeScript"
];
console.log(usage.join("\n"));
});
////////////////////////////////////////////////////////////////////////////////
// Clean
////////////////////////////////////////////////////////////////////////////////
gulp.task("clean", () => {
return clean();
});
function clean() {
return del([
"tmp/**",
"dist/**"
]);
}
////////////////////////////////////////////////////////////////////////////////
// TSLint
////////////////////////////////////////////////////////////////////////////////
gulp.task("tslint", function ()
{
"use strict";
return runTslint(true);
});
function runTslint(emitError)
{
"use strict";
let tslintArgs = [
"--project", "./tsconfig.json",
"--format", "stylish"
];
// Add the globs defining source files to the list of arguments.
tslintArgs = tslintArgs.concat(getSrcGlobs(true));
return spawn("./node_modules/.bin/tslint", tslintArgs, __dirname,
undefined, undefined, process.stdout, process.stderr)
.closePromise
.catch((err) => {
// If we're supposed to emit an error, then go ahead and rethrow it.
// Otherwise, just eat it.
if (emitError) {
throw err;
}
});
}
////////////////////////////////////////////////////////////////////////////////
// Unit Tests
////////////////////////////////////////////////////////////////////////////////
gulp.task("ut", () => {
return runUnitTests();
});
function runUnitTests() {
const Jasmine = require("jasmine");
const runJasmine = require("./devLib/jasmineHelpers").runJasmine;
const jasmine = new Jasmine({});
jasmine.loadConfig(
{
"spec_dir": "src",
"spec_files": [
"**/*.spec.ts"
],
"helpers": [
],
"stopSpecOnExpectationFailure": false,
"random": false
}
);
return runJasmine(jasmine);
}
////////////////////////////////////////////////////////////////////////////////
// Build
////////////////////////////////////////////////////////////////////////////////
gulp.task("build", () => {
let errorsEncountered = false;
return clean()
.then(() => {
return runTslint(true);
})
.catch(() => {
errorsEncountered = true;
})
.then(() => {
return runUnitTests();
})
.catch(() => {
errorsEncountered = true;
})
.then(() => {
return compileTypeScript();
})
.catch(() => {
errorsEncountered = true;
})
.then(() => {
if (errorsEncountered) {
throw "Errors encountered.";
}
});
});
function compileTypeScript() {
const ts = require("gulp-typescript");
const sourcemaps = require("gulp-sourcemaps");
// The gulp-typescript package interacts correctly with gulp if you
// return this outer steam from your task function. I, however, prefer
// to use promises so that build steps can be composed in a more modular
// fashion.
const tsResultDfd = new Deferred();
const jsDfd = new Deferred();
const dtsDfd = new Deferred();
const outDir = path.join(__dirname, "dist");
let numErrors = 0;
const tsResults = gulp.src(getSrcGlobs(false))
.pipe(sourcemaps.init())
.pipe(ts(getTsConfig(), ts.reporter.longReporter()))
.on("error", () => {
numErrors++;
})
.on("finish", () => {
if (numErrors > 0) {
tsResultDfd.reject(new Error(`TypeScript transpilation failed with ${numErrors} errors.`));
} else {
tsResultDfd.resolve();
}
});
tsResults.js
.pipe(sourcemaps.write())
.pipe(gulp.dest(outDir))
.on("finish", () => {
jsDfd.resolve();
});
tsResults.dts
.pipe(gulp.dest(outDir))
.on("finish", () => {
dtsDfd.resolve();
});
return Promise.all([tsResultDfd.promise, jsDfd.promise, dtsDfd.promise]);
}
////////////////////////////////////////////////////////////////////////////////
// Project Management
////////////////////////////////////////////////////////////////////////////////
function getSrcGlobs(includeSpecs) {
"use strict";
const srcGlobs = ["src/**/*.ts"];
if (!includeSpecs) {
srcGlobs.push("!src/**/*.spec.ts");
}
return srcGlobs;
}
function getTsConfig(tscConfigOverrides) {
"use strict";
const tsConfigFile = path.join(__dirname, "tsconfig.json");
const tsConfigJsonText = fs.readFileSync(tsConfigFile, "utf8");
const compilerOptions = JSON.parse(stripJsonComments(tsConfigJsonText)).compilerOptions;
// Apply any overrides provided by the caller.
_.assign(compilerOptions, tscConfigOverrides);
compilerOptions.typescript = require("typescript");
return compilerOptions;
}