release: WRNexusJS 0.8.3
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import { dirname, join, relative, resolve, sep } from "node:path";
|
||||
@@ -7,6 +8,15 @@ import { fileURLToPath } from "node:url";
|
||||
const require = createRequire(import.meta.url);
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const root = resolve(here, "..");
|
||||
const scriptPath = fileURLToPath(import.meta.url);
|
||||
|
||||
function normalizeText(value) {
|
||||
return value.replace(/\r\n?/g, "\n");
|
||||
}
|
||||
|
||||
function sha256(value) {
|
||||
return createHash("sha256").update(value).digest("hex");
|
||||
}
|
||||
|
||||
function loadTypeScript() {
|
||||
const candidates = [process.env.TYPESCRIPT_PATH, "typescript"].filter(Boolean);
|
||||
@@ -44,9 +54,15 @@ const sourceFiles = [
|
||||
...walk(join(root, "packages", "compiler", "src")),
|
||||
].sort();
|
||||
|
||||
const sourceHashBuilder = createHash("sha256");
|
||||
const modules = [];
|
||||
for (const file of sourceFiles) {
|
||||
const source = readFileSync(file, "utf8");
|
||||
const id = moduleId(file);
|
||||
const source = normalizeText(readFileSync(file, "utf8"));
|
||||
sourceHashBuilder.update(id);
|
||||
sourceHashBuilder.update("\0");
|
||||
sourceHashBuilder.update(source);
|
||||
sourceHashBuilder.update("\0");
|
||||
const result = ts.transpileModule(source, {
|
||||
fileName: file,
|
||||
reportDiagnostics: true,
|
||||
@@ -60,6 +76,7 @@ for (const file of sourceFiles) {
|
||||
inlineSourceMap: false,
|
||||
removeComments: false,
|
||||
rewriteRelativeImportExtensions: true,
|
||||
newLine: ts.NewLineKind.LineFeed,
|
||||
},
|
||||
});
|
||||
const diagnostics = result.diagnostics ?? [];
|
||||
@@ -72,12 +89,18 @@ for (const file of sourceFiles) {
|
||||
throw new Error(message);
|
||||
}
|
||||
modules.push(
|
||||
`${JSON.stringify(moduleId(file))}: function (module, exports, require, __filename, __dirname) {\n${result.outputText}\n}`,
|
||||
`${JSON.stringify(id)}: function (module, exports, require, __filename, __dirname) {\n${normalizeText(result.outputText)}\n}`,
|
||||
);
|
||||
}
|
||||
|
||||
const sourceHash = sourceHashBuilder.digest("hex");
|
||||
const generatorHash = sha256(normalizeText(readFileSync(scriptPath, "utf8")));
|
||||
|
||||
const output = `"use strict";
|
||||
// Generated by scripts/build-editor-compiler.mjs. Do not edit directly.
|
||||
// WRN editor compiler source hash: ${sourceHash}
|
||||
// WRN editor compiler generator hash: ${generatorHash}
|
||||
// Generated with TypeScript: ${ts.version}
|
||||
const __nodeRequire = require;
|
||||
const __path = __nodeRequire("node:path");
|
||||
const __modules = {
|
||||
@@ -129,17 +152,35 @@ module.exports = __load("packages/compiler/src/index.ts");
|
||||
|
||||
const destination = join(root, "editors", "vscode", "src", "compiler.cjs");
|
||||
if (process.argv.includes("--check")) {
|
||||
if (!existsSync(destination) || readFileSync(destination, "utf8") !== output) {
|
||||
const existing = existsSync(destination) ? normalizeText(readFileSync(destination, "utf8")) : "";
|
||||
const embeddedSourceHash = existing.match(
|
||||
/^\/\/ WRN editor compiler source hash: ([a-f0-9]{64})$/m,
|
||||
)?.[1];
|
||||
const embeddedGeneratorHash = existing.match(
|
||||
/^\/\/ WRN editor compiler generator hash: ([a-f0-9]{64})$/m,
|
||||
)?.[1];
|
||||
const embeddedTypeScriptVersion = existing.match(/^\/\/ Generated with TypeScript: (.+)$/m)?.[1];
|
||||
|
||||
const hashesMatch = embeddedSourceHash === sourceHash && embeddedGeneratorHash === generatorHash;
|
||||
const exactMatch = existing === normalizeText(output);
|
||||
const compatibleCompilerDifference =
|
||||
hashesMatch && Boolean(embeddedTypeScriptVersion) && embeddedTypeScriptVersion !== ts.version;
|
||||
|
||||
if (!hashesMatch || (!exactMatch && !compatibleCompilerDifference)) {
|
||||
throw new Error(
|
||||
`${relative(root, destination)} is stale. Run bun run --cwd editors/vscode build and commit the result.`,
|
||||
);
|
||||
}
|
||||
|
||||
const versionDetail = exactMatch
|
||||
? `TypeScript ${ts.version}`
|
||||
: `source-compatible output generated with TypeScript ${embeddedTypeScriptVersion}; current TypeScript is ${ts.version}`;
|
||||
process.stdout.write(
|
||||
`Verified ${relative(root, destination)} matches ${sourceFiles.length} TypeScript modules.\n`,
|
||||
`Verified ${relative(root, destination)} matches ${sourceFiles.length} TypeScript modules (${versionDetail}).\n`,
|
||||
);
|
||||
} else {
|
||||
writeFileSync(destination, output, "utf8");
|
||||
process.stdout.write(
|
||||
`Built ${relative(root, destination)} from ${sourceFiles.length} TypeScript modules.\n`,
|
||||
`Built ${relative(root, destination)} from ${sourceFiles.length} TypeScript modules with TypeScript ${ts.version}.\n`,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user