diff --git a/build/lib/spawn.js b/build/lib/spawn.js new file mode 100644 index 00000000..e0483a5d --- /dev/null +++ b/build/lib/spawn.js @@ -0,0 +1,16 @@ +import { spawn } from 'child_process'; + +const spawns = new Set(); + +export function spawnAndCheck(cmd, args, options) { + const s = spawn(cmd, args, options); + spawns.add(s); + s.on('close', (code) => { + spawns.delete(s); + if (code !== 0) { + console.error(cmd, 'exited with code:', code); + [...spawns].forEach((s) => s.kill()); + process.exit(code); + } + }); +} diff --git a/build/tools/build.js b/build/tools/build.js index 2996824e..a8cd4813 100644 --- a/build/tools/build.js +++ b/build/tools/build.js @@ -1,14 +1,14 @@ -import { spawn } from 'child_process'; import { mkdirSync } from 'fs'; +import { spawnAndCheck } from '../lib/spawn.js'; mkdirSync('out', { recursive: true }); -spawn('node', ['build/tools/copy.js'], { +spawnAndCheck('node', ['build/tools/copy.js'], { shell: true, stdio: 'inherit', }); -spawn('./node_modules/.bin/rollup', ['-c'], { +spawnAndCheck('./node_modules/.bin/rollup', ['-c'], { shell: true, stdio: 'inherit', }); diff --git a/build/tools/serve.js b/build/tools/serve.js index 0e7d1a53..1db73bee 100644 --- a/build/tools/serve.js +++ b/build/tools/serve.js @@ -1,23 +1,8 @@ -import { spawn } from 'child_process'; import { mkdirSync } from 'fs'; +import { spawnAndCheck } from '../lib/spawn.js'; mkdirSync('out', { recursive: true }); -const spawns = new Set(); - -function spawnAndCheck(cmd, args, options) { - const s = spawn(cmd, args, options); - spawns.add(s); - s.on('close', (code) => { - spawns.delete(s); - if (code !== 0) { - console.error(cmd, 'exited with code:', code); - [...spawns].forEach((s) => s.kill()); - process.exit(code); - } - }); -} - spawnAndCheck('npm', ['run', 'watch'], { shell: true, stdio: 'inherit',