@naturalcycles/nodejs-lib 12.64.3 → 12.65.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,5 +26,7 @@ export interface RunScriptOptions {
26
26
  * - No need to add `.catch(err => { console.error(err); process.exit(1) })`
27
27
  *
28
28
  * This function is kept light, dependency-free, exported separately.
29
+ *
30
+ * Set env DEBUG_RUN_SCRIPT for extra debugging.
29
31
  */
30
32
  export declare function runScript(fn: (...args: any[]) => any, opt?: RunScriptOptions): void;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runScript = void 0;
4
+ const { DEBUG_RUN_SCRIPT } = process.env;
4
5
  /**
5
6
  * Use it in your top-level scripts like this:
6
7
  *
@@ -16,6 +17,8 @@ exports.runScript = void 0;
16
17
  * - No need to add `.catch(err => { console.error(err); process.exit(1) })`
17
18
  *
18
19
  * This function is kept light, dependency-free, exported separately.
20
+ *
21
+ * Set env DEBUG_RUN_SCRIPT for extra debugging.
19
22
  */
20
23
  function runScript(fn, opt = {}) {
21
24
  const { logger = console, noExit } = opt;
@@ -25,9 +28,17 @@ function runScript(fn, opt = {}) {
25
28
  process.on('unhandledRejection', err => {
26
29
  logger.error('unhandledRejection:', err);
27
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
+ // fake timeout, to ensure node.js process won't exit until runScript main promise is resolved
36
+ const timeout = setTimeout(() => { }, 10000000);
28
37
  void (async () => {
29
38
  try {
30
39
  await fn();
40
+ if (DEBUG_RUN_SCRIPT)
41
+ logger.log(`runScript promise resolved`);
31
42
  if (!noExit) {
32
43
  setImmediate(() => process.exit(0));
33
44
  }
@@ -39,6 +50,9 @@ function runScript(fn, opt = {}) {
39
50
  setImmediate(() => process.exit(1));
40
51
  }
41
52
  }
53
+ finally {
54
+ clearTimeout(timeout);
55
+ }
42
56
  })();
43
57
  }
44
58
  exports.runScript = runScript;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.64.3",
3
+ "version": "12.65.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -14,6 +14,8 @@ export interface RunScriptOptions {
14
14
  logger?: CommonLogger
15
15
  }
16
16
 
17
+ const { DEBUG_RUN_SCRIPT } = process.env
18
+
17
19
  /**
18
20
  * Use it in your top-level scripts like this:
19
21
  *
@@ -29,6 +31,8 @@ export interface RunScriptOptions {
29
31
  * - No need to add `.catch(err => { console.error(err); process.exit(1) })`
30
32
  *
31
33
  * This function is kept light, dependency-free, exported separately.
34
+ *
35
+ * Set env DEBUG_RUN_SCRIPT for extra debugging.
32
36
  */
33
37
  export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {}): void {
34
38
  const { logger = console, noExit } = opt
@@ -40,10 +44,20 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
40
44
  logger.error('unhandledRejection:', err)
41
45
  })
42
46
 
47
+ if (DEBUG_RUN_SCRIPT) {
48
+ process.on('exit', code => logger.log(`process.exit event, code=${code}`))
49
+ process.on('beforeExit', code => logger.log(`process.beforeExit event, code=${code}`))
50
+ }
51
+
52
+ // fake timeout, to ensure node.js process won't exit until runScript main promise is resolved
53
+ const timeout = setTimeout(() => {}, 10000000)
54
+
43
55
  void (async () => {
44
56
  try {
45
57
  await fn()
46
58
 
59
+ if (DEBUG_RUN_SCRIPT) logger.log(`runScript promise resolved`)
60
+
47
61
  if (!noExit) {
48
62
  setImmediate(() => process.exit(0))
49
63
  }
@@ -53,6 +67,8 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
53
67
  if (!noExit) {
54
68
  setImmediate(() => process.exit(1))
55
69
  }
70
+ } finally {
71
+ clearTimeout(timeout)
56
72
  }
57
73
  })()
58
74
  }