@percy/cli-command 1.1.2 → 1.2.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.
Files changed (2) hide show
  1. package/dist/command.js +20 -41
  2. package/package.json +5 -5
package/dist/command.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import logger from '@percy/logger';
2
2
  import PercyConfig from '@percy/config';
3
3
  import { set, del } from '@percy/config/utils';
4
+ import { generatePromise, AbortController, AbortError } from '@percy/core/utils';
4
5
  import * as CoreConfig from '@percy/core/config';
5
6
  import * as builtInFlags from './flags.js';
6
7
  import formatHelp from './help.js';
@@ -42,45 +43,6 @@ function withBuiltIns(definition) {
42
43
  }
43
44
 
44
45
  return def;
45
- } // Calls the provided callback and handles callback cancelation when process signals are
46
- // triggered. If the provided callback returns a generator, it will automatically run and throw
47
- // an error when canceled to be able to gracefully handle it's own cancelation.
48
-
49
-
50
- async function handleProcessSignals(callback) {
51
- let signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
52
- let signalError; // keep track of signal handlers for cleanup
53
-
54
- let signalHandlers = signals.map(signal => {
55
- let handler = () => {
56
- signalError = Object.assign(new Error(signal), {
57
- canceled: true,
58
- exitCode: 0,
59
- signal
60
- });
61
- };
62
-
63
- process.on(signal, handler);
64
- return [signal, handler];
65
- });
66
-
67
- try {
68
- // maybe async function
69
- let gen = await callback(); // run any returned generator
70
-
71
- if (typeof (gen === null || gen === void 0 ? void 0 : gen.next) === 'function' && (typeof gen[Symbol.iterator] === 'function' || typeof gen[Symbol.asyncIterator] === 'function')) {
72
- let result = await gen.next();
73
-
74
- while (!result.done) {
75
- result = signalError ? await gen.throw(signalError) : await gen.next(result.value);
76
- }
77
- }
78
- } finally {
79
- // always cleanup
80
- for (let [signal, handler] of signalHandlers) {
81
- process.off(signal, handler);
82
- }
83
- }
84
46
  } // Helper to throw an error with an exit code and optional reason message
85
47
 
86
48
 
@@ -139,10 +101,27 @@ async function runCommandWithContext(parsed) {
139
101
  }
140
102
 
141
103
  });
142
- } // wrap and bind the parsed command callback
104
+ } // process signals will abort
105
+
106
+
107
+ let ctrl = new AbortController();
108
+ let signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'].map(signal => {
109
+ let handler = () => ctrl.abort(new AbortError(signal, {
110
+ signal,
111
+ exitCode: 0
112
+ }));
143
113
 
114
+ handler.off = () => process.off(signal, handler);
144
115
 
145
- await handleProcessSignals(command.callback.bind(null, context));
116
+ process.on(signal, handler);
117
+ return handler;
118
+ }); // run the command callback with context and cleanup handlers after
119
+
120
+ await generatePromise(command.callback(context), ctrl.signal, error => {
121
+ for (let handler of signals) handler.off();
122
+
123
+ if (error) throw error;
124
+ });
146
125
  } // Returns a command runner function that when run will parse provided command-line options and run
147
126
  // the parsed command callback. The returned runner will automatically output version and help
148
127
  // information when requested, and handle any thrown errors exiting when appropriate.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/cli-command",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -34,9 +34,9 @@
34
34
  "test:coverage": "yarn test --coverage"
35
35
  },
36
36
  "dependencies": {
37
- "@percy/config": "1.1.2",
38
- "@percy/core": "1.1.2",
39
- "@percy/logger": "1.1.2"
37
+ "@percy/config": "1.2.0",
38
+ "@percy/core": "1.2.0",
39
+ "@percy/logger": "1.2.0"
40
40
  },
41
- "gitHead": "1232b3fe054c7bd73879fd85ca109a723dbfb5b6"
41
+ "gitHead": "ab7652e08dab49b3efbe2968339c5a70391ed4ff"
42
42
  }