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