Files
orca/config/scripts/ci-unit-dependency-graph.mjs
T
Neil 1912c90141 Extract subprocess handling into @orca/process-host (#26963)
Extract subprocess execution and process contracts into @orca/process-host with explicit public entry points, independent checks, and one Node 18 compatible CommonJS runtime. Migrate application consumers and integrate package builds, dependency selection, import enforcement, and runtime resource staging.

Preserve daemon startup ownership and profile isolation, and qualify development rebuild recovery and concurrent output publication. Remove unnecessary aliases, test forwarding exports, and a duplicate output scan.

Validation: full typechecks, changed-code quality, package and artifact checks, native Windows builds/types plus 306 runtime tests, and independent native reviews.

Matched full E2E controls on original main and unchanged reviewed head both pass all 17 applicable tests (nine skips). Earlier intermittent E2E failures are documented in the PR; no assertions were weakened. The final combined-tree audit preserves both branches with no import-boundary violations.
2026-10-09 18:32:28 -07:00

99 lines
2.8 KiB
JavaScript

import { globSync, readFileSync } from 'node:fs'
import { posix, join } from 'node:path'
import ts from 'typescript-api'
import { createWorkspaceSourceResolver } from './workspace-source-exports.mjs'
const EXTENSIONS = [
'',
'.ts',
'.tsx',
'.mjs',
'.js',
'.cjs',
'.json',
'/index.ts',
'/index.tsx',
'/index.js'
]
const INDIRECT_INPUT =
/\b(?:readFile\w*|readdir\w*|glob\w*|spawn\w*|execFile\w*|execSync|runProcess\w*|fork|Worker)\b|\bimport\s*\(\s*[^'"\s]|\brequire\s*\(\s*[^'"\s]|\bnew\s+URL\s*\(/
function localPath(file, specifier) {
if (specifier.startsWith('.')) {
return posix.join(posix.dirname(file), specifier)
}
if (specifier.startsWith('@renderer/')) {
return `src/renderer/src/${specifier.slice(10)}`
}
if (specifier.startsWith('@/')) {
return `src/renderer/src/${specifier.slice(2)}`
}
return null
}
export function buildUnitDependencyGraph(sources, workspace = { resolve: () => null }) {
const reverse = new Map()
const opaque = new Set()
function addConsumer(input, file) {
if (!reverse.has(input)) {
reverse.set(input, new Set())
}
reverse.get(input).add(file)
}
for (const [file, source] of sources) {
if (INDIRECT_INPUT.test(source) || file.startsWith('config/') || file.startsWith('tests/')) {
opaque.add(file)
}
for (const imported of ts.preProcessFile(source, true, true).importedFiles) {
const workspaceSource = workspace.resolve(imported.fileName)
const path = workspaceSource?.file ?? localPath(file, imported.fileName)
if (path === null) {
continue
}
let resolved
for (const extension of EXTENSIONS) {
const candidate = path + extension
if (sources.has(candidate)) {
resolved = candidate
break
}
}
if (!resolved) {
opaque.add(file)
continue
}
if (workspaceSource) {
addConsumer(workspaceSource.manifest, file)
}
addConsumer(resolved, file)
}
}
return { reverse, opaque }
}
export function collectUnitDependencyGraph(root = process.cwd()) {
const files = globSync(
[
'src/**/*.{ts,tsx,js,mjs,cjs,json}',
'config/**/*.{ts,tsx,js,mjs,cjs,json}',
'tests/**/*.{ts,tsx,js,mjs,cjs,json}'
],
{ cwd: root, exclude: ['**/node_modules/**', '**/dist/**'] }
)
const sources = new Map(
files.map((file) => [file.replaceAll('\\', '/'), readFileSync(join(root, file), 'utf8')])
)
const workspace = createWorkspaceSourceResolver(root)
return { ...buildUnitDependencyGraph(sources, workspace), files: new Set(sources.keys()) }
}
export function unitConsumers(seeds, reverse) {
const result = new Set(seeds)
for (const file of result) {
for (const consumer of reverse.get(file) ?? []) {
result.add(consumer)
}
}
return result
}