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

123 lines (122 loc) 6.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseService = void 0; const http_service_1 = require("./http.service"); const types_1 = require("./types"); const errors_1 = require("./errors"); const messages = { errConfigNotFoundInProjectConfig: "unable to get config value from your project configuration file. You might need to run `onboardbase setup` to create a setup configuration for your current working directory or global(for all your directories)", errGlobalConfigNotFound: "unable to pull from your global configuration file. You might need to run `onboardbase setup` to create a setup configuration for your current working directory or global(for all your directories)", }; class BaseService { constructor(configManager) { this._CURRENT_DIR_SCOPE = process.cwd(); this.configManager = configManager; // TODO: Optimize so a single HTTPService instance exists for all the services this.httpInstance = new http_service_1.HTTPService(this.getConfigForCurrentScope("api-host")); } setCurrentDirScope(path = process.cwd()) { this._CURRENT_DIR_SCOPE = path; } getCurrentDirScope() { return this._CURRENT_DIR_SCOPE; } setScopeConfig(key, value) { const _key = `scoped.${this._CURRENT_DIR_SCOPE}.${key}`; return this.configManager.setGlobal(_key, value); } async initialize(argsFlags) { throw new Error("Method not implemented"); } async preInit() { } /** * Get the current project and environment from the user input * @returns {currentProject: string, currentEnvironment: string} */ _handleGetProjectAndEnvironment({ project, environment }) { var _a; const currentProject = this._getFromProjectConfigOrThrow({ userConfig: project, configPath: "setup.project", envName: types_1.ENV_NAMES.PROJECT, }); const currentEnvironment = (_a = this._getFromProjectConfigOrThrow({ userConfig: environment, configPath: "setup.environment", envName: types_1.ENV_NAMES.ENVIRONMENT, })) !== null && _a !== void 0 ? _a : "development"; if (!currentProject) throw new errors_1.BadInputError("Please specify project name"); return { currentProject, currentEnvironment }; } /** * Reads the configuration from an onboardbase.yml file * It prioritizes the value of the envName, * then check if the userConfig has a value, * before proceeeding to read the value from onboardbase.yml file, * * @param options parameters to use when reading the config value from .onboardbase.yaml file * @property {options.envName} {string} An environment variable name to read from. * @property {options.userConfig} {string} The value entered by a user from the command line for a given flag or argument * @property {options.configPath} {string} A valid json path using dot separator inside of onboardbase.yml file * @returns {string} */ _getFromProjectConfigOrThrow({ userConfig, configPath, envName, errMsg, }) { const envValue = this.configManager.getFromProcessEnv(envName); if (envValue) return envValue; const configValue = userConfig !== null && userConfig !== void 0 ? userConfig : this.configManager.getFromProject(configPath); if (!configValue) { // TODO: accept the error message throw errors_1.BadConfigError.from(errMsg !== null && errMsg !== void 0 ? errMsg : messages.errConfigNotFoundInProjectConfig); } return configValue; } /** * Reads the configuration values from the Global config file at {configDir}/config.json file * It prioritizes the value of the envName, * then check if the userConfig has a value, * before proceeeding to read the value from config.json file. * * TO read from config.json file requires checking if there is an existing config for the present working directory(refered to as pwd scope), before checking the root directory(the global scope). If none of these has a value for the required env, it throws a bad configuration error. * * @param options parameters to use when reading the config value from .onboardbase.yaml file * @property {options.envName} {string} An environment variable name to read from. * @property {options.userConfig} {string} The value entered by a user from the command line for a given flag or argument * @property {options.configPath} {string} A valid json path using dot separator inside of onboardbase.yaml file * @returns {string} */ _getFromGlobalConfigOrThrow({ userConfig, configPath, envName, }) { const envValue = this.configManager.getFromProcessEnv(envName); if (envValue) return envValue; const configValue = userConfig !== null && userConfig !== void 0 ? userConfig : this.getConfigForCurrentScope(configPath); if (!configValue) { // TODO: accept the error message throw errors_1.BadConfigError.from(messages.errGlobalConfigNotFound); } return configValue; } /** * Sets a configuration for the current scope. * @param subPath - Optional sub-path within the current scope. * @returns The configuration for the current scope. */ getConfigForCurrentScope(subPath) { return this.configManager.getFromGlobal(`scoped.${this._CURRENT_DIR_SCOPE}${subPath ? `.${subPath}` : ""}`); } /** * Retrieves the configuration for the current scope. * @param subPath - Optional sub-path within the current scope. * @returns The configuration for the current scope. */ setConfigForCurrentScope(subPath, val) { return this.configManager.setGlobal(`scoped.${this._CURRENT_DIR_SCOPE}${subPath ? `.${subPath}` : ""}`, val); } async ensureConfigExists() { const config = this.getConfigForCurrentScope(); if (!config) throw errors_1.BadConfigError.from("please run onboardbase setup to authorize this CLI"); } } exports.BaseService = BaseService;