import invariant from 'tiny-invariant' ; import { isMainThread } from 'worker_threads' ; import { ServeAndBuildChecker , BuildInCheckerNames } from './types' ; import { createScript , Script } from './worker' ; import type { } from 'vite' ; import type { CreateDiagnostic , BuildInCheckers } from './types' ; if ( ! isMainThread ) { process . stdout . isTTY = true ; } export interface CheckerMeta < T extends BuildInCheckerNames > { name : string ; absFilePath : string ; createDiagnostic : CreateDiagnostic < T > ; build : ServeAndBuildChecker [ 'build' ] ; script ? : Script < any > ; } export abstract class Checker < T extends BuildInCheckerNames > implements CheckerMeta < T > { public static logger : ( ( ... args : any [ ] ) => void ) [ ] = [ ( ... args ) => { console . log ( args [ 0 ] . payload ) ; } , ] ; public static log ( ... args ) { this . logger . forEach ( ( fn ) => fn ( ... args ) ) ; } public name : string ; public absFilePath : string ; public createDiagnostic : CreateDiagnostic < T > ; public build : ServeAndBuildChecker [ 'build' ] ; public script ? : Script < any > ; public constructor ( { name , absFilePath , createDiagnostic , build } ) { this . name = name ; this . absFilePath = absFilePath ; this . build = build ; this . createDiagnostic = createDiagnostic ; this . build = build ; } public prepare ( ) { const script = createScript < Pick < BuildInCheckers , T > > ( { absFilename : this . absFilePath , buildBin : this . build . buildBin , serverChecker : { createDiagnostic : this . createDiagnostic } , } ) ! ; this . script = script ; return script ; } public initMainThread ( ) { invariant ( this . script , `script should be created in 'prepare', but got ${ this . script }` ) ; if ( isMainThread ) { const createServeAndBuild = this . script . mainScript ( ) ; return createServeAndBuild ; } } public initWorkerThread ( ) { invariant ( this . script , `script should be created in 'prepare', but got ${ this . script }` ) ; if ( ! isMainThread ) { this . script . workerScript ( ) ; } } }