1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.printErrorAndExit = printErrorAndExit;
|
7 | exports.executeFinally = executeFinally;
|
8 | exports.orNullIfFileNotExist = orNullIfFileNotExist;
|
9 | exports.orIfFileNotExist = orIfFileNotExist;
|
10 | exports.NestedError = void 0;
|
11 |
|
12 | function _chalk() {
|
13 | const data = _interopRequireDefault(require("chalk"));
|
14 |
|
15 | _chalk = function () {
|
16 | return data;
|
17 | };
|
18 |
|
19 | return data;
|
20 | }
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 | function printErrorAndExit(error) {
|
25 | console.error(_chalk().default.red((error.stack || error).toString()));
|
26 | process.exit(1);
|
27 | }
|
28 |
|
29 |
|
30 | async function executeFinally(promise, task) {
|
31 | let result = null;
|
32 |
|
33 | try {
|
34 | result = await promise;
|
35 | } catch (originalError) {
|
36 | try {
|
37 | await task(true);
|
38 | } catch (taskError) {
|
39 | throw new NestedError([originalError, taskError]);
|
40 | }
|
41 |
|
42 | throw originalError;
|
43 | }
|
44 |
|
45 | await task(false);
|
46 | return result;
|
47 | }
|
48 |
|
49 | class NestedError extends Error {
|
50 | constructor(errors, message = "Compound error: ") {
|
51 | let m = message;
|
52 | let i = 1;
|
53 |
|
54 | for (const error of errors) {
|
55 | const prefix = `Error #${i++} `;
|
56 | m += "\n\n" + prefix + "-".repeat(80) + "\n" + error.stack;
|
57 | }
|
58 |
|
59 | super(m);
|
60 | }
|
61 |
|
62 | }
|
63 |
|
64 | exports.NestedError = NestedError;
|
65 |
|
66 | function orNullIfFileNotExist(promise) {
|
67 | return orIfFileNotExist(promise, null);
|
68 | }
|
69 |
|
70 | function orIfFileNotExist(promise, fallbackValue) {
|
71 | return promise.catch(e => {
|
72 | if (e.code === "ENOENT" || e.code === "ENOTDIR") {
|
73 | return fallbackValue;
|
74 | }
|
75 |
|
76 | throw e;
|
77 | });
|
78 | }
|
79 |
|
80 |
|
\ | No newline at end of file |