Skip to content

Commit

Permalink
Merge pull request #5 from StatelessStudio/v2.0.0
Browse files Browse the repository at this point in the history
[2.0.0]
  • Loading branch information
DrewImm authored Feb 15, 2022
2 parents 1250f8b + 93853ca commit a833d40
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# ts-async-bootstrap

## [2.0.0]

### Breaking Changes

- [Issue #3] shouldExit option should not cause the app to exit if there are pending intervals/timeouts

### Additions

### Fixes


## [1.0.0]

Initial Release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ Next, your run function is called. This could be the main function of the applic

### Exit

After the run function completes, the application will exit (you can change this by setting `shouldExit: false`)
After the run function completes, the `onComplete` method will be called.

### Error

If an exception is thrown or a promise is rejected during register or run, the errorHandler function will be called.
- If `shouldExit` is true (default), the application will exit with a non-zero exit code
- If `shouldExitOnError` is true (default), the application will exit with a non-zero exit code
- If `errorHandler` is not set, `console.error` will be used to log the error
- If the error occured in the register function, the run function will not be called
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-async-bootstrap",
"version": "1.0.0",
"version": "2.0.0",
"ts-project-version": "1.2.4",
"private": "true",
"scripts": {
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export interface BootstrapOptions {
// Entrypoint
run: BootstrapFunction;

// Should exit after program completes?
shouldExit?: boolean;
// Callback to run on successs
onComplete?: () => void;

// Should exit after unhandled exception?
shouldExitOnError?: boolean;

// (Optional) Error handling function for uncaught errors
errorHandler?: ErrorHandlerFunction;
Expand All @@ -25,7 +28,8 @@ export interface BootstrapOptions {
export const defaultOptions: BootstrapOptions = {
register: null,
run: null,
shouldExit: true,
onComplete: () => { },
shouldExitOnError: true,
errorHandler: console.error
};

Expand All @@ -41,16 +45,12 @@ export function bootstrap(options: BootstrapOptions): void {
.then(async () => {
return await Promise.resolve(options.run());
})
.then((returned) => {
if (options.shouldExit) {
process.exit(returned ? returned : 0);
}
})
.then(options.onComplete)
.catch(async e => {
// Handle or log the error
await Promise.resolve(options.errorHandler(e));

if (options.shouldExit) {
if (options.shouldExitOnError) {
process.exit(e.code ?? 1);
}
});
Expand All @@ -71,7 +71,7 @@ export async function bootstrapPromise(
register: register,
run: async () => accept(),
errorHandler: async (...e) => reject(...e),
shouldExit: false,
shouldExitOnError: false,
});
});
}
31 changes: 23 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ describe('BootstrapOptions', () => {

it('accepts an option to exit after completion', () => {
const options: Partial<BootstrapOptions> = {
shouldExit: true
shouldExitOnError: true
};

expect(options.shouldExit).toBeTrue();
expect(options.shouldExitOnError).toBeTrue();
});

it('accepts a error callback', () => {
Expand All @@ -40,7 +40,7 @@ describe('bootstrap', () => {
let registered = null;

bootstrap({
shouldExit: false,
shouldExitOnError: false,
register: async () => (new Promise((accept) => {
setTimeout(() => {
registered = true;
Expand All @@ -53,7 +53,7 @@ describe('bootstrap', () => {

it('runs the error handler if an error is thrown from register()', () => {
bootstrap({
shouldExit: false,
shouldExitOnError: false,
register: async () => {
throw new Error('test');
},
Expand All @@ -64,7 +64,7 @@ describe('bootstrap', () => {

it('runs the error handler if an error is thrown from run()', () => {
bootstrap({
shouldExit: false,
shouldExitOnError: false,
register: async () => {},
run: async () => {
throw new Error('test');
Expand All @@ -76,7 +76,7 @@ describe('bootstrap', () => {
it('can run without a register function', async () => {
const returned = await new Promise<string>(accept => {
bootstrap({
shouldExit: false,
shouldExitOnError: false,
run: async () => {
accept('done');
}
Expand All @@ -89,7 +89,7 @@ describe('bootstrap', () => {
it('can run with a synchronous register function', async () => {
const returned = await new Promise<string>(accept => {
bootstrap({
shouldExit: false,
shouldExitOnError: false,
register: async () => {},
run: async () => {
accept('done');
Expand All @@ -100,11 +100,26 @@ describe('bootstrap', () => {
expect(returned).toBe('done');
});

it('can call a onComplete method', async () => {
const returned = await new Promise<string>(accept => {
bootstrap({
shouldExitOnError: false,
register: async () => { },
run: async () => { },
onComplete: async () => {
accept('done');
}
});
});

expect(returned).toBe('done');
});

it('logs to console if no error handler is provided', async () => {
spyOn(console, 'error');

bootstrap({
shouldExit: false,
shouldExitOnError: false,
register: async () => {
throw new Error('test');
},
Expand Down

0 comments on commit a833d40

Please sign in to comment.