|
| 1 | +import { |
| 2 | + Worker, |
| 3 | + isMainThread, |
| 4 | + parentPort, |
| 5 | + workerData, |
| 6 | +} from 'node:worker_threads'; |
| 7 | +import type { Report, Target } from '../types'; |
| 8 | +import * as logger from './logger'; |
| 9 | +import commonjs from '../targets/commonjs'; |
| 10 | +import module from '../targets/module'; |
| 11 | +import typescript from '../targets/typescript'; |
| 12 | +import codegen from '../targets/codegen'; |
| 13 | +import custom from '../targets/custom'; |
| 14 | + |
| 15 | +type WorkerData<T extends Target> = { |
| 16 | + target: T; |
| 17 | + data: Omit<Parameters<(typeof targets)[T]>[0], 'report'>; |
| 18 | +}; |
| 19 | + |
| 20 | +const targets = { |
| 21 | + commonjs, |
| 22 | + module, |
| 23 | + typescript, |
| 24 | + codegen, |
| 25 | + custom, |
| 26 | +} as const; |
| 27 | + |
| 28 | +export const run = async <T extends Target>( |
| 29 | + target: T, |
| 30 | + data: Omit<Parameters<(typeof targets)[T]>[0], 'report'> |
| 31 | +) => { |
| 32 | + if (!isMainThread) { |
| 33 | + throw new Error('Worker can only be run from the main thread'); |
| 34 | + } |
| 35 | + |
| 36 | + const worker = new Worker(__filename, { |
| 37 | + workerData: { |
| 38 | + target, |
| 39 | + data, |
| 40 | + } satisfies WorkerData<T>, |
| 41 | + env: { |
| 42 | + ...process.env, |
| 43 | + FORCE_COLOR: process.stdout.isTTY ? '1' : '0', |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const report = logger.grouped(target); |
| 48 | + |
| 49 | + worker.on('message', (message) => { |
| 50 | + switch (message.type) { |
| 51 | + case 'info': |
| 52 | + report.info(message.message); |
| 53 | + break; |
| 54 | + case 'warn': |
| 55 | + report.warn(message.message); |
| 56 | + break; |
| 57 | + case 'error': |
| 58 | + report.error(message.message); |
| 59 | + break; |
| 60 | + case 'success': |
| 61 | + report.success(message.message); |
| 62 | + break; |
| 63 | + default: |
| 64 | + console.error('Unknown message type', message); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + worker.on('error', (error) => { |
| 69 | + console.log(error); |
| 70 | + }); |
| 71 | + |
| 72 | + worker.on('exit', (code) => { |
| 73 | + if (code !== 0) { |
| 74 | + report.error(`Failed to build ${target} with code ${code}`); |
| 75 | + } |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +if (!isMainThread) { |
| 80 | + const { target, data } = workerData as WorkerData<Target>; |
| 81 | + |
| 82 | + const report: Report = { |
| 83 | + info: (message) => parentPort?.postMessage({ type: 'info', message }), |
| 84 | + warn: (message) => parentPort?.postMessage({ type: 'warn', message }), |
| 85 | + error: (message) => parentPort?.postMessage({ type: 'error', message }), |
| 86 | + success: (message) => parentPort?.postMessage({ type: 'success', message }), |
| 87 | + }; |
| 88 | + |
| 89 | + if (target in targets) { |
| 90 | + // @ts-expect-error - typescript doesn't support correlated union types https://github.com/microsoft/TypeScript/issues/30581 |
| 91 | + targets[target]({ ...data, report }).catch((error) => { |
| 92 | + console.log(error); |
| 93 | + process.exit(1); |
| 94 | + }); |
| 95 | + } else { |
| 96 | + throw new Error(`Unknown target: ${target}`); |
| 97 | + } |
| 98 | +} |
0 commit comments