diff --git a/packages/build/src/npm-packages.spec.ts b/packages/build/src/npm-packages.spec.ts index b41ed44a8..4e33789f6 100644 --- a/packages/build/src/npm-packages.spec.ts +++ b/packages/build/src/npm-packages.spec.ts @@ -36,6 +36,17 @@ describe('npm-packages', () => { } expect.fail('Expected error'); }); + + it('ignores errors when asked to for ENOENT', () => { + const result = spawnSync('notaprogram', [], { encoding: 'utf8' }, true); + expect(result).to.not.be.undefined; + }); + + it('ignores errors when asked to for non-zero exit code', () => { + const result = spawnSync('bash', ['-c', 'exit 1'], { encoding: 'utf8' }, true); + expect(result).to.not.be.undefined; + expect(result?.status).to.equal(1); + }); }); describe('bumpNpmPackages', () => { diff --git a/packages/build/src/npm-packages.ts b/packages/build/src/npm-packages.ts index a2837a45e..f8746bc98 100644 --- a/packages/build/src/npm-packages.ts +++ b/packages/build/src/npm-packages.ts @@ -13,18 +13,30 @@ export interface LernaPackageDescription { location: string; } -export function spawnSync(command: string, args: string[], options: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns { +export function spawnSync(command: string, args: string[], options: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns; +export function spawnSync(command: string, args: string[], options: SpawnSyncOptionsWithStringEncoding, ignoreErrors: false): SpawnSyncReturns; +export function spawnSync(command: string, args: string[], options: SpawnSyncOptionsWithStringEncoding, ignoreErrors: true): SpawnSyncReturns | undefined; +export function spawnSync(command: string, args: string[], options: SpawnSyncOptionsWithStringEncoding, ignoreErrors = false): SpawnSyncReturns | undefined { const result = spawn.sync(command, args, options); if (result.error) { console.error('spawn.sync returned error', result.error); console.error(result.stdout); console.error(result.stderr); - throw new Error(`Failed to spawn ${command}, args: ${args.join(',')}: ${result.error}`); + + if (!ignoreErrors) { + throw new Error(`Failed to spawn ${command}, args: ${args.join(',')}: ${result.error}`); + } else { + console.warn('Ignoring error and continuing...'); + } } else if (result.status !== 0) { console.error('spawn.sync exited with non-zero', result.status); console.error(result.stdout); console.error(result.stderr); - throw new Error(`Spawn exited non-zero for ${command}, args: ${args.join(',')}: ${result.status}`); + if (!ignoreErrors) { + throw new Error(`Spawn exited non-zero for ${command}, args: ${args.join(',')}: ${result.status}`); + } else { + console.warn('Ignoring error and continuing...'); + } } return result; } @@ -138,7 +150,7 @@ export function markBumpedFilesAsAssumeUnchanged( stdio: 'inherit', cwd: PROJECT_ROOT, encoding: 'utf8' - }); + }, true); console.info(`File ${f} is now ${assumeUnchanged ? '' : 'NOT '}assumed to be unchanged`); }); }