forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.js
547 lines (437 loc) · 16.7 KB
/
Makefile.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* @fileoverview Build file
* @author nzakas
*/
/*global cat, cd, cp, echo, exec, exit, find, mkdir, mv, pwd, rm, target, test*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
require("shelljs/make");
var path = require("path"),
dateformat = require("dateformat"),
nodeCLI = require("shelljs-nodecli"),
os = require("os"),
semver = require("semver");
//------------------------------------------------------------------------------
// Settings
//------------------------------------------------------------------------------
/*
* A little bit fuzzy. My computer has a first CPU speed of 3093 and the perf test
* always completes in < 2000ms. However, Travis is less predictable due to
* multiple different VM types. So I'm fudging this for now in the hopes that it
* at least provides some sort of useful signal.
*/
var PERF_MULTIPLIER = 7.5e6;
//------------------------------------------------------------------------------
// Data
//------------------------------------------------------------------------------
var NODE = "node ", // intentional extra space
NODE_MODULES = "./node_modules/",
TEMP_DIR = "./tmp/",
BUILD_DIR = "./build/",
DOCS_DIR = "../eslint.github.io/docs",
// Utilities - intentional extra space at the end of each string
MOCHA = NODE_MODULES + "mocha/bin/_mocha ",
ESLINT = NODE + " bin/eslint.js ",
// Files
MAKEFILE = "./Makefile.js",
/*eslint-disable no-use-before-define */
JS_FILES = find("lib/").filter(fileType("js")).join(" "),
JSON_FILES = find("conf/").filter(fileType("json")).join(" ") + " .eslintrc",
TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" ");
/*eslint-enable no-use-before-define */
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Generates a function that matches files with a particular extension.
* @param {string} extension The file extension (i.e. "js")
* @returns {Function} The function to pass into a filter method.
* @private
*/
function fileType(extension) {
return function(filename) {
return filename.substring(filename.lastIndexOf(".") + 1) === extension;
};
}
/**
* Generates a static file that includes each rule by name rather than dynamically
* looking up based on directory. This is used for the browser version of ESLint.
* @param {string} basedir The directory in which to look for code.
* @returns {void}
*/
function generateRulesIndex(basedir) {
var output = "module.exports = function() {\n";
output += " var rules = Object.create(null);\n";
find(basedir + "rules/").filter(fileType("js")).forEach(function(filename) {
var basename = path.basename(filename, ".js");
output += " rules[\"" + basename + "\"] = require(\"./rules/" + basename + "\");\n";
});
output += "\n return rules;\n};";
output.to(basedir + "load-rules.js");
}
/**
* Creates a release version tag and pushes to origin.
* @param {string} type The type of release to do (patch, minor, major)
* @returns {void}
*/
function release(type) {
target.test();
exec("npm version " + type);
target.changelog();
exec("git push origin master --tags");
exec("npm publish");
target.gensite();
target.publishsite();
}
/**
* Executes a command and returns the output instead of printing it to stdout.
* @param {string} cmd The command string to execute.
* @returns {string} The result of the executed command.
*/
function execSilent(cmd) {
return exec(cmd, { silent: true }).output;
}
/**
* Splits a command result to separate lines.
* @param {string} result The command result string.
* @returns {array} The separated lines.
*/
function splitCommandResultToLines(result) {
return result.trim().split("\n");
}
/**
* Gets the first commit sha of the given file.
* @param {string} filePath The file path which should be checked.
* @returns {string} The commit sha.
*/
function getFirstCommitOfFile(filePath) {
var commits = execSilent("git rev-list HEAD -- " + filePath);
commits = splitCommandResultToLines(commits);
return commits[commits.length - 1].trim();
}
/**
* Gets the tag name where a given file was introduced first.
* @param {string} filePath The file path to check.
* @returns {string} The tag name.
*/
function getTagOfFirstOccurrence(filePath) {
var firstCommit = getFirstCommitOfFile(filePath),
tags = execSilent("git tag --contains " + firstCommit);
tags = splitCommandResultToLines(tags);
return tags.reduce(function(list, version) {
version = semver.valid(version.trim());
if (version) {
list.push(version);
}
return list;
}, []).sort(semver.compare)[0];
}
/**
* Gets the version number where a given file was introduced first.
* @param {string} filePath The file path to check.
* @returns {string} The version number.
*/
function getFirstVersionOfFile(filePath) {
return getTagOfFirstOccurrence(filePath);
}
//------------------------------------------------------------------------------
// Tasks
//------------------------------------------------------------------------------
target.all = function() {
target.test();
};
target.lint = function() {
var errors = 0,
lastReturn;
echo("Validating Makefile.js");
lastReturn = exec(ESLINT + MAKEFILE);
if (lastReturn.code !== 0) {
errors++;
}
echo("Validating JSON Files");
lastReturn = nodeCLI.exec("jsonlint", "-q -c", JSON_FILES);
if (lastReturn.code !== 0) {
errors++;
}
echo("Validating JavaScript files");
lastReturn = exec(ESLINT + JS_FILES);
if (lastReturn.code !== 0) {
errors++;
}
echo("Validating JavaScript test files");
lastReturn = exec(ESLINT + TEST_FILES);
if (lastReturn.code !== 0) {
errors++;
}
if (errors) {
exit(1);
}
};
target.test = function() {
target.lint();
target.checkRuleFiles();
var errors = 0,
lastReturn;
// exec(ISTANBUL + " cover " + MOCHA + "-- -c " + TEST_FILES);
lastReturn = nodeCLI.exec("istanbul", "cover", MOCHA, "-- -c", TEST_FILES);
if (lastReturn.code !== 0) {
errors++;
}
// exec(ISTANBUL + "check-coverage --statement 99 --branch 98 --function 99 --lines 99");
lastReturn = nodeCLI.exec("istanbul", "check-coverage", "--statement 99 --branch 98 --function 99 --lines 99");
if (lastReturn.code !== 0) {
errors++;
}
target.browserify();
lastReturn = nodeCLI.exec("mocha-phantomjs", "-R dot", "tests/tests.htm");
if (lastReturn.code !== 0) {
errors++;
}
if (errors) {
exit(1);
}
};
target.docs = function() {
echo("Generating documentation");
nodeCLI.exec("jsdoc", "-d jsdoc lib");
echo("Documentation has been output to /jsdoc");
};
target.gensite = function() {
echo("Generating eslint.org");
rm("-r", DOCS_DIR);
mkdir(DOCS_DIR);
cp("-rf", "docs/*", DOCS_DIR);
find(DOCS_DIR).forEach(function(filename) {
if (test("-f", filename)) {
var rulesUrl = "https://github.com/eslint/eslint/tree/master/lib/rules/";
var docsUrl = "https://github.com/eslint/eslint/tree/master/docs/rules/";
var text = cat(filename);
var baseName = path.basename(filename);
var sourceBaseName = path.basename(filename, ".md") + ".js";
text = "---\ntitle: ESLint\nlayout: doc\n---\n<!-- Note: No pull requests accepted for this file. See README.md in the root directory for details. -->\n" + text;
text = text.replace(/\.md\)/g, ".html)").replace("README.html", "index.html");
if (filename.indexOf("rules/") !== -1 && baseName !== "README.md") {
var version = getFirstVersionOfFile(path.join("lib/rules", sourceBaseName));
if (version) {
text += "\n## Version\n\n";
text += "This rule was introduced in ESLint " + version + ".\n";
}
text += "\n## Resources\n\n";
text += "* [Rule source](" + rulesUrl + sourceBaseName + ")\n";
text += "* [Documentation source](" + docsUrl + baseName + ")\n";
}
text.to(filename.replace("README.md", "index.md"));
}
});
};
target.publishsite = function() {
var currentDir = pwd();
cd(DOCS_DIR);
exec("git add -A .");
exec("git commit -m \"Autogenerated new docs at " + dateformat(new Date()) + "\"");
exec("git fetch origin && git rebase origin/master");
exec("git push origin master");
cd(currentDir);
};
target.browserify = function() {
// 1. create temp and build directory
if (!test("-d", TEMP_DIR)) {
mkdir(TEMP_DIR);
}
if (!test("-d", BUILD_DIR)) {
mkdir(BUILD_DIR);
}
// 2. copy files into temp directory
cp("-r", "lib/*", TEMP_DIR);
// 3. delete the load-rules.js file
rm(TEMP_DIR + "load-rules.js");
// 4. create new load-rule.js with hardcoded requires
generateRulesIndex(TEMP_DIR);
// 5. browserify the temp directory
nodeCLI.exec("browserify", TEMP_DIR + "eslint.js", "-o", BUILD_DIR + "eslint.js", "-s eslint");
// exec(BROWSERIFY + TEMP_DIR + "eslint.js -o " + BUILD_DIR + "eslint.js -s eslint");
// 6. remove temp directory
rm("-r", TEMP_DIR);
};
target.changelog = function() {
// get most recent two tags
var tags = exec("git tag", { silent: true }).output.trim().split(/\s/g),
rangeTags = tags.slice(tags.length - 2),
now = new Date(),
timestamp = dateformat(now, "mmmm d, yyyy");
// output header
(rangeTags[1] + " - " + timestamp + "\n").to("CHANGELOG.tmp");
// get log statements
var logs = exec("git log --pretty=format:\"* %s (%an)\" " + rangeTags.join(".."), {silent: true}).output.split(/\n/g);
logs = logs.filter(function(line) {
return line.indexOf("Merge pull request") === -1 && line.indexOf("Merge branch") === -1;
});
logs.push(""); // to create empty lines
logs.unshift("");
// output log statements
logs.join("\n").toEnd("CHANGELOG.tmp");
// switch-o change-o
cat("CHANGELOG.tmp", "CHANGELOG.md").to("CHANGELOG.md.tmp");
rm("CHANGELOG.tmp");
rm("CHANGELOG.md");
mv("CHANGELOG.md.tmp", "CHANGELOG.md");
// add into commit
exec("git add CHANGELOG.md");
exec("git commit --amend --no-edit");
};
target.checkRuleFiles = function() {
echo("Validating rules");
var eslintConf = require("./conf/eslint.json");
var environmentsConf = require("./conf/environments.json");
var confRules = {};
confRules["default"] = eslintConf.rules;
Object.keys(environmentsConf).forEach(function (env) {
confRules[env] = environmentsConf[env].rules;
});
var ruleFiles = find("lib/rules/").filter(fileType("js")),
rulesIndexText = cat("docs/rules/README.md"),
errors = 0;
ruleFiles.forEach(function(filename) {
var basename = path.basename(filename, ".js");
var docFilename = "docs/rules/" + basename + ".md";
var indexLine = new RegExp("\\* \\[" + basename + "\\].*").exec(rulesIndexText);
indexLine = indexLine ? indexLine[0] : "";
function isInConfig(env) {
return confRules[env] && confRules[env].hasOwnProperty(basename);
}
function isOffInConfig(env) {
var envRule = confRules[env][basename];
return envRule === 0 || (envRule && envRule[0] === 0);
}
function isOnInConfig(env) {
return !isOffInConfig(env);
}
function isOffInIndex(env) {
if (env === "default") {
return indexLine.indexOf("(off by default)") !== -1;
} else {
return indexLine.indexOf("(off by default in the " + env + " environment)") !== -1;
}
}
function isOnInIndex(env) {
if (env === "default") {
return indexLine.indexOf("(off by default)") === -1;
} else {
return indexLine.indexOf("(on by default in the " + env + " environment)") !== -1;
}
}
function hasIdInTitle(basename) {
var docText = cat(docFilename);
var idInTitleRegExp = new RegExp("^# (.*?) \\(" + basename + "\\)");
return idInTitleRegExp.test(docText);
}
// check for docs
if (!test("-f", docFilename)) {
console.error("Missing documentation for rule %s", basename);
errors++;
} else {
// check for entry in docs index
if (rulesIndexText.indexOf("(" + basename + ".md)") === -1) {
console.error("Missing link to documentation for rule %s in index", basename);
errors++;
}
// check for proper doc format
if (!hasIdInTitle(basename)) {
console.error("Missing id in the doc page's title of rule %s", basename);
errors++;
}
}
// check for default configuration
if (!isInConfig("default")) {
console.error("Missing default setting for %s in eslint.json", basename);
errors++;
}
// check that rule is not on in docs but off in default config
if (isOnInIndex("default") && isOffInConfig("default")) {
console.error("Missing '(off by default)' for rule %s in index", basename);
errors++;
}
// check that rule is not off in docs but on in default config
if (isOffInIndex("default") && isOnInConfig("default")) {
console.error("Rule documentation says that %s is off by default but it is enabled in eslint.json.", basename);
errors++;
}
// check rule config for each environment
Object.keys(confRules).forEach(function (env) {
if (env === "default") {
return;
}
// only check if rule has been explicitly set for environment
if (isInConfig(env)) {
// check that rule is not on in docs but off in environment config
if (isOnInIndex(env)) {
if (isOffInConfig(env)) {
console.error("Rule documentation says that %s is off in environment %s but it is enabled in eslint.json.", basename, env);
errors++;
}
// check that rule is not off in docs but on in default config
} else if (isOffInIndex(env)) {
if (isOnInConfig(env)) {
console.error("Rule documentation says that %s is on in environment %s but it is disabled in eslint.json.", basename, env);
errors++;
}
// rule has been overridden in environment but is not in docs
} else {
console.error("Missing '(%s by default in the %s environment)' for rule %s in index", isOnInConfig(env) ? "on" : "off", env, basename);
errors++;
}
}
});
// check for tests
if (!test("-f", "tests/lib/rules/" + basename + ".js")) {
console.error("Missing tests for rule %s", basename);
errors++;
}
});
if (errors) {
exit(1);
}
};
function time(cmd, runs, runNumber, results, cb) {
var start = process.hrtime();
exec(cmd, { silent: true }, function() {
var diff = process.hrtime(start),
actual = (diff[0] * 1e3 + diff[1] / 1e6); // ms
results.push(actual);
echo("Performance Run #" + runNumber + ": %dms", actual);
if (runs > 1) {
time(cmd, runs - 1, runNumber + 1, results, cb);
} else {
cb(results);
}
});
}
target.perf = function() {
var cpuSpeed = os.cpus()[0].speed,
max = PERF_MULTIPLIER / cpuSpeed,
cmd = ESLINT + "./tests/performance/jshint.js";
echo("CPU Speed is %d with multiplier %d", cpuSpeed, PERF_MULTIPLIER);
time(cmd, 5, 1, [], function(results) {
results.sort(function(a, b) {
return a - b;
});
var median = results[~~(results.length / 2)];
if (median > max) {
echo("Performance budget exceeded: %dms (limit: %dms)", median, max);
exit(1);
} else {
echo("Performance budget ok: %dms (limit: %dms)", median, max);
}
});
};
target.patch = function() {
release("patch");
};
target.minor = function() {
release("minor");
};
target.major = function() {
release("major");
};