|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import { mkdir, mkdtemp, rm } from 'node:fs/promises'; |
| 3 | +import { dirname, resolve } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { generateFixtures } from './generate-fixtures.mjs'; |
| 6 | + |
| 7 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| 8 | +const generatedTempRoot = resolve(repoRoot, 'fixtures/generated/tmp'); |
| 9 | +const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'; |
| 10 | + |
| 11 | +function usage() { |
| 12 | + return `Usage: |
| 13 | + node scripts/run-fixture-command.mjs [--build <workspace>]... -- <command> [args...] |
| 14 | +`; |
| 15 | +} |
| 16 | + |
| 17 | +function parseArgs(argv) { |
| 18 | + const builds = []; |
| 19 | + const args = [...argv]; |
| 20 | + |
| 21 | + while (args.length > 0) { |
| 22 | + const arg = args.shift(); |
| 23 | + if (arg === '--help' || arg === '-h') { |
| 24 | + return { builds, command: [], help: true }; |
| 25 | + } |
| 26 | + |
| 27 | + if (arg === '--') { |
| 28 | + return { builds, command: args }; |
| 29 | + } |
| 30 | + |
| 31 | + if (arg !== '--build') { |
| 32 | + throw new Error(`Unknown option: ${arg}`); |
| 33 | + } |
| 34 | + |
| 35 | + const workspace = args.shift(); |
| 36 | + if (!workspace || workspace.startsWith('--')) { |
| 37 | + throw new Error('--build requires a workspace name'); |
| 38 | + } |
| 39 | + builds.push(workspace); |
| 40 | + } |
| 41 | + |
| 42 | + throw new Error('Missing command after --'); |
| 43 | +} |
| 44 | + |
| 45 | +async function run(command, args, options = {}) { |
| 46 | + const child = spawn(command, args, { |
| 47 | + cwd: options.cwd ?? process.cwd(), |
| 48 | + env: options.env ?? process.env, |
| 49 | + stdio: 'inherit', |
| 50 | + }); |
| 51 | + |
| 52 | + const code = await new Promise((resolveCode, reject) => { |
| 53 | + child.on('error', reject); |
| 54 | + child.on('close', resolveCode); |
| 55 | + }); |
| 56 | + |
| 57 | + if (code !== 0) { |
| 58 | + throw new Error(`${command} ${args.join(' ')} exited with code ${code}`); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +const { builds, command, help } = parseArgs(process.argv.slice(2)); |
| 63 | +if (help) { |
| 64 | + console.log(usage().trimEnd()); |
| 65 | + process.exit(0); |
| 66 | +} |
| 67 | + |
| 68 | +if (command.length === 0) { |
| 69 | + throw new Error('Missing command after --'); |
| 70 | +} |
| 71 | + |
| 72 | +await mkdir(generatedTempRoot, { recursive: true }); |
| 73 | +const tempRoot = await mkdtemp(resolve(generatedTempRoot, 'test-')); |
| 74 | +const env = { |
| 75 | + ...process.env, |
| 76 | + MRTDOWN_FIXTURE_DATA_DIR: resolve(tempRoot, 'data'), |
| 77 | + MRTDOWN_FIXTURE_META_PATH: resolve(tempRoot, 'meta.json'), |
| 78 | +}; |
| 79 | + |
| 80 | +try { |
| 81 | + await generateFixtures({ |
| 82 | + dataDir: env.MRTDOWN_FIXTURE_DATA_DIR, |
| 83 | + metaPath: env.MRTDOWN_FIXTURE_META_PATH, |
| 84 | + }); |
| 85 | + |
| 86 | + for (const workspace of builds) { |
| 87 | + await run(npmCommand, ['--workspace', workspace, 'run', 'build'], { |
| 88 | + cwd: repoRoot, |
| 89 | + env, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + const executable = command[0] === 'npm' ? npmCommand : command[0]; |
| 94 | + await run(executable, command.slice(1), { env }); |
| 95 | +} finally { |
| 96 | + await rm(tempRoot, { recursive: true, force: true }); |
| 97 | +} |
0 commit comments