-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
351 lines (279 loc) · 8.97 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
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
"use strict";
const
del = require("del"),
exec = require("child_process").exec,
fs = require("fs"),
gulp = require("gulp"),
loadPlugins = require("gulp-load-plugins"),
mkdirp = require("mkdirp"),
path = require("path"),
PluginError = require("gulp-util").PluginError,
runSequence = require("run-sequence"),
through = require("through2").obj,
utils = require("./test/lib/utils.js");
const // jscs: ignore requireMultipleVarDecl
$ = loadPlugins(),
paths = {
lint: [
"gulpfile.js",
"lib/*.js",
"test/*.js",
"test/lib/*.js"
],
fixturesBase: "test/fixtures",
test: ["test/*.js"]
};
// Cleaning
gulp.task("clean", () => del("test/fixtures/**/{dest,misc}/*"));
// Linting
gulp.task("lint:eslint", () =>
gulp.src(paths.lint)
.pipe($.eslint({
rulePaths: ["node_modules/eslint-config-cappuccino/lib/rules"],
quiet: true
}))
.pipe($.eslint.format("node_modules/eslint-clang-formatter"))
.pipe($.eslint.failAfterError())
);
gulp.task("lint:jscs", () =>
gulp.src(paths.lint)
.pipe($.jscs())
.pipe($.jscs.reporter("jscs-clang-reporter"))
.pipe($.jscs.reporter("fail"))
);
gulp.task("lint", cb => runSequence("lint:eslint", "lint:jscs", cb));
// Fixtures
function compileFixture(options, file, encoding, cb)
{
let filePath = file.path;
console.log(path.basename(filePath));
options = utils.setCompilerOptions(options, filePath);
if (path.basename(filePath) === "stdin.j")
options.stdin = true;
const output = utils.compiledFixture(filePath, options);
let text = options.captureStdout ? output.stdout : output.code;
if (file.path.includes("/exceptions/"))
text = utils.convertIssuePathsToPosix(text);
file.contents = new Buffer(text);
if (options.sourceMap)
{
const parsed = path.parse(file.path);
fs.writeFileSync(path.join(path.dirname(parsed.dir), "dest", parsed.name + ".js.map"), output.map);
}
cb(null, file);
}
function cliCompileFixture(options, file, encoding, cb)
{
const result = utils.compiledCliFixture(file.path);
console.log(path.basename(file.path));
file.contents = new Buffer(result.output);
cb(null, file);
}
function generateFixtures(fixturesDir, renameSpec, options, filenamePattern)
{
filenamePattern = filenamePattern || "*{.js,.j,.txt}";
options = options || {};
options.ignoreWarnings = fixturesDir !== "exceptions";
/*
gulp.src/dest config is a bit tricky here because we want to put
the destination files in a sibling directory of the source.
*/
const
srcDir = path.join(paths.fixturesBase, fixturesDir, "src"),
compileFunc = fixturesDir.startsWith("cli") ? cliCompileFixture : compileFixture;
// Since we only need paths for the compiler, no need to read the file source.
return gulp.src("./" + filenamePattern, { cwd: srcDir, base: srcDir, read: false })
// Only generate fixtures whose source has changed
.pipe($.newer(makeNewerOptions(srcDir, renameSpec)))
// Compile with the given options, save the result in the vinyl file.content
.pipe(through(compileFunc.bind(null, options)))
// Rename the compiled file
.pipe($.rename(renameSpec))
// Save the compiled code into the sibling dest directory
.pipe(gulp.dest("../dest", { cwd: srcDir }));
}
/**
* Create options for gulp-newer that will return a path in sourceDir/../dest
*
* @param {String} sourceDir - A relative path from the project root to the fixture source directory,
* e.g. "test/fixtures/js-nodes/src".
* @param {String|function(Object)} renameSpec - Destination file rename spec.
* @returns {{map: (function())}} - Absolute path to destination file to compare against.
*/
function makeNewerOptions(sourceDir, renameSpec)
{
const destDir = path.join(path.dirname(sourceDir), "dest");
return {
// Because cwd and base are set to the directory of the fixture source,
// the relative path passed to the map function is actually just the filename.
map: filename =>
{
let extension = path.extname(filename);
if (typeof renameSpec === "function")
{
let spec = {
suffix: "",
extname: extension
};
renameSpec(spec);
extension = spec.extname;
}
else
extension = (renameSpec.suffix || "") + renameSpec.extname;
// Strip the source filename extension, then add the destination suffix + extension
const extraDir = path.dirname(filename);
filename = path.basename(filename, path.extname(filename)) + extension;
// Return an absolute path to the destination file to compare against
return path.join(process.cwd(), destDir, extraDir, filename);
}
};
}
gulp.task("generate-fixtures:cli:code", () =>
{
const renameSpec = pathSpec =>
{
pathSpec.extname = pathSpec.extname === ".txt" ? ".txt" : ".js";
return pathSpec;
};
return generateFixtures("cli/code", renameSpec);
});
gulp.task("generate-fixtures:cli:exceptions", () =>
{
const renameSpec = { extname: ".txt" };
return generateFixtures("cli/exceptions", renameSpec);
});
gulp.task("generate-fixtures:cli:misc", cb =>
{
mkdirp(path.join(paths.fixturesBase, "cli/misc"));
for (const test of ["list-formats", "list-optional-warnings"])
{
const result = utils.compiledMiscCliFixture(test);
if (result)
{
fs.writeFileSync(result.dest, result.output);
console.log(path.basename(result.dest));
}
}
cb();
});
gulp.task("generate-fixtures:cli", cb =>
{
runSequence(
"generate-fixtures:cli:code",
"generate-fixtures:cli:exceptions",
"generate-fixtures:cli:misc",
cb
);
});
gulp.task("generate-fixtures:js", () =>
{
const renameSpec = { extname: ".js" };
return generateFixtures("js-nodes", renameSpec);
});
gulp.task("generate-fixtures:objj", cb =>
{
runSequence(
"generate-fixtures:objj:nodes",
"generate-fixtures:objj:import",
cb
);
});
gulp.task("generate-fixtures:objj:nodes", () =>
{
const renameSpec = { extname: ".js" };
return generateFixtures("objj-nodes", renameSpec);
});
gulp.task("generate-fixtures:objj:import", () =>
{
const renameImport = function(spec)
{
spec.extname = ".js";
};
return generateFixtures("objj-nodes", renameImport, null, "import/**/import-*.j");
});
gulp.task("generate-fixtures:exceptions", () =>
{
const renameSpec = { extname: ".txt" };
return generateFixtures("exceptions", renameSpec, { captureStdout: true });
});
gulp.task("generate-fixtures:source-maps", () =>
{
const renameSpec = { extname: ".js" };
return generateFixtures("source-maps", renameSpec, { sourceMap: true });
});
gulp.task("generate-fixtures", cb =>
{
runSequence(
"generate-fixtures:cli",
"generate-fixtures:js",
"generate-fixtures:objj",
"generate-fixtures:exceptions",
cb
);
});
gulp.task("regenerate-fixtures", cb => runSequence("clean", "generate-fixtures", cb));
// Testing
function mochaTask(reporter)
{
return function()
{
return gulp.src(paths.test)
.pipe($.mocha({ reporter: reporter || "spec" }));
};
}
gulp.task("mocha", mochaTask("spec"));
gulp.task("mocha-dot", mochaTask("dot"));
let coverResults = "";
function istanbulExecArgs()
{
return "node node_modules/istanbul/lib/cli.js";
}
gulp.task("cover", cb =>
{
// Add --colors to force colorizing, normally chalk won't because
// it doesn't think it is writing to a terminal.
exec(
`${istanbulExecArgs()} cover --colors node_modules/mocha/bin/_mocha -- --reporter dot --colors`,
(error, stdout) =>
{
if (error)
{
error = new PluginError(
"istanbul cover",
{
message: error.message,
showStack: false
}
);
return cb(error);
}
coverResults = stdout;
return cb();
}
);
});
gulp.task("show-cover", ["cover"], cb =>
{
console.log(coverResults);
cb();
});
gulp.task("check-cover", ["cover"], cb =>
{
exec(`${istanbulExecArgs()} check-cover`, error =>
{
if (error)
{
error = new PluginError(
"istanbul check-cover",
{
message: "Coverage did not meet the thresholds",
showStack: false
}
);
return cb(error);
}
return cb();
});
});
gulp.task("test", cb => runSequence("lint", "check-cover", cb));
gulp.task("default", ["test"]);