-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
213 lines (175 loc) · 5.54 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
"use strict";
const
del = require("del"),
exec = require("child_process").exec,
fs = require("fs"),
gulp = require("gulp"),
loadPlugins = require("gulp-load-plugins"),
parse = require("./lib/parse.js").parseFileToString,
path = require("path"),
PluginError = require("gulp-util").PluginError,
run = require("./test/lib/test-utils.js").run,
runSequence = require("run-sequence"),
through = require("through2").obj;
const // jscs: ignore requireMultipleVarDecl
$ = loadPlugins(),
basePaths = [
"gulpfile.js",
"lib/*.js",
"test/**/*.js",
"!test/coverage/**/*.js",
"!test/fixtures/**/*.js"
],
paths = {
lint: [...basePaths, "!test/acorn/*"],
fixtures: ["test/fixtures/**/*.j"],
test: ["test/*.js"],
coverage: ["lib/**/*.js"]
};
// Cleaning
gulp.task("clean", () => del("test/fixtures/**/*.{json,txt}"));
// Linting
gulp.task("lint:eslint", () =>
gulp.src(paths.lint)
.pipe($.eslint({ rulePaths: ["node_modules/eslint-config-cappuccino/lib/rules"] }))
.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
const specialFixtureOptions = new Map([
["type-locations.j", { locations: true }]
]);
gulp.task("generate-fixtures", () =>
{
const fixturesPath = "test/fixtures";
function parseFixture(file, encoding, cb)
{
const specialOptions = specialFixtureOptions.get(path.basename(file.path));
let options = Object.assign({}, specialOptions);
console.log(file.relative);
file.contents = new Buffer(parse(file.path, options));
cb(null, file);
}
const cliFixtures = [
[["--no-color"], "hash-bang.js", "hash-bang.txt"],
[["--allow-hash-bang"], "hash-bang.js"],
[[], "compact.js", "pretty.json"],
[["--compact"], "compact.js"],
[["--ecma", "5"], "ecma.js", "ecma.txt"],
[[], "ecma.js"],
[["--locations"], "compact.js", "locations.json"],
[["--no-objj"], "objj.j", "objj.txt"],
[["--no-objj"], "no-objj.js"],
[["--strict-semicolons"], "strict-semicolons.js", "strict-semicolons.txt"],
[["--module", "--ecma", "5"], "module.js", "module.txt"]
];
for (let fixture of cliFixtures)
{
let args = fixture[0].slice(),
source = fixture[1],
dest = fixture[2],
baseDir = fixture[3] || "cli";
if (!dest)
dest = path.basename(source, path.extname(source)) + ".json";
let basePath = path.join(fixturesPath, baseDir),
sourcePath = path.join(basePath, source),
destPath = path.join(basePath, dest),
sourceStat,
destStat;
try
{
sourceStat = fs.statSync(sourcePath);
destStat = fs.statSync(destPath);
// Don't bother if the source file has not changed since
// the last write of the dest file.
if (sourceStat.mtime <= destStat.mtime)
continue;
}
catch (ex)
{
// Don't do anything
}
args.push(sourcePath);
const output = run(args, { parseOptions: { slice: 0 }}).output;
fs.writeFileSync(destPath, output);
console.log(`${baseDir}/${dest}`);
}
return gulp.src(paths.fixtures)
.pipe($.changed("test/fixtures", { extension: ".json" }))
.pipe(through(parseFixture))
.pipe($.rename({ extname: ".json" }))
.pipe(gulp.dest("test/fixtures"));
});
gulp.task("regenerate-fixtures", cb => runSequence("clean", "generate-fixtures", cb));
// Tests
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"]);