| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /** Bundle the workflow guest as source data; --check rejects a stale committed module. */
- import { readFile, realpath, writeFile } from 'node:fs/promises'
- import { isBuiltin } from 'node:module'
- import { resolve } from 'node:path'
- import { parseArgs } from 'node:util'
- import { Rolldown } from 'tsdown'
- const GUEST_ENTRY = 'packages/workflow/workflow-ptc/src/guest.ts'
- const GUEST_SOURCE = 'packages/workflow/workflow-ptc/src/guest-source.ts'
- /**
- * Bundle typed guest sources without depending on workspace build outputs.
- * @param root - Repository root containing source aliases and the guest entry.
- * @returns One self-contained ESM module with only Node builtin imports.
- */
- export async function bundleWorkflowGuest(root: string): Promise<string> {
- root = await realpath(root)
- const bundle = await Rolldown.rolldown({
- cwd: root,
- input: resolve(root, GUEST_ENTRY),
- platform: 'node',
- tsconfig: resolve(root, 'tsconfig.base.json'),
- external: id => isBuiltin(id),
- // The guest imports pure helpers from package barrels, without mounting their services.
- treeshake: { moduleSideEffects: false },
- onLog(level, log, defaultHandler) {
- if (log.code === 'UNRESOLVED_IMPORT') throw new Error(`workflow guest has an unresolved import: ${log.message}`)
- defaultHandler(level, log)
- },
- })
- try {
- const { output } = await bundle.generate({ format: 'esm', codeSplitting: false, sourcemap: false })
- const chunk = output[0]
- if (output.length !== 1) {
- throw new Error('workflow guest must bundle into exactly one JavaScript module')
- }
- const external = [...chunk.imports, ...chunk.dynamicImports].filter(id => !isBuiltin(id))
- if (external.length > 0) throw new Error(`workflow guest imports files outside its module: ${external.join(', ')}`)
- return `${chunk.code.trimEnd()}\n//# sourceURL=dsh-workflow-guest.js\n`
- } finally {
- await bundle.close()
- }
- }
- /**
- * Write the guest-source module or reject a stale committed copy.
- * @param root - Repository root whose guest sources are bundled.
- * @param check - Verify without writing when true.
- */
- export async function generateWorkflowGuest(root: string, check: boolean): Promise<void> {
- const source = await bundleWorkflowGuest(root)
- const generated = [
- '/** Generated by scripts/gen-workflow-guest.ts. Do not edit directly. */',
- '',
- '/** Self-contained ESM guest transported into the mounted PTC runtime. */',
- `export const WORKFLOW_GUEST_SOURCE = ${JSON.stringify(source)}`,
- '',
- ].join('\n')
- const path = resolve(root, GUEST_SOURCE)
- if (check) {
- const current = await readFile(path, 'utf8')
- if (current !== generated) throw new Error('workflow guest source is stale; run pnpm gen-workflow-guest')
- } else {
- await writeFile(path, generated)
- }
- }
- if (import.meta.main) {
- const { values } = parseArgs({ options: { check: { type: 'boolean', default: false } } })
- await generateWorkflowGuest(resolve(import.meta.dirname, '..'), values.check)
- }
|