Files
orca/config/scripts/workspace-source-exports.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

105 lines
3.9 KiB
JavaScript

import { existsSync, globSync, readFileSync, realpathSync } from 'node:fs'
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
import { parse } from 'yaml'
export function workspacePackageManifests(root) {
const policy = join(root, 'pnpm-workspace.yaml')
if (!existsSync(policy)) {
return []
}
const { packages = [] } = parse(readFileSync(policy, 'utf8')) ?? {}
if (!Array.isArray(packages) || packages.some((pattern) => typeof pattern !== 'string')) {
throw new Error('Workspace package patterns are unavailable')
}
const manifests = (patterns) =>
patterns.map((pattern) => `${pattern.replace(/\/$/, '')}/package.json`)
return globSync(manifests(packages.filter((pattern) => !pattern.startsWith('!'))), {
cwd: root,
exclude: manifests(
packages.filter((pattern) => pattern.startsWith('!')).map((pattern) => pattern.slice(1))
)
})
.map((manifest) => manifest.split(sep).join('/'))
.sort()
}
function sourceTarget(root, manifest, target) {
if (
typeof target !== 'string' ||
!target.startsWith('./') ||
target.includes('\\') ||
target.split('/').some((segment) => ['..', 'node_modules', 'dist'].includes(segment)) ||
!/\.(?:ts|tsx|js|mjs|cjs|json)$/.test(target) ||
target.endsWith('.d.ts')
) {
throw new Error(`Invalid orca-source export in ${manifest}`)
}
const packageDir = realpathSync(join(root, dirname(manifest)))
const path = realpathSync(resolve(packageDir, target))
const withinPackage = relative(packageDir, path)
if (withinPackage.startsWith(`..${sep}`) || isAbsolute(withinPackage)) {
throw new Error(`orca-source export escapes ${manifest}`)
}
return { path, file: relative(root, path).split(sep).join('/'), manifest }
}
function packageExports(manifest, metadata) {
const exports = metadata.exports
if (!exports || typeof exports !== 'object' || Array.isArray(exports)) {
return []
}
const entries = Object.keys(exports).some((key) => key.startsWith('.'))
? Object.entries(exports)
: [['.', exports]]
if (entries.some(([key]) => key.includes('*') || (key !== '.' && !key.startsWith('./')))) {
throw new Error(`Workspace source exports must name public entry points: ${manifest}`)
}
return entries
}
export function createWorkspaceSourceResolver(root = process.cwd()) {
root = realpathSync(root)
const packages = new Map()
const sources = new Map()
for (const manifest of workspacePackageManifests(root)) {
const metadata = JSON.parse(readFileSync(join(root, manifest), 'utf8'))
if (typeof metadata.name !== 'string' || packages.has(metadata.name)) {
throw new Error(`Missing or duplicate workspace package name: ${manifest}`)
}
packages.set(metadata.name, { manifest, native: manifest.startsWith('native/') })
for (const [key, conditions] of packageExports(manifest, metadata)) {
if (
!conditions ||
typeof conditions !== 'object' ||
!Object.hasOwn(conditions, 'orca-source')
) {
continue
}
const specifier = metadata.name + (key === '.' ? '' : key.slice(1))
sources.set(specifier, sourceTarget(root, manifest, conditions['orca-source']))
}
}
return {
esbuildAliases: Object.fromEntries(
[...sources].map(([specifier, source]) => [specifier, source.path])
),
aliases: [...sources].map(([specifier, source]) => ({
find: new RegExp(`^${specifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`),
replacement: source.path
})),
resolve(specifier) {
const name = specifier.startsWith('@')
? specifier.split('/').slice(0, 2).join('/')
: specifier.split('/')[0]
const workspace = packages.get(name)
if (sources.has(specifier)) {
return sources.get(specifier)
}
if (workspace?.native || (!workspace && !specifier.startsWith('@orca/'))) {
return null
}
throw new Error(`No public orca-source export for ${specifier}`)
}
}
}