UNPKG

@onboardbase/cli

Version:

[![Version](https://img.shields.io/npm/v/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![Downloads/week](https://img.shields.io/npm/dw/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![License](https://img

90 lines (89 loc) 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseCommand = void 0; const command_1 = require("@oclif/command"); const config_1 = require("../config"); const chalk = require("chalk"); const onDeath = require("death"); const perf_hooks_1 = require("perf_hooks"); const path_1 = require("path"); const fs_1 = require("fs"); class BaseCommand extends command_1.Command { constructor() { super(...arguments); /** * Indicates whether to skip the configuration step completely. */ this.skipConfig = false; /** * Indicates whether the configuration should be loaded partially. * If set to `true`, only a partial configuration will be loaded. * Defaults to `false`, meaning the entire configuration will be loaded. */ this.loadConfigPartially = false; // when loading config, some error are thrown, in the case of the config:set command these errors can be ignored. this.haltOnConfigError = true; } async catch(err) { console.error(`${chalk.bold(chalk.red(err.__type || "COMMON_ERROR"))}: ${err.message}`); this.writeLogfile(err); } writeLogfile(err) { (0, fs_1.mkdirSync)(this.config.dataDir, { recursive: true }); (0, fs_1.writeFileSync)(this.getErrorLogFile(), `\n\n${new Date().toISOString()}: ${err}`, { flag: "a" }); } getErrorLogFile() { return (0, path_1.join)(this.config.dataDir, `error.log`); } async finally(err) { if (!this.skipConfig && !this.loadConfigPartially) this.configManager.persistConfigs(); perf_hooks_1.performance.mark("end"); // performance.measure("TOTAL RUNTIME:", "start", "end"); } async init() { const obs = new perf_hooks_1.PerformanceObserver((list) => { const entries = list.getEntries(); entries.forEach((entry) => { console.log(`${entry.name}: ${Number(entry.duration / 1000).toFixed(2)}s`); }); }); perf_hooks_1.performance.mark("start"); obs.observe({ entryTypes: ["measure"] }); if (this.skipConfig) return; this.configManager = new config_1.ConfigManager(); if (this.loadConfigPartially) { this.configManager.loadPartialConfig(this.config, this.haltOnConfigError); } else { this.configManager.loadAllConfig(this.config, this.haltOnConfigError); } onDeath(() => { console.log("Gracefully shutting down..."); if (!this.loadConfigPartially && !this.skipConfig) this.configManager.persistConfigs(); process.exit(); }); } async execute() { throw new Error("Command Not Implemented"); } getService(configManager) { throw new Error("method not implemented"); } getArgsAndFlags() { throw new Error("method not implemented"); } async run() { const service = this.getService(this.configManager); if (!this.skipConfig) { const dirScope = service.getConfigForCurrentScope() ? process.cwd() : "/"; service.setCurrentDirScope(dirScope); } const { args, flags } = this.getArgsAndFlags(); await service.preInit(); await service.initialize({ args, flags }); } } exports.BaseCommand = BaseCommand;