UNPKG

@boost/core

Version:

Robust pipeline for creating dev tools that separate logic into routines and tasks.

87 lines (86 loc) 3.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const exit_1 = __importDefault(require("exit")); const path_1 = __importDefault(require("path")); const util_1 = __importDefault(require("util")); const common_1 = require("@boost/common"); const debug_1 = require("@boost/debug"); const ExitError_1 = __importDefault(require("./ExitError")); const Routine_1 = __importDefault(require("./Routine")); const Tool_1 = __importDefault(require("./Tool")); class Pipeline extends Routine_1.default { constructor(tool, context, options) { super('root', 'Pipeline', options); if (common_1.instanceOf(tool, Tool_1.default)) { tool.initialize(); } else { throw new TypeError('A `Tool` instance is required to operate the pipeline.'); } this.tool = tool; this.tool.debug('Instantiating pipeline'); // Child routines should start at 0 this.metadata.depth = -1; this.setContext(context); } blueprint({ func }) { return { exit: func(exit_1.default).notNullable(), }; } /** * Execute all routines in order. */ run(initialValue) { const { console: cli } = this.tool; this.tool.debug('Running pipeline'); cli.start([Array.from(this.routines), initialValue]); return this.serializeRoutines(initialValue) .then(result => { cli.stop(); process.exitCode = 0; return result; }) .catch(error => { cli.stop(error); this.reportCrash(error); if (common_1.instanceOf(error, ExitError_1.default)) { this.options.exit(error.code); } else { this.options.exit(1); } return error; }); } /** * Report the pipeline failure by writing a crash log. */ reportCrash(error) { const { config, options } = this.tool; const reporter = new debug_1.CrashReporter() .reportBinaries() .reportProcess() .reportSystem(); reporter .addSection('Tool Instance') .add('App name', options.appName) .add('App path', options.appPath) .add('Plugin types', Object.keys(this.tool.getRegisteredPlugins()).join(', ')) .add('Scoped package', options.scoped ? 'Yes' : 'No') .add('Root', options.root) .add('Config name', options.configName) .add('Package path', path_1.default.join(options.root, 'package.json')) .add('Workspaces root', options.workspaceRoot || '(Not enabled)') .add('Extending configs', config.extends.length > 0 ? util_1.default.inspect(config.extends) : '(Not extending)'); reporter .reportLanguages() .reportStackTrace(error) .reportEnvVars() .write(path_1.default.join(options.root, `${options.appName}-error.log`)); } } exports.default = Pipeline;