forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHerebyfile.mjs
587 lines (506 loc) · 16.2 KB
/
Herebyfile.mjs
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// @ts-check
import chokidar from "chokidar";
import { $ as _$ } from "execa";
import { glob } from "glob";
import { task } from "hereby";
import assert from "node:assert";
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import { parseArgs } from "node:util";
import pc from "picocolors";
import which from "which";
const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = path.dirname(__filename);
const isCI = !!process.env.CI;
const $pipe = _$({ verbose: "short" });
const $ = _$({ verbose: "short", stdio: "inherit" });
const { values: options } = parseArgs({
args: process.argv.slice(2),
options: {
race: { type: "boolean" },
tests: { type: "string", short: "t" },
fix: { type: "boolean" },
noembed: { type: "boolean" },
debug: { type: "boolean" },
concurrentTestPrograms: { type: "boolean" },
},
strict: false,
allowPositionals: true,
allowNegative: true,
noembed: false,
debug: false,
concurrentTestPrograms: false,
});
const defaultGoBuildTags = [
...(options.noembed ? ["noembed"] : []),
];
/**
* @param {...string} extra
* @returns {string[]}
*/
function goBuildTags(...extra) {
const tags = new Set(defaultGoBuildTags.concat(extra));
return tags.size ? [`-tags=${[...tags].join(",")}`] : [];
}
const goBuildFlags = [
...(options.race ? ["-race"] : []),
// https://github.com/go-delve/delve/blob/62cd2d423c6a85991e49d6a70cc5cb3e97d6ceef/Documentation/usage/dlv_exec.md?plain=1#L12
...(options.debug ? ["-gcflags=all=-N -l"] : []),
];
/**
* @type {<T>(fn: () => T) => (() => T)}
*/
function memoize(fn) {
let value;
return () => {
if (fn !== undefined) {
value = fn();
fn = /** @type {any} */ (undefined);
}
return value;
};
}
const typeScriptSubmodulePath = path.join(__dirname, "_submodules", "TypeScript");
function assertTypeScriptCloned() {
try {
const stat = fs.statSync(path.join(typeScriptSubmodulePath, "package.json"));
if (stat.isFile()) {
return;
}
}
catch {}
throw new Error("_submodules/TypeScript does not exist; try running `git submodule update --init --recursive`");
}
const tools = new Map([
["gotest.tools/gotestsum", "latest"],
["mvdan.cc/gofumpt", "v0.7.0"],
]);
/**
* @param {string} tool
*/
function isInstalled(tool) {
return !!which.sync(tool, { nothrow: true });
}
const libsDir = "./internal/bundled/libs";
const libsRegexp = /(?:^|[\\/])internal[\\/]bundled[\\/]libs[\\/]/;
async function generateLibs() {
await fs.promises.mkdir("./built/local", { recursive: true });
const libs = await fs.promises.readdir(libsDir);
await Promise.all(libs.map(async lib => {
fs.promises.copyFile(`${libsDir}/${lib}`, `./built/local/${lib}`);
}));
}
export const lib = task({
name: "lib",
run: generateLibs,
});
/**
* @param {string} packagePath
* @param {AbortSignal} [abortSignal]
*/
function buildExecutableToBuilt(packagePath, abortSignal) {
return $({ cancelSignal: abortSignal })`go build ${goBuildFlags} ${goBuildTags("noembed")} -o ./built/local/ ${packagePath}`;
}
export const tsgoBuild = task({
name: "tsgo:build",
run: async () => {
await buildExecutableToBuilt("./cmd/tsgo");
},
});
export const tsgo = task({
name: "tsgo",
dependencies: [lib, tsgoBuild],
});
export const local = task({
name: "local",
dependencies: [tsgo],
});
export const build = task({
name: "build",
dependencies: [local],
});
export const buildWatch = task({
name: "build:watch",
run: async () => {
await watchDebounced("build:watch", async (paths, abortSignal) => {
let libsChanged = false;
let goChanged = false;
if (paths) {
for (const p of paths) {
if (libsRegexp.test(p)) {
libsChanged = true;
}
else if (p.endsWith(".go")) {
goChanged = true;
}
if (libsChanged && goChanged) {
break;
}
}
}
else {
libsChanged = true;
goChanged = true;
}
if (libsChanged) {
console.log("Generating libs...");
await generateLibs();
}
if (goChanged) {
console.log("Building tsgo...");
await buildExecutableToBuilt("./cmd/tsgo", abortSignal);
}
}, {
paths: ["cmd", "internal"],
ignored: path => /[\\/]testdata[\\/]/.test(path),
});
},
});
export const cleanBuilt = task({
name: "clean:built",
hiddenFromTaskList: true,
run: () => fs.promises.rm("built", { recursive: true, force: true }),
});
export const generate = task({
name: "generate",
run: async () => {
assertTypeScriptCloned();
await $`go generate ./...`;
},
});
const goTestFlags = [
...goBuildFlags,
...goBuildTags(),
...(options.tests ? [`-run=${options.tests}`] : []),
];
const goTestEnv = {
...(options.concurrentTestPrograms ? { TS_TEST_PROGRAM_SINGLE_THREADED: "false" } : {}),
};
const $test = $({ env: goTestEnv });
const gotestsum = memoize(() => {
const args = isInstalled("gotestsum") ? ["gotestsum", "--format-hide-empty-pkg", "--"] : ["go", "test"];
return args.concat(goTestFlags);
});
const goTest = memoize(() => {
return ["go", "test"].concat(goTestFlags);
});
async function runTests() {
await $test`${gotestsum()} ./... ${isCI ? ["--timeout=45m"] : []}`;
}
export const test = task({
name: "test",
run: runTests,
});
async function runTestBenchmarks() {
// Run the benchmarks once to ensure they compile and run without errors.
await $test`${goTest()} -run=- -bench=. -benchtime=1x ./...`;
}
export const testBenchmarks = task({
name: "test:benchmarks",
run: runTestBenchmarks,
});
async function runTestTools() {
await $test({ cwd: path.join(__dirname, "_tools") })`${gotestsum()} ./...`;
}
export const testTools = task({
name: "test:tools",
run: runTestTools,
});
export const testAll = task({
name: "test:all",
run: async () => {
// Prevent interleaving by running these directly instead of in parallel.
await runTests();
await runTestBenchmarks();
await runTestTools();
},
});
const customLinterPath = "./_tools/custom-gcl";
const customLinterHashPath = customLinterPath + ".hash";
const golangciLintVersion = memoize(() => {
const golangciLintYml = fs.readFileSync(".custom-gcl.yml", "utf8");
const pattern = /^version:\s*(v\d+\.\d+\.\d+).*$/m;
const match = pattern.exec(golangciLintYml);
if (!match) {
throw new Error("Expected version in .custom-gcl.yml");
}
return match[1];
});
const customlintHash = memoize(() => {
const files = glob.sync([
"./_tools/go.mod",
"./_tools/customlint/**/*",
"./.custom-gcl.yml",
], {
ignore: "**/testdata/**",
nodir: true,
absolute: true,
});
files.sort();
const hash = crypto.createHash("sha256");
for (const file of files) {
hash.update(file);
hash.update(fs.readFileSync(file));
}
return hash.digest("hex") + "\n";
});
const buildCustomLinter = memoize(async () => {
const hash = customlintHash();
if (
isInstalled(customLinterPath)
&& fs.existsSync(customLinterHashPath)
&& fs.readFileSync(customLinterHashPath, "utf8") === hash
) {
return;
}
await $`go run github.com/golangci/golangci-lint/cmd/golangci-lint@${golangciLintVersion()} custom`;
await $`${customLinterPath} cache clean`;
fs.writeFileSync(customLinterHashPath, hash);
});
export const lint = task({
name: "lint",
run: async () => {
await buildCustomLinter();
const lintArgs = ["run", "--sort-results", "--show-stats"];
if (isCI) {
lintArgs.push("--timeout=5m");
}
if (defaultGoBuildTags.length) {
lintArgs.push("--build-tags", defaultGoBuildTags.join(","));
}
if (options.fix) {
lintArgs.push("--fix");
}
const resolvedCustomLinterPath = path.resolve(customLinterPath);
await $`${resolvedCustomLinterPath} ${lintArgs}`;
console.log("Linting _tools");
await $({ cwd: "./_tools" })`${resolvedCustomLinterPath} ${lintArgs}`;
},
});
export const installTools = task({
name: "install-tools",
run: async () => {
await Promise.all([
...[...tools].map(([tool, version]) => $`go install ${tool}${version ? `@${version}` : ""}`),
buildCustomLinter(),
]);
},
});
export const format = task({
name: "format",
run: async () => {
await $`dprint fmt`;
},
});
export const checkFormat = task({
name: "check:format",
run: async () => {
await $`dprint check`;
},
});
export const postinstall = task({
name: "postinstall",
hiddenFromTaskList: true,
run: () => {
// Ensure the go command doesn't waste time looking into node_modules.
// Remove once https://github.com/golang/go/issues/42965 is fixed.
fs.writeFileSync(path.join(__dirname, "node_modules", "go.mod"), `module example.org/ignoreme\n`);
},
});
/**
* @param {string} localBaseline Path to the local copy of the baselines
* @param {string} refBaseline Path to the reference copy of the baselines
*/
function baselineAcceptTask(localBaseline, refBaseline) {
/**
* @param {string} p
*/
function localPathToRefPath(p) {
const relative = path.relative(localBaseline, p);
return path.join(refBaseline, relative);
}
return async () => {
const toCopy = await glob(`${localBaseline}/**`, { nodir: true, ignore: `${localBaseline}/**/*.delete` });
for (const p of toCopy) {
const out = localPathToRefPath(p);
await fs.promises.mkdir(path.dirname(out), { recursive: true });
await fs.promises.copyFile(p, out);
}
const toDelete = await glob(`${localBaseline}/**/*.delete`, { nodir: true });
for (const p of toDelete) {
const out = localPathToRefPath(p).replace(/\.delete$/, "");
await rimraf(out);
await rimraf(p); // also delete the .delete file so that it no longer shows up in a diff tool.
}
};
}
export const baselineAccept = task({
name: "baseline-accept",
description: "Makes the most recent test results the new baseline, overwriting the old baseline",
run: baselineAcceptTask("testdata/baselines/local/", "testdata/baselines/reference/"),
});
/**
* @param {fs.PathLike} p
*/
function rimraf(p) {
// The rimraf package uses maxRetries=10 on Windows, but Node's fs.rm does not have that special case.
return fs.promises.rm(p, { recursive: true, force: true, maxRetries: process.platform === "win32" ? 10 : 0 });
}
/** @typedef {{
* name: string;
* paths: string | string[];
* ignored?: (path: string) => boolean;
* run: (paths: Set<string>, abortSignal: AbortSignal) => void | Promise<unknown>;
* }} WatchTask */
void 0;
/**
* @param {string} name
* @param {(paths: Set<string> | undefined, abortSignal: AbortSignal) => void | Promise<unknown>} run
* @param {object} options
* @param {string | string[]} options.paths
* @param {(path: string) => boolean} [options.ignored]
* @param {string} [options.name]
*/
async function watchDebounced(name, run, options) {
let watching = true;
let running = true;
let lastChangeTimeMs = Date.now();
let changedDeferred = /** @type {Deferred<void>} */ (new Deferred());
let abortController = new AbortController();
const debouncer = new Debouncer(1_000, endRun);
const watcher = chokidar.watch(options.paths, {
ignored: options.ignored,
ignorePermissionErrors: true,
alwaysStat: true,
});
// The paths that have changed since the last run.
/** @type {Set<string> | undefined} */
let paths;
process.on("SIGINT", endWatchMode);
process.on("beforeExit", endWatchMode);
watcher.on("all", onChange);
while (watching) {
const promise = changedDeferred.promise;
const token = abortController.signal;
if (!token.aborted) {
running = true;
try {
const thePaths = paths;
paths = new Set();
await run(thePaths, token);
}
catch {
// ignore
}
running = false;
}
if (watching) {
console.log(pc.yellowBright(`[${name}] run complete, waiting for changes...`));
await promise;
}
}
console.log("end");
/**
* @param {'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir' | 'all' | 'ready' | 'raw' | 'error'} eventName
* @param {string} path
* @param {fs.Stats | undefined} stats
*/
function onChange(eventName, path, stats) {
switch (eventName) {
case "change":
case "unlink":
case "unlinkDir":
break;
case "add":
case "addDir":
// skip files that are detected as 'add' but haven't actually changed since the last time we ran.
if (stats && stats.mtimeMs <= lastChangeTimeMs) {
return;
}
break;
}
beginRun(path);
}
/**
* @param {string} path
*/
function beginRun(path) {
if (debouncer.empty) {
console.log(pc.yellowBright(`[${name}] changed due to '${path}', restarting...`));
if (running) {
console.log(pc.yellowBright(`[${name}] aborting in-progress run...`));
}
abortController.abort();
abortController = new AbortController();
}
debouncer.enqueue();
paths ??= new Set();
paths.add(path);
}
function endRun() {
lastChangeTimeMs = Date.now();
changedDeferred.resolve();
changedDeferred = /** @type {Deferred<void>} */ (new Deferred());
}
function endWatchMode() {
if (watching) {
watching = false;
console.log(pc.yellowBright(`[${name}] exiting watch mode...`));
abortController.abort();
watcher.close();
}
}
}
/**
* @template T
*/
export class Deferred {
constructor() {
/** @type {Promise<T>} */
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
export class Debouncer {
/**
* @param {number} timeout
* @param {() => Promise<any> | void} action
*/
constructor(timeout, action) {
this._timeout = timeout;
this._action = action;
}
get empty() {
return !this._deferred;
}
enqueue() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = undefined;
}
if (!this._deferred) {
this._deferred = new Deferred();
}
this._timer = setTimeout(() => this.run(), 100);
return this._deferred.promise;
}
run() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = undefined;
}
const deferred = this._deferred;
assert(deferred);
this._deferred = undefined;
try {
deferred.resolve(this._action());
}
catch (e) {
deferred.reject(e);
}
}
}