diff --git a/packages/cli/src/bin/dev-core.ts b/packages/cli/src/bin/dev-core.ts index 6b5d8c3478..a6fedfc057 100644 --- a/packages/cli/src/bin/dev-core.ts +++ b/packages/cli/src/bin/dev-core.ts @@ -1,3 +1,4 @@ +import type { ChildProcess } from 'node:child_process'; import { spawn } from 'node:child_process'; import { existsSync } from 'node:fs'; import path from 'node:path'; @@ -93,22 +94,72 @@ flags.channel = buildFlags.channel as any; flags.coverage = buildFlags.coverage; watchI18N(); -spawn( - 'node', - [ - '--loader', - 'ts-node/esm/transpile-only', - '../../node_modules/webpack/bin/webpack.js', - flags.mode === 'development' ? 'serve' : undefined, - '--mode', - flags.mode === 'development' ? 'development' : 'production', - '--env', - 'flags=' + Buffer.from(JSON.stringify(flags), 'utf-8').toString('hex'), - ].filter((v): v is string => !!v), - { - cwd, - stdio: 'inherit', - shell: true, - env: process.env, - } -); + +function awaitChildProcess(child: ChildProcess): Promise { + return new Promise((resolve, reject) => { + const handleExitCode = (code: number | null) => { + if (code) { + reject( + new Error( + `Child process at ${ + (child as any).cwd + } fails: ${child.spawnargs.join(' ')}` + ) + ); + } else { + resolve(0); + } + }; + + child.on('error', () => handleExitCode(child.exitCode)); + child.on('exit', code => handleExitCode(code)); + }); +} + +try { + // Build:infra + await awaitChildProcess( + spawn('yarn', ['build:infra'], { + cwd, + stdio: 'inherit', + shell: true, + env: process.env, + }) + ); + + // Build:plugins + await awaitChildProcess( + spawn('yarn', ['build:plugins'], { + cwd, + stdio: 'inherit', + shell: true, + env: process.env, + }) + ); + + // Start webpack + await awaitChildProcess( + spawn( + 'node', + [ + '--loader', + 'ts-node/esm/transpile-only', + '../../node_modules/webpack/bin/webpack.js', + flags.mode === 'development' ? 'serve' : undefined, + '--mode', + flags.mode === 'development' ? 'development' : 'production', + '--env', + 'flags=' + Buffer.from(JSON.stringify(flags), 'utf-8').toString('hex'), + ].filter((v): v is string => !!v), + { + cwd, + stdio: 'inherit', + shell: true, + env: process.env, + } + ) + ); +} catch (error) { + console.error('Error during build:', error); + process.exit(1); +}