1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.runScript = void 0;
|
4 | const { DEBUG_RUN_SCRIPT } = process.env;
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function runScript(fn, opt = {}) {
|
24 | const { logger = console, noExit } = opt;
|
25 | process.on('uncaughtException', err => {
|
26 | logger.error('uncaughtException:', err);
|
27 | });
|
28 | process.on('unhandledRejection', err => {
|
29 | logger.error('unhandledRejection:', err);
|
30 | });
|
31 | if (DEBUG_RUN_SCRIPT) {
|
32 | process.on('exit', code => logger.log(`process.exit event, code=${code}`));
|
33 | process.on('beforeExit', code => logger.log(`process.beforeExit event, code=${code}`));
|
34 | }
|
35 |
|
36 | const timeout = setTimeout(() => { }, 10000000);
|
37 | void (async () => {
|
38 | try {
|
39 | await fn();
|
40 | if (DEBUG_RUN_SCRIPT)
|
41 | logger.log(`runScript promise resolved`);
|
42 | if (!noExit) {
|
43 | setImmediate(() => process.exit(0));
|
44 | }
|
45 | }
|
46 | catch (err) {
|
47 | logger.error('runScript error:', err);
|
48 | process.exitCode = 1;
|
49 | if (!noExit) {
|
50 | setImmediate(() => process.exit(1));
|
51 | }
|
52 | }
|
53 | finally {
|
54 | clearTimeout(timeout);
|
55 | }
|
56 | })();
|
57 | }
|
58 | exports.runScript = runScript;
|