Files
orca/config/scripts/packaged-source-map-prune.test.mjs
T
Neil 97eb762b27 refactor(packaging): prune declaration and source-map artifacts in one walk (#17659)
* refactor(packaging): prune declaration and source-map artifacts in one walk

prunePackagedRuntimeTypeDeclarations and prunePackagedRuntimeSourceMaps
were byte-identical apart from their regex, and each did its own full
recursive walk of packaged Resources/node_modules (~1.7s per walk).
Collapse them into prunePackagedRuntimeTypeAndSourceMapArtifacts, which
runs a single walk with the OR of both predicates.

The two regexes are disjoint (.d.ts.map never ends in .js.map), so one
pass deletes exactly the union the two passes deleted. Neither old
function had a production caller outside prunePackagedRuntimeNodeModules,
so both exports are replaced by the combined one rather than kept as
wrappers, which would have reintroduced the duplicate walk.

Also moves prunePackagedZodSources ahead of the filename walk: zod/src is
removed wholesale, so traversing it first was pure wasted work. The
prunes are independent, so the reorder does not change the result.

* fix: correct the one-walk rationale and close the .d.mts coverage gap

The comment credited predicate disjointness for making the merge safe. That
is not the reason and is misleading: it implies a future overlapping
predicate would break the collapse. Passes commute because
pruneMatchingFiles only deletes files and never removes directories, so the
tree it walks is identical each time — verified by running the old two-walk
code with the passes reversed and diffing survivors.

Also narrow isPrunablePackagedRuntimeArtifact to isPrunableTypeOrSourceMapArtifact
(node-pty prebuilds and duplicate sherpa dylibs are prunable runtime
artifacts too, but this predicate returns false for them), and add the
missing .d.mts fixture so every branch of the (?:c|m)? alternation is
exercised against the exact-survivor assertion.
2026-08-31 01:19:32 -07:00

138 lines
5.9 KiB
JavaScript

import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
const require = createRequire(import.meta.url)
const {
prunePackagedRuntimeTypeAndSourceMapArtifacts,
prunePackagedRuntimeNodeModules
} = require('../packaged-runtime-node-modules.cjs')
const LINEAR_SDK_PACKAGE_JSON =
'{"name":"@linear/sdk","type":"module","main":"./dist/index.cjs","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.mjs"}}}'
async function createPackagedNodeModulesFixture(resourcesDir) {
const packageDir = join(resourcesDir, 'node_modules', '@linear', 'sdk')
const distDir = join(packageDir, 'dist')
const webhooksDir = join(packageDir, 'webhooks')
const updaterDir = join(resourcesDir, 'node_modules', 'electron-updater', 'out')
const jsYamlDir = join(resourcesDir, 'node_modules', 'js-yaml', 'dist')
const nodePtyDir = join(resourcesDir, 'node_modules', 'node-pty', 'lib')
await mkdir(distDir, { recursive: true })
await mkdir(webhooksDir, { recursive: true })
await mkdir(updaterDir, { recursive: true })
await mkdir(jsYamlDir, { recursive: true })
await mkdir(nodePtyDir, { recursive: true })
await writeFile(join(packageDir, 'package.json'), LINEAR_SDK_PACKAGE_JSON, 'utf8')
await writeFile(join(packageDir, 'README.md'), 'SDK documentation', 'utf8')
await writeFile(join(packageDir, 'metadata.json.map'), '{"keep":true}', 'utf8')
await writeFile(
join(distDir, 'index.cjs'),
"module.exports = require('./runtime-helper.cjs')",
'utf8'
)
await writeFile(
join(distDir, 'runtime-helper.cjs'),
'exports.LinearClient = class LinearClient {}',
'utf8'
)
await writeFile(join(distDir, 'index.mjs'), 'export {}', 'utf8')
await writeFile(join(distDir, 'index.cjs.map'), '{}', 'utf8')
await writeFile(join(distDir, 'index.mjs.map'), '{}', 'utf8')
await writeFile(join(webhooksDir, 'index.cjs'), 'module.exports = {}', 'utf8')
await writeFile(join(webhooksDir, 'index.cjs.map'), '{}', 'utf8')
await writeFile(join(updaterDir, 'main.js'), 'module.exports = {}', 'utf8')
await writeFile(join(updaterDir, 'main.js.map'), '{}', 'utf8')
await writeFile(join(updaterDir, 'main.d.ts'), 'export {}', 'utf8')
await writeFile(join(updaterDir, 'main.d.ts.map'), '{}', 'utf8')
await writeFile(join(jsYamlDir, 'js-yaml.min.js'), 'globalThis.jsyaml = {}', 'utf8')
await writeFile(join(jsYamlDir, 'js-yaml.min.js.map'), '{}', 'utf8')
await writeFile(join(nodePtyDir, 'index.js'), 'module.exports = {}', 'utf8')
await writeFile(join(nodePtyDir, 'index.js.map'), '{}', 'utf8')
return { packageDir, distDir, webhooksDir, updaterDir, jsYamlDir, nodePtyDir }
}
describe('packaged runtime type-declaration and source-map pruning', () => {
it('removes @linear/sdk source maps while preserving runtime files and non-JS maps', async () => {
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-prune-'))
try {
const { packageDir, distDir, webhooksDir } =
await createPackagedNodeModulesFixture(resourcesDir)
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
await expect(readdir(distDir).then((entries) => entries.sort())).resolves.toEqual([
'index.cjs',
'index.mjs',
'runtime-helper.cjs'
])
await expect(readdir(webhooksDir)).resolves.toEqual(['index.cjs'])
await expect(readFile(join(packageDir, 'package.json'), 'utf8')).resolves.toBe(
LINEAR_SDK_PACKAGE_JSON
)
await expect(readFile(join(packageDir, 'README.md'), 'utf8')).resolves.toBe(
'SDK documentation'
)
// Why: the predicate is filename-based, so a non-JS `.map` payload must survive.
await expect(readFile(join(packageDir, 'metadata.json.map'), 'utf8')).resolves.toBe(
'{"keep":true}'
)
const sdk = createRequire(join(resourcesDir, 'consumer.cjs'))('@linear/sdk')
expect(typeof sdk.LinearClient).toBe('function')
} finally {
await rm(resourcesDir, { recursive: true, force: true })
}
})
it('removes source maps from every packaged dependency, not just @linear/sdk', async () => {
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-prune-all-'))
try {
const { jsYamlDir, nodePtyDir } = await createPackagedNodeModulesFixture(resourcesDir)
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
await expect(readdir(jsYamlDir)).resolves.toEqual(['js-yaml.min.js'])
await expect(readdir(nodePtyDir)).resolves.toEqual(['index.js'])
} finally {
await rm(resourcesDir, { recursive: true, force: true })
}
})
it('removes type declarations and declaration maps in the same walk', async () => {
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-prune-dts-'))
try {
const { updaterDir } = await createPackagedNodeModulesFixture(resourcesDir)
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
await expect(readdir(updaterDir)).resolves.toEqual(['main.js'])
} finally {
await rm(resourcesDir, { recursive: true, force: true })
}
})
it('runs the artifact prune through aggregate runtime cleanup', async () => {
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-aggregate-prune-'))
try {
const { distDir, updaterDir, packageDir } =
await createPackagedNodeModulesFixture(resourcesDir)
prunePackagedRuntimeNodeModules(resourcesDir, 'darwin', 'arm64')
await expect(readdir(distDir).then((entries) => entries.sort())).resolves.toEqual([
'index.cjs',
'index.mjs',
'runtime-helper.cjs'
])
await expect(readdir(updaterDir)).resolves.toEqual(['main.js'])
await expect(readFile(join(packageDir, 'metadata.json.map'), 'utf8')).resolves.toBe(
'{"keep":true}'
)
} finally {
await rm(resourcesDir, { recursive: true, force: true })
}
})
})