@ipp/cli
Version:
An image build orchestrator for the modern web
82 lines (81 loc) • 2.52 kB
JavaScript
;
/**
* Image Processing Pipeline - Copyright (c) Marcus Cemes
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.exceptionHandler = void 0;
const common_1 = require("@ipp/common");
const fs_1 = require("fs");
const path_1 = require("path");
const exception_1 = require("../lib/exception");
const map_1 = require("../lib/stream/operators/map");
const DEFAULT_ERROR_FILE = "errors.json";
function exceptionHandler(outputPath, target) {
switch (typeof target) {
case "string": {
const path = (0, path_1.resolve)(outputPath, target);
return saveExceptions(path);
}
case "function":
return mapExceptions(target);
default: {
const path = (0, path_1.resolve)(outputPath, DEFAULT_ERROR_FILE);
return saveExceptions(path);
}
}
}
exports.exceptionHandler = exceptionHandler;
function saveExceptions(path) {
let writer = null;
return (0, map_1.map)((item) => {
if (!(item instanceof common_1.Exception))
return item;
if (writer === null)
writer = createExceptionWriter(path);
writer.write(item);
return null;
}, () => writer === null || writer === void 0 ? void 0 : writer.end());
}
function createExceptionWriter(path) {
let first = true;
const stream = (0, fs_1.createWriteStream)(path);
stream.write("[");
return {
write: (exception) => {
const additional = exception instanceof exception_1.CliException
? {
title: exception.title,
code: exception.code,
comment: exception.comment,
}
: {};
if (first) {
first = false;
}
else {
stream.write(",");
}
stream.write(JSON.stringify({
name: exception.name,
message: exception.message,
...additional,
}));
},
end: () => {
stream.write("]");
stream.end();
},
};
}
function mapExceptions(callback) {
return (0, map_1.map)(async (item) => {
if (!(item instanceof common_1.Exception)) {
return item;
}
await callback(item);
return null;
});
}