1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.orIfFileNotExist = exports.orNullIfFileNotExist = exports.NestedError = exports.executeFinally = exports.printErrorAndExit = void 0;
|
4 | const chalk = require("chalk");
|
5 | function printErrorAndExit(error) {
|
6 | console.error(chalk.red((error.stack || error).toString()));
|
7 | process.exit(1);
|
8 | }
|
9 | exports.printErrorAndExit = printErrorAndExit;
|
10 |
|
11 | async 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 | }
|
28 | exports.executeFinally = executeFinally;
|
29 | class 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 | }
|
40 | exports.NestedError = NestedError;
|
41 | function orNullIfFileNotExist(promise) {
|
42 | return orIfFileNotExist(promise, null);
|
43 | }
|
44 | exports.orNullIfFileNotExist = orNullIfFileNotExist;
|
45 | function 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 | }
|
53 | exports.orIfFileNotExist = orIfFileNotExist;
|
54 |
|
\ | No newline at end of file |