mirror of
https://github.com/SukkaW/Surge.git
synced 2026-04-30 01:46:57 +08:00
Refactor: use jest-worker
This commit is contained in:
28
Build/lib/worker.ts
Normal file
28
Build/lib/worker.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import process from 'node:process';
|
||||
import type { JestWorkerFarm } from 'jest-worker';
|
||||
import { Worker as JestWorker } from 'jest-worker';
|
||||
|
||||
const sharedWorkerOptions = {
|
||||
numWorkers: 1,
|
||||
enableWorkerThreads: true,
|
||||
forkOptions: {
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_OPTIONS: process.env.NODE_OPTIONS
|
||||
}
|
||||
}
|
||||
} satisfies ConstructorParameters<typeof JestWorker>[1];
|
||||
|
||||
export function createWorker<T extends Record<string, unknown>>(workerPath: string) {
|
||||
return <const K extends ReadonlyArray<keyof T & string>>(exposedMethods: K): JestWorkerFarm<Pick<T, K[number]>> => {
|
||||
const worker = new JestWorker(workerPath, {
|
||||
...sharedWorkerOptions,
|
||||
exposedMethods
|
||||
}) as JestWorkerFarm<Pick<T, K[number]>>;
|
||||
|
||||
worker.getStdout().pipe(process.stdout);
|
||||
worker.getStderr().pipe(process.stderr);
|
||||
|
||||
return worker;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user