Skip to content

Don't use needsUpdate for quick tasks #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions Herebyfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { task } from "hereby";
import _glob from "glob";
import util from "util";
import chalk from "chalk";
import { exec, readJson, needsUpdate, getDiffTool, getDirSize, memoize } from "./scripts/build/utils.mjs";
import { exec, readJson, getDiffTool, getDirSize, memoize, needsUpdate } from "./scripts/build/utils.mjs";
import { runConsoleTests, refBaseline, localBaseline, refRwcBaseline, localRwcBaseline } from "./scripts/build/tests.mjs";
import { buildProject as realBuildProject, cleanProject, watchProject } from "./scripts/build/projects.mjs";
import { localizationDirectories } from "./scripts/build/localization.mjs";
Expand Down Expand Up @@ -81,22 +81,18 @@ export const generateLibs = task({
description: "Builds the library targets",
run: async () => {
await fs.promises.mkdir("./built/local", { recursive: true });
const allSources = libs().flatMap((lib) => lib.sources);
const allTargets = libs().flatMap((lib) => lib.target);
if (needsUpdate([copyrightFilename, ...allSources], allTargets)) {
for (const lib of libs()) {
let output = await copyright();

for (const source of lib.sources) {
const contents = await fs.promises.readFile(source, "utf-8");
// TODO(jakebailey): "\n\n" is for compatibility with our current tests; our test baselines
// are sensitive to the positions of things in the lib files. Eventually remove this,
// or remove lib.d.ts line numbers from our baselines.
output += "\n\n" + contents.replace(/\r\n/g, "\n");
}

await fs.promises.writeFile(lib.target, output);
for (const lib of libs()) {
let output = await copyright();

for (const source of lib.sources) {
const contents = await fs.promises.readFile(source, "utf-8");
// TODO(jakebailey): "\n\n" is for compatibility with our current tests; our test baselines
// are sensitive to the positions of things in the lib files. Eventually remove this,
// or remove lib.d.ts line numbers from our baselines.
output += "\n\n" + contents.replace(/\r\n/g, "\n");
}

await fs.promises.writeFile(lib.target, output);
}
},
});
Expand All @@ -110,9 +106,7 @@ export const generateDiagnostics = task({
name: "generate-diagnostics",
description: "Generates a diagnostic file in TypeScript based on an input JSON file",
run: async () => {
if (needsUpdate(diagnosticMessagesJson, [diagnosticMessagesGeneratedJson, diagnosticInformationMapTs])) {
await exec(process.execPath, ["scripts/processDiagnosticMessages.mjs", diagnosticMessagesJson]);
}
await exec(process.execPath, ["scripts/processDiagnosticMessages.mjs", diagnosticMessagesJson]);
}
});

Expand Down Expand Up @@ -410,7 +404,11 @@ export const dtsServices = task({
name: "dts-services",
description: "Bundles typescript.d.ts",
dependencies: [buildServices],
run: () => runDtsBundler("./built/local/typescript/typescript.d.ts", "./built/local/typescript.d.ts"),
run: async () => {
if (needsUpdate("./built/local/typescript/tsconfig.tsbuildinfo", ["./built/local/typescript.d.ts", "./built/local/typescript.internal.d.ts"])) {
runDtsBundler("./built/local/typescript/typescript.d.ts", "./built/local/typescript.d.ts");
}
},
});


Expand Down Expand Up @@ -464,7 +462,11 @@ export const dtsLssl = task({
name: "dts-lssl",
description: "Bundles tsserverlibrary.d.ts",
dependencies: [buildLssl],
run: () => runDtsBundler("./built/local/tsserverlibrary/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.d.ts")
run: async () => {
if (needsUpdate("./built/local/tsserverlibrary/tsconfig.tsbuildinfo", ["./built/local/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.internal.d.ts"])) {
await runDtsBundler("./built/local/tsserverlibrary/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.d.ts");
}
}
});

export const dts = task({
Expand Down Expand Up @@ -560,11 +562,9 @@ export const generateTypesMap = task({
run: async () => {
const source = "src/server/typesMap.json";
const target = "built/local/typesMap.json";
if (needsUpdate(source, target)) {
const contents = await fs.promises.readFile(source, "utf-8");
JSON.parse(contents);
await fs.promises.writeFile(target, contents);
}
const contents = await fs.promises.readFile(source, "utf-8");
JSON.parse(contents); // Validates that the JSON parses.
await fs.promises.writeFile(target, contents);
}
});

Expand All @@ -577,11 +577,9 @@ const copyBuiltLocalDiagnosticMessages = task({
name: "copy-built-local-diagnostic-messages",
dependencies: [generateDiagnostics],
run: async () => {
if (needsUpdate(diagnosticMessagesGeneratedJson, builtLocalDiagnosticMessagesGeneratedJson)) {
const contents = await fs.promises.readFile(diagnosticMessagesGeneratedJson, "utf-8");
JSON.parse(contents);
await fs.promises.writeFile(builtLocalDiagnosticMessagesGeneratedJson, contents);
}
const contents = await fs.promises.readFile(diagnosticMessagesGeneratedJson, "utf-8");
JSON.parse(contents); // Validates that the JSON parses.
await fs.promises.writeFile(builtLocalDiagnosticMessagesGeneratedJson, contents);
}
});

Expand Down
25 changes: 17 additions & 8 deletions scripts/processDiagnosticMessages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void 0;

/** @typedef {Map<string, DiagnosticDetails>} InputDiagnosticMessageTable */

function main() {
async function main() {
if (process.argv.length < 3) {
console.log("Usage:");
console.log("\tnode processDiagnosticMessages.mjs <diagnostic-json-input-file>");
Expand All @@ -24,15 +24,24 @@ function main() {
* @param {string} fileName
* @param {string} contents
*/
function writeFile(fileName, contents) {
fs.writeFile(path.join(path.dirname(inputFilePath), fileName), contents, { encoding: "utf-8" }, err => {
if (err) throw err;
});
async function writeFile(fileName, contents) {
const filePath = path.join(path.dirname(inputFilePath), fileName);
try {
const existingContents = await fs.promises.readFile(filePath, "utf-8");
if (existingContents === contents) {
return;
}
}
catch {
// Just write the file.
}

await fs.promises.writeFile(filePath, contents, { encoding: "utf-8" });
}

const inputFilePath = process.argv[2].replace(/\\/g, "/");
console.log(`Reading diagnostics from ${inputFilePath}`);
const inputStr = fs.readFileSync(inputFilePath, { encoding: "utf-8" });
const inputStr = await fs.promises.readFile(inputFilePath, { encoding: "utf-8" });

/** @type {{ [key: string]: DiagnosticDetails }} */
const diagnosticMessagesJson = JSON.parse(inputStr);
Expand All @@ -47,10 +56,10 @@ function main() {

const infoFileOutput = buildInfoFileOutput(diagnosticMessages, inputFilePath);
checkForUniqueCodes(diagnosticMessages);
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
await writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);

const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
writeFile("diagnosticMessages.generated.json", messageOutput);
await writeFile("diagnosticMessages.generated.json", messageOutput);
}

/**
Expand Down