Skip to content

Commit

Permalink
Manually exit process
Browse files Browse the repository at this point in the history
Partial revert of broccolijs#476
to work around babel/broccoli-babel-transpiler#169
  • Loading branch information
asakusuma committed Apr 29, 2021
1 parent 64b3135 commit adadccf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
14 changes: 6 additions & 8 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
} catch (e) {
if (e instanceof CliError) {
ui.writeError(e);
process.exitCode = 1;
return;
return process.exit(1);
}

throw e;
Expand Down Expand Up @@ -197,8 +196,7 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
.action((outputDir: string, options: BuildOptions) => {
if (outputDir && options.outputPath) {
ui.writeLine('option --output-path and [target] cannot be passed at same time', 'ERROR');
process.exitCode = 1;
return;
return process.exit(1);
}

if (options.outputPath) {
Expand All @@ -220,8 +218,7 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
} catch (e) {
if (e instanceof CliError) {
ui.writeError(e);
process.exitCode = 1;
return;
return process.exit(1);
}

throw e;
Expand Down Expand Up @@ -271,10 +268,11 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
if (!process.exitCode) {
process.exitCode = 0;
}
process.exit(process.exitCode);
} catch (e) {
ui.writeLine('Cleanup error:', 'ERROR');
ui.writeError(e);
process.exitCode = 1;
process.exit(1);
}
}
})();
Expand All @@ -286,6 +284,6 @@ export = function broccoliCLI(args: string[], ui = new UI()) {
return actionPromise;
} else {
program.outputHelp();
process.exitCode = 1;
process.exit(1);
}
};
3 changes: 2 additions & 1 deletion lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ function serve(
if (!_process.exitCode) {
_process.exitCode = 0;
}
_process.exit(_process.exitCode);
})
.catch(err => {
ui.writeLine('Broccoli Cleanup error:', 'ERROR');
ui.writeError(err);
_process.exitCode = 1;
_process.exit(1);
});
}

Expand Down
12 changes: 10 additions & 2 deletions test/cli_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ function createWatcherSpy() {

describe('cli', function() {
let oldCwd = null;
let exitStub;
const EXIT_CODE = process.exitCode;

beforeEach(function() {
exitStub = sinon.stub(process, 'exit');
oldCwd = process.cwd();
process.chdir('test/fixtures/project/subdir');
});
Expand Down Expand Up @@ -86,6 +88,7 @@ describe('cli', function() {
it('closes process on completion', async function() {
await cli(['node', 'broccoli', 'build', 'dist']);
chai.expect(process.exitCode).to.eql(0);
chai.expect(exitStub).to.be.calledWith(0);
});

it('creates output folder', async function() {
Expand Down Expand Up @@ -146,7 +149,7 @@ describe('cli', function() {
chai
.expect(mockUI.errors)
.to.contain('build directory can not be the current or direct parent directory: /');
chai.expect(process.exitCode).to.eql(1);
chai.expect(exitStub).to.be.calledWith(1);
});
});

Expand All @@ -162,6 +165,7 @@ describe('cli', function() {
it('closes process on completion', async function() {
await cli(['node', 'broccoli', 'build', 'dist', '--watcher', 'polling']);
chai.expect(process.exitCode).to.eql(0);
chai.expect(exitStub).to.be.calledWith(0);
});

it('creates watcher with sane options for polling', async function() {
Expand Down Expand Up @@ -221,6 +225,7 @@ describe('cli', function() {
it('closes process on completion', async function() {
await cli(['node', 'broccoli', 'build', 'dist', '--brocfile-path', '../Brocfile.js']);
chai.expect(process.exitCode).to.eql(0);
chai.expect(exitStub).to.be.calledWith(0);
});

it('loads brocfile from a path', async function() {
Expand All @@ -243,6 +248,7 @@ describe('cli', function() {
'../../empty/Brocfile.js',
]);
chai.expect(process.exitCode).eql(0);
chai.expect(exitStub).to.be.calledWith(0);
});
});
});
Expand Down Expand Up @@ -300,12 +306,14 @@ describe('cli', function() {
it('supports `b` alias', async function() {
await cli(['node', 'broccoli', 'b']);
chai.expect(process.exitCode).to.eql(0);
chai.expect(exitStub).to.be.calledWith(0);
});

context('with param --output-path', function() {
it('closes process on completion', async function() {
await cli(['node', 'broccoli', 'build', '--output-path', 'dist']);
chai.expect(process.exitCode).to.eql(0);
chai.expect(exitStub).to.be.calledWith(0);
});

it('creates output folder', async function() {
Expand All @@ -316,7 +324,7 @@ describe('cli', function() {
context('and with [target]', function() {
it('exits with error', function() {
cli(['node', 'broccoli', 'build', 'dist', '--output-path', 'dist']);
chai.expect(process.exitCode).to.eql(1);
chai.expect(exitStub).to.be.calledWith(1);
});

it('outputs error reason to console', function() {
Expand Down

0 comments on commit adadccf

Please sign in to comment.