UNPKG

@ipp/cli

Version:

An image build orchestrator for the modern web

61 lines (60 loc) 2.49 kB
"use strict"; /** * 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. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = void 0; const chalk_1 = __importDefault(require("chalk")); const process_1 = require("process"); const cli_1 = require("../cli"); const constants_1 = require("../constants"); const exception_1 = require("../lib/exception"); const utils_1 = require("../lib/utils"); const ui_1 = require("../ui"); const args_1 = require("./args"); const config_1 = require("./config"); async function init(concurrency) { try { const { args, config } = await load(concurrency); const ui = args.text ? ui_1.TextUi : void 0; await (0, cli_1.startCli)(config, ui); } catch (err) { process_1.stdout.write(err instanceof Array ? err.map(formatError).join("") : formatError(err)); process_1.stdout.write("\n" + (0, utils_1.pad)(chalk_1.default.grey("Learn more at " + constants_1.REPOSITORY_SHORT)) + "\n\n"); process.exitCode = 1; } } exports.init = init; /** Parses CLI flags and loads the configuration */ async function load(concurrency) { const args = await (0, args_1.parseArgs)(); (0, args_1.validateArgs)(args); const initialConfig = { concurrency, }; if (args.input) initialConfig.input = args.input; if (args.output) initialConfig.output = args.output; const config = await (0, config_1.getConfig)(initialConfig, args.config); return { args, config }; } function formatError(err) { if (err instanceof exception_1.CliException) { const { title, comment } = err; const heading = "\n" + chalk_1.default.red.bold(`${utils_1.BULLET} ${title ? title : "CLI Exception"}`) + "\n\n"; const body = comment ? (0, utils_1.pad)(chalk_1.default.red(comment)) + "\n\n" : ""; return (0, utils_1.pad)(heading + body); } const error = chalk_1.default.red(err instanceof Error ? (0, utils_1.prettifyError)(err) : "A non-Error like object was thrown") + "\n"; const message = "This should not have happened.\nIf you feel that this was in error, consider opening a new issue\n"; return (0, utils_1.pad)(error + message); }