UNPKG

1.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.orIfFileNotExist = exports.orNullIfFileNotExist = exports.NestedError = exports.executeFinally = exports.printErrorAndExit = void 0;
4const chalk = require("chalk");
5function printErrorAndExit(error) {
6 console.error(chalk.red((error.stack || error).toString()));
7 process.exit(1);
8}
9exports.printErrorAndExit = printErrorAndExit;
10// you don't need to handle error in your task - it is passed only indicate status of promise
11async function executeFinally(promise, task) {
12 let result = null;
13 try {
14 result = await promise;
15 }
16 catch (originalError) {
17 try {
18 await task(true);
19 }
20 catch (taskError) {
21 throw new NestedError([originalError, taskError]);
22 }
23 throw originalError;
24 }
25 await task(false);
26 return result;
27}
28exports.executeFinally = executeFinally;
29class NestedError extends Error {
30 constructor(errors, message = "Compound error: ") {
31 let m = message;
32 let i = 1;
33 for (const error of errors) {
34 const prefix = `Error #${i++} `;
35 m += `\n\n${prefix}${"-".repeat(80)}\n${error.stack}`;
36 }
37 super(m);
38 }
39}
40exports.NestedError = NestedError;
41function orNullIfFileNotExist(promise) {
42 return orIfFileNotExist(promise, null);
43}
44exports.orNullIfFileNotExist = orNullIfFileNotExist;
45function orIfFileNotExist(promise, fallbackValue) {
46 return promise.catch(e => {
47 if (e.code === "ENOENT" || e.code === "ENOTDIR") {
48 return fallbackValue;
49 }
50 throw e;
51 });
52}
53exports.orIfFileNotExist = orIfFileNotExist;
54//# sourceMappingURL=promise.js.map
\No newline at end of file