From 16b9f2114e65fb996f3fa5aec122fbb9b3cf5102 Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Mon, 9 Feb 2026 22:33:13 -0800 Subject: [PATCH] Simplify scripts and improve internal types --- ...-a1057346-1fa6-441e-b15a-c3a9963c7e7f.json | 102 ++++++++++++++++++ lage.config.js | 4 +- packages/cache-github-actions/package.json | 2 +- packages/cache/package.json | 4 +- packages/cli/package.json | 4 +- packages/config/package.json | 4 +- packages/e2e-tests/package.json | 4 +- packages/format-hrtime/package.json | 4 +- packages/hasher/package.json | 4 +- packages/logger/package.json | 4 +- packages/monorepo-fixture/package.json | 2 +- packages/reporters/package.json | 4 +- packages/rpc/package.json | 2 +- packages/runners/package.json | 4 +- packages/scheduler-types/package.json | 2 +- packages/scheduler/package.json | 4 +- packages/target-graph/package.json | 4 +- packages/worker-threads-pool/package.json | 4 +- scripts/bin/monorepo-scripts.js | 58 ++-------- scripts/commands/lint.js | 2 +- scripts/commands/transpile.js | 2 + scripts/config/jest.config.js | 1 + scripts/tsconfig.json | 6 +- scripts/types.ts | 16 +++ scripts/worker/depcheck.js | 4 +- scripts/worker/jest.js | 19 +++- scripts/worker/lint.js | 18 ++-- scripts/worker/transpile.js | 13 ++- scripts/worker/types.js | 38 ++++--- 29 files changed, 217 insertions(+), 122 deletions(-) create mode 100644 change/change-a1057346-1fa6-441e-b15a-c3a9963c7e7f.json create mode 100644 scripts/types.ts diff --git a/change/change-a1057346-1fa6-441e-b15a-c3a9963c7e7f.json b/change/change-a1057346-1fa6-441e-b15a-c3a9963c7e7f.json new file mode 100644 index 000000000..e238d0571 --- /dev/null +++ b/change/change-a1057346-1fa6-441e-b15a-c3a9963c7e7f.json @@ -0,0 +1,102 @@ +{ + "changes": [ + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/cache-github-actions", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/cache", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/cli", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/config", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/format-hrtime", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/hasher", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/logger", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/reporters", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/rpc", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/runners", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/scheduler-types", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/scheduler", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/target-graph", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + }, + { + "type": "none", + "comment": "Update scripts", + "packageName": "@lage-run/worker-threads-pool", + "email": "elcraig@microsoft.com", + "dependentChangeType": "none" + } + ] +} \ No newline at end of file diff --git a/lage.config.js b/lage.config.js index 07d7a6821..6141bced3 100644 --- a/lage.config.js +++ b/lage.config.js @@ -10,7 +10,7 @@ const fastGlob = require("fast-glob"); const config = { pipeline: { "lage#bundle": ["^^transpile", "types"], - // Note that transpile/types/bundle are overridden later for the @lage-run/globby package + // Note that transpile/types are overridden later for the @lage-run/globby package types: { type: "worker", options: { @@ -37,7 +37,7 @@ const config = { test: { type: "worker", weight: (target) => { - return fastGlob.sync("tests/**/*.test.ts", { cwd: target.cwd }).length; + return fastGlob.sync("src/__tests__/**/*.test.ts", { cwd: target.cwd }).length; }, options: { worker: path.join(__dirname, "scripts/worker/jest.js"), diff --git a/packages/cache-github-actions/package.json b/packages/cache-github-actions/package.json index 6a6c1f6c0..6b4c7b84b 100644 --- a/packages/cache-github-actions/package.json +++ b/packages/cache-github-actions/package.json @@ -13,7 +13,7 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", + "types": "yarn run -T tsc", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/cache/package.json b/packages/cache/package.json index 832235465..9f9fc2a57 100644 --- a/packages/cache/package.json +++ b/packages/cache/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/cli/package.json b/packages/cli/package.json index 7b48eba80..98023ea19 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -17,8 +17,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/config/package.json b/packages/config/package.json index 6df0a01e7..48c6f9a06 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/e2e-tests/package.json b/packages/e2e-tests/package.json index 135eba45d..dbef7a5d2 100644 --- a/packages/e2e-tests/package.json +++ b/packages/e2e-tests/package.json @@ -12,8 +12,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/format-hrtime/package.json b/packages/format-hrtime/package.json index 614ba3c2c..b6ec613cc 100644 --- a/packages/format-hrtime/package.json +++ b/packages/format-hrtime/package.json @@ -11,8 +11,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "devDependencies": { diff --git a/packages/hasher/package.json b/packages/hasher/package.json index 2f3fd20b0..eefbca847 100644 --- a/packages/hasher/package.json +++ b/packages/hasher/package.json @@ -11,8 +11,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/logger/package.json b/packages/logger/package.json index 36916177f..076c8daec 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "devDependencies": { diff --git a/packages/monorepo-fixture/package.json b/packages/monorepo-fixture/package.json index ea59abc40..dbd05d034 100644 --- a/packages/monorepo-fixture/package.json +++ b/packages/monorepo-fixture/package.json @@ -12,7 +12,7 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", + "types": "yarn run -T tsc", "lint": "monorepo-scripts lint" }, "devDependencies": { diff --git a/packages/reporters/package.json b/packages/reporters/package.json index 991cb8370..fe7f22e4d 100644 --- a/packages/reporters/package.json +++ b/packages/reporters/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/rpc/package.json b/packages/rpc/package.json index c5073b065..f5c861ac5 100644 --- a/packages/rpc/package.json +++ b/packages/rpc/package.json @@ -13,7 +13,7 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", + "types": "yarn run -T tsc", "lint": "buf lint", "generate": "buf generate" }, diff --git a/packages/runners/package.json b/packages/runners/package.json index a5344ef8a..483d1d2d3 100644 --- a/packages/runners/package.json +++ b/packages/runners/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/scheduler-types/package.json b/packages/scheduler-types/package.json index ab86ec064..dc4e8c1de 100644 --- a/packages/scheduler-types/package.json +++ b/packages/scheduler-types/package.json @@ -13,7 +13,7 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", + "types": "yarn run -T tsc", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/scheduler/package.json b/packages/scheduler/package.json index a80d2073b..ec917a3ea 100644 --- a/packages/scheduler/package.json +++ b/packages/scheduler/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/target-graph/package.json b/packages/target-graph/package.json index c1ea4ea0e..c7d00bb8b 100644 --- a/packages/target-graph/package.json +++ b/packages/target-graph/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/packages/worker-threads-pool/package.json b/packages/worker-threads-pool/package.json index 805fe2ede..588db997c 100644 --- a/packages/worker-threads-pool/package.json +++ b/packages/worker-threads-pool/package.json @@ -13,8 +13,8 @@ "scripts": { "build": "yarn types && yarn transpile", "transpile": "monorepo-scripts transpile", - "types": "monorepo-scripts tsc", - "test": "monorepo-scripts jest", + "types": "yarn run -T tsc", + "test": "yarn run -T jest", "lint": "monorepo-scripts lint" }, "dependencies": { diff --git a/scripts/bin/monorepo-scripts.js b/scripts/bin/monorepo-scripts.js index efebee16b..49173953b 100755 --- a/scripts/bin/monorepo-scripts.js +++ b/scripts/bin/monorepo-scripts.js @@ -2,62 +2,18 @@ const { spawn } = require("child_process"); const path = require("path"); const fs = require("fs"); -const { findPackageRoot, findProjectRoot } = require("workspace-tools"); - -const projectRoot = findProjectRoot(process.cwd()); const [command, ...spawnArgs] = process.argv.slice(2); -const spawnCommand = findCommand(command); -let cp; -if (spawnCommand.endsWith(".js")) { - console.log(`Running ${command} as: node ${spawnCommand} ${spawnArgs.join(" ")}`); - cp = spawn(process.execPath, [spawnCommand, ...spawnArgs], { +const commandPath = path.resolve(__dirname, `../commands/${command}.js`); +if (fs.existsSync(commandPath)) { + const cp = spawn(process.execPath, [commandPath, ...spawnArgs], { stdio: "inherit", }); -} else { - console.log(`Running ${command} as: ${spawnCommand} ${spawnArgs.join(" ")}`); - cp = spawn(spawnCommand, spawnArgs, { - stdio: "inherit", - shell: true, + cp.on("exit", (code) => { + process.exitCode = code || 0; }); -} - -cp.on("exit", (code) => { - process.exitCode = code || 0; -}); - -function findCommand(/** @type {string} */ command) { - // Try to find a command under monorepo-scripts/commands - const commandPath = path.resolve(__dirname, "../commands", `${command}.js`); - if (fs.existsSync(commandPath)) { - return commandPath; - } - - // Try to find command in package's own node_modules, then scripts/node_modules, then root node_modules - for (const basePath of [findPackageRoot(process.cwd()), findPackageRoot(__dirname), projectRoot]) { - if (basePath) { - const binPath = getBinPath(basePath, command); - if (binPath) { - return binPath; - } - } - } - - console.error("Could not find command: " + command); +} else { + console.error(`Command not found (under scripts/commands): ${command}`); process.exit(1); } - -function getBinPath(/** @type {string | undefined} */ packagePath, /** @type {string | undefined} */ command) { - if (!packagePath || !command) { - return undefined; - } - - const binPath = path.join(packagePath, "node_modules/.bin", process.platform === "win32" ? `${command}.cmd` : command); - - if (fs.existsSync(binPath)) { - return binPath; - } - - return undefined; -} diff --git a/scripts/commands/lint.js b/scripts/commands/lint.js index 15c3a93ea..0abb2498e 100755 --- a/scripts/commands/lint.js +++ b/scripts/commands/lint.js @@ -9,7 +9,7 @@ const lintWorker = require("../worker/lint"); } return lintWorker({ - target: { packageName: packageInfo.name, cwd: path.dirname(packageInfo.packageJsonPath), task: "lint" }, + target: { packageName: packageInfo.name, cwd: path.dirname(packageInfo.packageJsonPath) }, taskArgs: process.argv.slice(2), }); })().catch((error) => { diff --git a/scripts/commands/transpile.js b/scripts/commands/transpile.js index 072282595..0c6e39b64 100644 --- a/scripts/commands/transpile.js +++ b/scripts/commands/transpile.js @@ -11,6 +11,8 @@ const { getPackageInfo } = require("workspace-tools"); await transpileWorker({ target: { packageName: packageJson.name, cwd: packageRoot }, + // no CLI args are respected for the swc wrapper + taskArgs: [], }); })().catch((error) => { process.exitCode = 1; diff --git a/scripts/config/jest.config.js b/scripts/config/jest.config.js index 331a28e70..f818fac90 100644 --- a/scripts/config/jest.config.js +++ b/scripts/config/jest.config.js @@ -27,6 +27,7 @@ moduleNameMapper["^(\\.{1,2}/.*)\\.js$"] = "$1"; /** @type {import("jest").Config} */ const config = { clearMocks: true, + passWithNoTests: true, extensionsToTreatAsEsm: [".ts"], testMatch: ["**/?(*.)+(spec|test).ts?(x)"], testPathIgnorePatterns: ["/node_modules/"], diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index 3054d1c6e..c92a9f953 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -4,11 +4,7 @@ "emitDeclarationOnly": false, "noEmit": true, "paths": { - // Special path mappings to avoid relative references in individual files - "@/TargetGraph": ["../packages/target-graph/src/index.ts"], - "@/TargetRunner": ["../packages/runners/src/types/TargetRunner.ts"], - "@/WorkerRunner": ["../packages/runners/src/WorkerRunner.ts"], - // Required for above types to resolve @lage-run/target-graph if not built yet + // Required for resolution of @lage-run/target-graph by references in types.ts if not built yet "@lage-run/target-graph": ["../packages/target-graph/src/index.ts"] } }, diff --git a/scripts/types.ts b/scripts/types.ts new file mode 100644 index 000000000..0fe01f0c6 --- /dev/null +++ b/scripts/types.ts @@ -0,0 +1,16 @@ +import type { WorkerRunnerOptions as BaseWorkerRunnerOptions } from "../packages/runners/src/WorkerRunner.js"; +import type { TargetRunnerOptions } from "../packages/runners/src/types/TargetRunner.js"; +import type { Target } from "../packages/target-graph/src/index.js"; + +/** + * Actual complete worker runner options. + * TODO: a type like this should be exported from the runners package and used elsewhere... + */ +export type WorkerRunnerOptions = BaseWorkerRunnerOptions & TargetRunnerOptions; + +/** + * Subset of options needed by the worker functions that are reused by `commands/*.js`. + */ +export type BasicWorkerRunnerOptions = Pick & { + target: Pick; +}; diff --git a/scripts/worker/depcheck.js b/scripts/worker/depcheck.js index f40faca91..be278859a 100644 --- a/scripts/worker/depcheck.js +++ b/scripts/worker/depcheck.js @@ -1,4 +1,4 @@ -/** @import { TargetRunnerOptions } from "@/TargetRunner" */ +/** @import { WorkerRunnerOptions } from "../types.js" */ const depcheck = require("depcheck"); const path = require("path"); const { findProjectRoot } = require("workspace-tools"); @@ -17,7 +17,7 @@ const extraIgnoreMatches = { }; /** - * @param {TargetRunnerOptions} data + * @param {WorkerRunnerOptions} data */ async function depcheckWorker({ target }) { if (!target.packageName) return; diff --git a/scripts/worker/jest.js b/scripts/worker/jest.js index 22a44612a..162e1a65f 100644 --- a/scripts/worker/jest.js +++ b/scripts/worker/jest.js @@ -1,17 +1,30 @@ -/** @import { TargetRunnerOptions } from "@/TargetRunner" */ +/** @import { WorkerRunnerOptions } from "../types.js" */ +const fs = require("fs"); const { runCLI } = require("jest"); +const path = require("path"); /** - * @param {TargetRunnerOptions} data + * This worker is used for `lage run test`, in place of the per-package `test` script. + * + * Note that if running `test` for an individual package, it will use that package's `test` script instead + * (typically `yarn run -T jest`). + * + * @param {WorkerRunnerOptions} data */ async function jest({ target, weight }) { + if (!fs.existsSync(path.join(target.cwd, "jest.config.js"))) { + console.log("No jest.config.js found - skipping"); + return; + } + console.log(`Running ${target.id}, maxWorkers: ${weight}`); const { results } = await runCLI( { + // Instead of adding more options here, prefer adding them to jest.config.js unless they're truly + // specific to running in this lage worker context. maxWorkers: weight, rootDir: target.cwd, - passWithNoTests: true, verbose: true, _: [], $0: "", diff --git a/scripts/worker/lint.js b/scripts/worker/lint.js index c7b567c76..be7bd9625 100644 --- a/scripts/worker/lint.js +++ b/scripts/worker/lint.js @@ -1,20 +1,22 @@ -/** @import { Target } from "@/TargetGraph" */ -/** @import { WorkerRunnerOptions } from "@/WorkerRunner" */ +/** @import { BasicWorkerRunnerOptions } from "../types.js" */ const { ESLint } = require("eslint"); const path = require("path"); const { getPackageInfo } = require("workspace-tools"); /** - * The type here should be `WorkerRunnerOptions & TargetRunnerOptions`, but we only specify the - * needed properties so the runner function can be reused by commands/lint.js. - * @param {WorkerRunnerOptions & { target: Pick }} data + * This worker is used for `lage run lint`, in place of the per-package `lint` script. + * + * Since this worker function has some extra logic/config, it's reused by the per-package `lint` script + * (`monorepo-scripts lint` which runs commands/lint.js) to avoid duplication. + * + * @param {BasicWorkerRunnerOptions} data */ async function lint(data) { const { target, taskArgs } = data; const packageJson = getPackageInfo(target.cwd); - if (!packageJson?.scripts?.[target.task]) { - process.stdout.write(`No script found for ${target.task} in ${target.cwd}... skipped`); + if (!packageJson?.scripts?.lint) { + process.stdout.write('No "lint" script found - skipping'); // pass return; } @@ -37,10 +39,8 @@ async function lint(data) { const formatter = await eslint.loadFormatter("stylish"); const resultText = await formatter.format(results); - // 3. Modify the files with the fixed code. await ESLint.outputFixes(results); - // 4. Output it. if (resultText) { process.stdout.write(resultText + "\n"); } diff --git a/scripts/worker/transpile.js b/scripts/worker/transpile.js index 208cbadb2..8410f3727 100644 --- a/scripts/worker/transpile.js +++ b/scripts/worker/transpile.js @@ -1,4 +1,4 @@ -/** @import { Target } from "@/TargetGraph" */ +/** @import { BasicWorkerRunnerOptions } from "../types.js" */ const fs = require("fs"); const path = require("path"); const fsPromises = require("fs/promises"); @@ -8,9 +8,13 @@ const { findProjectRoot } = require("workspace-tools"); const root = findProjectRoot(process.cwd()); /** - * The type here should be `WorkerRunnerOptions & TargetRunnerOptions`, but we only specify the - * needed properties so the runner function can be reused by commands/transpile.js. - * @param {{ target: Pick }} data + * This worker is used for `lage run transpile`, in place of the per-package `transpile` script + * (except for `@lage-run/globby`, which per lage.config.js uses its custom `transpile` script). + * + * Since this worker function has some extra logic to use swc, it's reused by the per-package `transpile` script + * (`monorepo-scripts transpile` which runs commands/transpile.js) to avoid duplication. + * + * @param {BasicWorkerRunnerOptions} data */ async function transpile({ target }) { if (target.packageName?.includes("docs")) { @@ -20,6 +24,7 @@ async function transpile({ target }) { // Start from the src directory to avoid unnecessary transpilation of scripts etc const srcDir = path.join(target.cwd, "src"); if (!fs.existsSync(srcDir)) { + console.log("No src directory found - skipping"); return; } diff --git a/scripts/worker/types.js b/scripts/worker/types.js index ce4a261b3..a6b408dcc 100644 --- a/scripts/worker/types.js +++ b/scripts/worker/types.js @@ -1,4 +1,4 @@ -/** @import { Target } from "@/TargetGraph" */ +/** @import { WorkerRunnerOptions } from "../types" */ const ts = require("typescript"); const path = require("path"); const { existsSync } = require("fs"); @@ -6,30 +6,32 @@ const { existsSync } = require("fs"); /** @type {ts.Program | undefined} */ let oldProgram; -const log = (/** @type {*} */ msg) => { - process.stdout.write(String(msg) + "\n"); -}; - /** - * The type here should be `WorkerRunnerOptions & TargetRunnerOptions`, but we only specify the - * needed properties so the runner function can be reused by commands/types.js. - * @param {{ target: Pick }} data + * This worker is used for `lage run types`, in place of the per-package `types` script + * (except for `@lage-run/globby`, which per lage.config.js uses its custom `types` script). + * + * Note that if running `types` for an individual package, it will use that package's `types` script instead + * (typically `yarn run -T tsc`). + * + * @param {WorkerRunnerOptions} data */ async function run(data) { - const { target } = data; + const { target, taskArgs } = data; + + const verbose = taskArgs.includes("--verbose"); const tsconfigFile = "tsconfig.json"; const tsconfigJsonFile = path.join(target.cwd, tsconfigFile); // Find tsconfig.json if (!existsSync(tsconfigJsonFile)) { - log("no tsconfig.json found - skipping this package"); + console.log("no tsconfig.json found - skipping"); // pass return; } // Parse tsconfig - log(`Parsing config...`); + verbose && console.log(`Parsing config...`); const parsedCommandLine = ts.getParsedCommandLineOfConfigFile( tsconfigJsonFile, {}, @@ -50,10 +52,12 @@ async function run(data) { throw new Error("Could not parse tsconfig.json"); } + // This should NOT be modified here! Instead, any options updates should be made to tsconfig.base.json + // so that they're also reflected if used by the per-package `types` script (`yarn run -T tsc`). const compilerOptions = parsedCommandLine.options; // Creating compilation host program - log(`Creating host compiler...`); + verbose && console.log(`Creating host compiler...`); const compilerHost = ts.createCompilerHost(compilerOptions); const program = ts.createProgram(parsedCommandLine.fileNames, compilerOptions, compilerHost, oldProgram); @@ -68,11 +72,11 @@ async function run(data) { const allErrors = []; - log(`Compiling...`); + console.log(`Compiling...`); try { program.emit(); } catch (e) { - log(`Encountered error while transpiling: ${/** @type {ts.Diagnostic} */ (e).messageText}`); + console.error(`Encountered error while transpiling: ${/** @type {ts.Diagnostic} */ (e).messageText}`); throw new Error("Encountered error while transpiling"); } let hasErrors = false; @@ -85,12 +89,12 @@ async function run(data) { } if (hasErrors) { - log(`Type errors found in ${target.packageName}`); - log(ts.formatDiagnosticsWithColorAndContext(allErrors, compilerHost)); + console.error(`Type errors found in ${target.packageName}`); + console.error(ts.formatDiagnosticsWithColorAndContext(allErrors, compilerHost)); throw new Error("Failed to compile"); } else { - log("Compiled successfully"); + console.log("Compiled successfully"); return; }