diff --git a/package.json b/package.json index 89ef88fd09..0a8ec3fb41 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "build-storybook": "pnpm --filter @paperclipai/ui build-storybook", "build": "pnpm run preflight:workspace-links && pnpm -r build", "typecheck": "pnpm run preflight:workspace-links && pnpm -r typecheck", - "typecheck:build-gaps": "pnpm run preflight:workspace-links && node scripts/run-typecheck-build-gaps.mjs", + "typecheck:build-gaps": "pnpm run preflight:workspace-links && pnpm --filter @paperclipai/plugin-sdk ensure-build-deps && pnpm --filter @paperclipai/server build && node scripts/run-typecheck-build-gaps.mjs", "test": "pnpm run test:run", "test:watch": "pnpm run preflight:workspace-links && vitest", "test:run": "pnpm run preflight:workspace-links && node scripts/run-vitest-stable.mjs", diff --git a/scripts/__tests__/run-typecheck-build-gaps.test.mjs b/scripts/__tests__/run-typecheck-build-gaps.test.mjs new file mode 100644 index 0000000000..f3d1bc0799 --- /dev/null +++ b/scripts/__tests__/run-typecheck-build-gaps.test.mjs @@ -0,0 +1,60 @@ +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import test from "node:test"; + +const script = new URL("../run-typecheck-build-gaps.mjs", import.meta.url).pathname; + +function createFixtureRepo() { + return mkdtempSync(path.join(tmpdir(), "run-typecheck-build-gaps-test-")); +} + +function writeFixtureFile(root, relativePath, body = "fixture") { + const filePath = path.join(root, relativePath); + mkdirSync(path.dirname(filePath), { recursive: true }); + writeFileSync(filePath, body); +} + +function runRuntimeAssetGuard(root) { + return spawnSync(process.execPath, [script, "--runtime-assets-only"], { + cwd: root, + encoding: "utf8", + }); +} + +test("passes when all source runtime assets are present in dist", () => { + const root = createFixtureRepo(); + try { + writeFixtureFile(root, "server/src/built-ins/agents/default.md"); + writeFixtureFile(root, "server/dist/built-ins/agents/default.md"); + writeFixtureFile(root, "server/src/onboarding-assets/welcome.txt"); + writeFixtureFile(root, "server/dist/onboarding-assets/welcome.txt"); + writeFixtureFile(root, "server/src/built-ins/ignored.ts"); + + const result = runRuntimeAssetGuard(root); + + assert.equal(result.status, 0, result.stderr); + assert.match(result.stdout, /server runtime assets present in dist: 2 file\(s\)/); + assert.equal(result.stderr, ""); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +test("fails with the missing source asset and expected dist path", () => { + const root = createFixtureRepo(); + try { + writeFixtureFile(root, "server/src/built-ins/agents/default.md"); + + const result = runRuntimeAssetGuard(root); + + assert.notEqual(result.status, 0); + assert.match(result.stderr, /Missing server runtime asset\(s\) in dist/); + assert.match(result.stderr, /source: server\/src\/built-ins\/agents\/default\.md/); + assert.match(result.stderr, /expected dist: server\/dist\/built-ins\/agents\/default\.md/); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/scripts/run-typecheck-build-gaps.mjs b/scripts/run-typecheck-build-gaps.mjs index e149990bf6..9c2320969e 100644 --- a/scripts/run-typecheck-build-gaps.mjs +++ b/scripts/run-typecheck-build-gaps.mjs @@ -1,9 +1,17 @@ #!/usr/bin/env node -import { readFileSync } from "node:fs"; +import { existsSync, readFileSync, readdirSync, statSync } from "node:fs"; import path from "node:path"; import { spawnSync } from "node:child_process"; const repoRoot = process.cwd(); +const args = new Set(process.argv.slice(2)); +const allowedArgs = new Set(["--runtime-assets-only"]); + +for (const arg of args) { + if (!allowedArgs.has(arg)) { + fail(`Unknown argument: ${arg}`); + } +} function fail(message) { console.error(`[typecheck:build-gaps] ${message}`); @@ -30,6 +38,91 @@ function readJson(filePath) { return JSON.parse(readFileSync(filePath, "utf8")); } +function formatPath(filePath) { + return path.relative(repoRoot, filePath).split(path.sep).join("/"); +} + +function listFilesRecursive(rootDir) { + if (!existsSync(rootDir)) { + return []; + } + + const files = []; + const entries = readdirSync(rootDir, { withFileTypes: true }).sort((left, right) => + left.name.localeCompare(right.name), + ); + + for (const entry of entries) { + const entryPath = path.join(rootDir, entry.name); + if (entry.isDirectory()) { + files.push(...listFilesRecursive(entryPath)); + continue; + } + + if (entry.isFile()) { + files.push(entryPath); + } + } + + return files; +} + +function isRuntimeAsset(filePath) { + const ext = path.extname(filePath).toLowerCase(); + return !new Set([".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]).has(ext); +} + +function checkServerRuntimeAssets() { + const runtimeAssetTrees = [ + { + sourceDir: path.join(repoRoot, "server", "src", "built-ins"), + distDir: path.join(repoRoot, "server", "dist", "built-ins"), + }, + { + sourceDir: path.join(repoRoot, "server", "src", "onboarding-assets"), + distDir: path.join(repoRoot, "server", "dist", "onboarding-assets"), + }, + ]; + const missingAssets = []; + let checkedCount = 0; + + for (const assetTree of runtimeAssetTrees) { + const sourceAssets = listFilesRecursive(assetTree.sourceDir).filter(isRuntimeAsset); + checkedCount += sourceAssets.length; + + for (const sourcePath of sourceAssets) { + const relativeAssetPath = path.relative(assetTree.sourceDir, sourcePath); + const distPath = path.join(assetTree.distDir, relativeAssetPath); + + if (!existsSync(distPath) || !statSync(distPath).isFile()) { + missingAssets.push({ sourcePath, distPath }); + } + } + } + + if (missingAssets.length > 0) { + const missingList = missingAssets + .map( + ({ sourcePath, distPath }) => + ` - source: ${formatPath(sourcePath)}\n expected dist: ${formatPath(distPath)}`, + ) + .join("\n"); + + fail( + `Missing server runtime asset(s) in dist:\n${missingList}\nRun pnpm --filter @paperclipai/server build and ensure source runtime asset trees are copied into dist.`, + ); + } + + console.log( + `[typecheck:build-gaps] server runtime assets present in dist: ${checkedCount} file(s)`, + ); +} + +if (args.has("--runtime-assets-only")) { + checkServerRuntimeAssets(); + process.exit(0); +} + function listWorkspacePackages() { const result = spawnSync("pnpm", ["ls", "-r", "--depth", "-1", "--json"], { cwd: repoRoot, @@ -82,12 +175,12 @@ console.log( `[typecheck:build-gaps] typechecking ${buildGapPackages.length} workspace(s): ${buildGapPackages.map(({ name }) => name).join(", ") || "(none)"}`, ); -if (buildGapPackages.length === 0) { - process.exit(0); +if (buildGapPackages.length > 0) { + run("pnpm", ["--filter", "@paperclipai/plugin-sdk", "ensure-build-deps"]); + + for (const workspacePkg of buildGapPackages) { + run("pnpm", ["--filter", workspacePkg.name, "typecheck"]); + } } -run("pnpm", ["--filter", "@paperclipai/plugin-sdk", "ensure-build-deps"]); - -for (const workspacePkg of buildGapPackages) { - run("pnpm", ["--filter", workspacePkg.name, "typecheck"]); -} +checkServerRuntimeAssets();