UNPKG

1.76 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.printErrorAndExit = printErrorAndExit;
7exports.executeFinally = executeFinally;
8exports.orNullIfFileNotExist = orNullIfFileNotExist;
9exports.orIfFileNotExist = orIfFileNotExist;
10exports.NestedError = void 0;
11
12function _chalk() {
13 const data = _interopRequireDefault(require("chalk"));
14
15 _chalk = function () {
16 return data;
17 };
18
19 return data;
20}
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24function printErrorAndExit(error) {
25 console.error(_chalk().default.red((error.stack || error).toString()));
26 process.exit(1);
27} // you don't need to handle error in your task - it is passed only indicate status of promise
28
29
30async 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
49class 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
64exports.NestedError = NestedError;
65
66function orNullIfFileNotExist(promise) {
67 return orIfFileNotExist(promise, null);
68}
69
70function 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// __ts-babel@6.0.4
80//# sourceMappingURL=promise.js.map
\No newline at end of file