Skip to content

Commit a833d40

Browse files
authored
Merge pull request #5 from StatelessStudio/v2.0.0
[2.0.0]
2 parents 1250f8b + 93853ca commit a833d40

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-22
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# ts-async-bootstrap
22

3+
## [2.0.0]
4+
5+
### Breaking Changes
6+
7+
- [Issue #3] shouldExit option should not cause the app to exit if there are pending intervals/timeouts
8+
9+
### Additions
10+
11+
### Fixes
12+
13+
314
## [1.0.0]
415

516
Initial Release

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ Next, your run function is called. This could be the main function of the applic
4949

5050
### Exit
5151

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

5454
### Error
5555

5656
If an exception is thrown or a promise is rejected during register or run, the errorHandler function will be called.
57-
- If `shouldExit` is true (default), the application will exit with a non-zero exit code
57+
- If `shouldExitOnError` is true (default), the application will exit with a non-zero exit code
5858
- If `errorHandler` is not set, `console.error` will be used to log the error
5959
- If the error occured in the register function, the run function will not be called

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-async-bootstrap",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"ts-project-version": "1.2.4",
55
"private": "true",
66
"scripts": {

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ export interface BootstrapOptions {
1212
// Entrypoint
1313
run: BootstrapFunction;
1414

15-
// Should exit after program completes?
16-
shouldExit?: boolean;
15+
// Callback to run on successs
16+
onComplete?: () => void;
17+
18+
// Should exit after unhandled exception?
19+
shouldExitOnError?: boolean;
1720

1821
// (Optional) Error handling function for uncaught errors
1922
errorHandler?: ErrorHandlerFunction;
@@ -25,7 +28,8 @@ export interface BootstrapOptions {
2528
export const defaultOptions: BootstrapOptions = {
2629
register: null,
2730
run: null,
28-
shouldExit: true,
31+
onComplete: () => { },
32+
shouldExitOnError: true,
2933
errorHandler: console.error
3034
};
3135

@@ -41,16 +45,12 @@ export function bootstrap(options: BootstrapOptions): void {
4145
.then(async () => {
4246
return await Promise.resolve(options.run());
4347
})
44-
.then((returned) => {
45-
if (options.shouldExit) {
46-
process.exit(returned ? returned : 0);
47-
}
48-
})
48+
.then(options.onComplete)
4949
.catch(async e => {
5050
// Handle or log the error
5151
await Promise.resolve(options.errorHandler(e));
5252

53-
if (options.shouldExit) {
53+
if (options.shouldExitOnError) {
5454
process.exit(e.code ?? 1);
5555
}
5656
});
@@ -71,7 +71,7 @@ export async function bootstrapPromise(
7171
register: register,
7272
run: async () => accept(),
7373
errorHandler: async (...e) => reject(...e),
74-
shouldExit: false,
74+
shouldExitOnError: false,
7575
});
7676
});
7777
}

test/index.spec.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ describe('BootstrapOptions', () => {
2020

2121
it('accepts an option to exit after completion', () => {
2222
const options: Partial<BootstrapOptions> = {
23-
shouldExit: true
23+
shouldExitOnError: true
2424
};
2525

26-
expect(options.shouldExit).toBeTrue();
26+
expect(options.shouldExitOnError).toBeTrue();
2727
});
2828

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

4242
bootstrap({
43-
shouldExit: false,
43+
shouldExitOnError: false,
4444
register: async () => (new Promise((accept) => {
4545
setTimeout(() => {
4646
registered = true;
@@ -53,7 +53,7 @@ describe('bootstrap', () => {
5353

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

6565
it('runs the error handler if an error is thrown from run()', () => {
6666
bootstrap({
67-
shouldExit: false,
67+
shouldExitOnError: false,
6868
register: async () => {},
6969
run: async () => {
7070
throw new Error('test');
@@ -76,7 +76,7 @@ describe('bootstrap', () => {
7676
it('can run without a register function', async () => {
7777
const returned = await new Promise<string>(accept => {
7878
bootstrap({
79-
shouldExit: false,
79+
shouldExitOnError: false,
8080
run: async () => {
8181
accept('done');
8282
}
@@ -89,7 +89,7 @@ describe('bootstrap', () => {
8989
it('can run with a synchronous register function', async () => {
9090
const returned = await new Promise<string>(accept => {
9191
bootstrap({
92-
shouldExit: false,
92+
shouldExitOnError: false,
9393
register: async () => {},
9494
run: async () => {
9595
accept('done');
@@ -100,11 +100,26 @@ describe('bootstrap', () => {
100100
expect(returned).toBe('done');
101101
});
102102

103+
it('can call a onComplete method', async () => {
104+
const returned = await new Promise<string>(accept => {
105+
bootstrap({
106+
shouldExitOnError: false,
107+
register: async () => { },
108+
run: async () => { },
109+
onComplete: async () => {
110+
accept('done');
111+
}
112+
});
113+
});
114+
115+
expect(returned).toBe('done');
116+
});
117+
103118
it('logs to console if no error handler is provided', async () => {
104119
spyOn(console, 'error');
105120

106121
bootstrap({
107-
shouldExit: false,
122+
shouldExitOnError: false,
108123
register: async () => {
109124
throw new Error('test');
110125
},

0 commit comments

Comments
 (0)