|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | + |
| 3 | +async function main() { |
| 4 | + const commands = [ |
| 5 | + ['node', ['echo.js', '1'.repeat(100)], { stdio: 'inherit' }], |
| 6 | + ['node', ['echo.js', '2'.repeat(100)], { stdio: 'inherit' }], |
| 7 | + ['node', ['echo.js', '3'.repeat(100)], { stdio: 'inherit' }], |
| 8 | + ['node', ['echo.js', '4'.repeat(100)], { stdio: 'inherit' }], |
| 9 | + ['node', ['echo.js', '5'.repeat(100)], { stdio: 'inherit' }], |
| 10 | + ['node', ['echo.js', '6'.repeat(100)], { stdio: 'inherit' }], |
| 11 | + ['node', ['echo.js', '7'.repeat(100)], { stdio: 'inherit' }], |
| 12 | + ['node', ['echo.js', '8'.repeat(100)], { stdio: 'inherit' }], |
| 13 | + ['node', ['echo.js', '9'.repeat(100)], { stdio: 'inherit' }], |
| 14 | + ['node', ['echo.js', '10'.repeat(100)], { stdio: 'inherit' }], |
| 15 | + ['node', ['echo.js', '11'.repeat(100)], { stdio: 'inherit' }], |
| 16 | + ['node', ['echo.js', '12'.repeat(100)], { stdio: 'inherit' }], |
| 17 | + ['node', ['echo.js', '13'.repeat(100)], { stdio: 'inherit' }], |
| 18 | + ['node', ['echo.js', '14'.repeat(100)], { stdio: 'inherit' }], |
| 19 | + ['node', ['echo.js', '15'.repeat(100)], { stdio: 'inherit' }], |
| 20 | + ['node', ['echo.js', '16'.repeat(100)], { stdio: 'inherit' }], |
| 21 | + ['node', ['echo.js', '17'.repeat(100)], { stdio: 'inherit' }], |
| 22 | + ['node', ['echo.js', '18'.repeat(100)], { stdio: 'inherit' }], |
| 23 | + ['node', ['echo.js', '19'.repeat(100)], { stdio: 'inherit' }], |
| 24 | + ['node', ['echo.js', '20'.repeat(100)], { stdio: 'inherit' }], |
| 25 | + ['node', ['echo.js', '21'.repeat(100)], { stdio: 'inherit' }], |
| 26 | + ['node', ['echo.js', '22'.repeat(100)], { stdio: 'inherit' }], |
| 27 | + ['node', ['echo.js', '23'.repeat(100)], { stdio: 'inherit' }], |
| 28 | + ['node', ['echo.js', '24'.repeat(100)], { stdio: 'inherit' }], |
| 29 | + ['node', ['echo.js', '25'.repeat(100)], { stdio: 'inherit' }], |
| 30 | + ['node', ['echo.js', '26'.repeat(100)], { stdio: 'inherit' }], |
| 31 | + ['node', ['echo.js', '27'.repeat(100)], { stdio: 'inherit' }], |
| 32 | + ['node', ['echo.js', '28'.repeat(100)], { stdio: 'inherit' }], |
| 33 | + ['node', ['echo.js', '29'.repeat(100)], { stdio: 'inherit' }], |
| 34 | + ['node', ['echo.js', '30'.repeat(100)], { stdio: 'inherit' }], |
| 35 | + ]; |
| 36 | + |
| 37 | + console.log('[build.js] --------------------------------'); |
| 38 | + console.log('[build.js] start'); |
| 39 | + |
| 40 | + await Promise.all(commands.map(cmd => exec(...cmd))); |
| 41 | + // const r = await exec('node', ['echo.js', '4'.repeat(100)], { stdio: 'inherit' }); |
| 42 | + // console.log(r); |
| 43 | + |
| 44 | + console.log('[build.js] main process end'); |
| 45 | +} |
| 46 | + |
| 47 | +main().catch(console.error); |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {string} command |
| 51 | + * @param {ReadonlyArray<string>} args |
| 52 | + * @param {object} [options] |
| 53 | + */ |
| 54 | +export async function exec(command, args, options) { |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + const _process = spawn(command, args, { |
| 57 | + stdio: [ |
| 58 | + 'ignore', // stdin |
| 59 | + 'pipe', // stdout |
| 60 | + 'pipe', // stderr |
| 61 | + ], |
| 62 | + ...options, |
| 63 | + shell: process.platform === 'win32', |
| 64 | + }); |
| 65 | + |
| 66 | + /** |
| 67 | + * @type {Buffer[]} |
| 68 | + */ |
| 69 | + const stderrChunks = []; |
| 70 | + /** |
| 71 | + * @type {Buffer[]} |
| 72 | + */ |
| 73 | + const stdoutChunks = []; |
| 74 | + |
| 75 | + _process.stderr?.on('data', chunk => { |
| 76 | + stderrChunks.push(chunk); |
| 77 | + }); |
| 78 | + |
| 79 | + _process.stdout?.on('data', chunk => { |
| 80 | + stdoutChunks.push(chunk); |
| 81 | + }); |
| 82 | + |
| 83 | + _process.on('error', error => { |
| 84 | + reject(error); |
| 85 | + }); |
| 86 | + |
| 87 | + _process.on('exit', code => { |
| 88 | + const ok = code === 0; |
| 89 | + const stderr = Buffer.concat(stderrChunks).toString().trim(); |
| 90 | + const stdout = Buffer.concat(stdoutChunks).toString().trim(); |
| 91 | + |
| 92 | + if (ok) { |
| 93 | + const result = { ok, code, stderr, stdout }; |
| 94 | + resolve(result); |
| 95 | + } else { |
| 96 | + reject( |
| 97 | + new Error( |
| 98 | + `Failed to execute command: ${command} ${args.join(' ')}: ${stderr}`, |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | +} |
0 commit comments