@boost/core
Version:
Robust pipeline for creating dev tools that separate logic into routines and tasks.
89 lines (88 loc) • 3.36 kB
JavaScript
;
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 util_1 = __importDefault(require("util"));
const common_1 = require("@boost/common");
const debug_1 = require("@boost/debug");
const internal_1 = require("@boost/internal");
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, internal_1.ExitError)) {
this.options.exit(error.code);
}
else {
this.options.exit(1);
}
return error;
});
}
/**
* Report the pipeline failure by writing a crash log.
*/
reportCrash(error) {
const { appPath, rootPath, config, options } = this.tool;
const { appName } = options;
const reporter = new debug_1.CrashReporter()
.reportBinaries()
.reportProcess()
.reportSystem();
reporter
.addSection(appName)
.add('App name', appName)
.add('App path', appPath)
.add('Plugin types', Object.keys(this.tool.getRegisteredPlugins()).join(', '))
.add('Scoped package', options.scoped ? 'Yes' : 'No')
.add('Root', rootPath)
.add('Config name', options.configName)
.add('Package path', rootPath.append('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
.reportPackageVersions(options.scoped ? [`@${appName}/*`, `${appName}-*`] : `${appName}-*`, `${appName} Packages`)
.reportPackageVersions(['@boost/*', 'boost-*'], 'Boost Packages')
.reportLanguages()
.reportStackTrace(error)
.reportEnvVars()
.write(rootPath.append(`${options.appName}-error.log`));
}
}
exports.default = Pipeline;