UNPKG

@cto.ai/ops

Version:

💻 CTO.ai Ops - The CLI built for Teams 🚀

65 lines (64 loc) • 2.3 kB
"use strict"; /** * @author: JP Lew (jp@cto.ai) * @date: Tuesday, 6th August 2019 3:07:48 pm * @lastModifiedBy: JP Lew (jp@cto.ai) * @lastModifiedTime: Wednesday, 4th September 2019 3:19:11 pm * @copyright (c) 2019 CTO.ai */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const debug_1 = tslib_1.__importDefault(require("debug")); const fs_extra_1 = require("fs-extra"); const path = tslib_1.__importStar(require("path")); const env_1 = require("../constants/env"); const debug = debug_1.default('ops:ConfigUtil'); exports.writeConfig = async (oldConfigObj = {}, newConfigObj, configDir) => { debug('writing new config'); const mergedConfigObj = Object.assign(Object.assign({}, oldConfigObj), newConfigObj); await fs_extra_1.outputJson(path.join(configDir, 'config.json'), mergedConfigObj); return mergedConfigObj; }; exports.readConfig = async (configDir) => { debug('reading config'); return fs_extra_1.readJson(path.join(configDir, 'config.json')).catch(err => { if (err.code === 'ENOENT') { debug(`No config file found at ${err.path}`); } else { debug('%O', err); } return {}; }); }; exports.clearConfig = async (configDir) => { debug('clearing config'); const configPath = path.join(configDir, 'config.json'); await fs_extra_1.remove(configPath); }; const handleTeamNotFound = () => { debug('team not found'); return { id: '', name: 'not found', }; }; const getTeam = (username, teams) => { const team = teams.find(({ name }) => name === username); return team || handleTeamNotFound(); }; exports.includeRegistryHost = (debug) => debug ? { registryHost: env_1.OPS_REGISTRY_HOST, nodeEnv: env_1.NODE_ENV } : {}; exports.formatConfigObject = (signinData) => { const { tokens: { accessToken, refreshToken, idToken, sessionState }, meResponse: { teams, me }, } = signinData; const configObj = { user: Object.assign(Object.assign({}, me), exports.includeRegistryHost(env_1.OPS_DEBUG)), team: getTeam(me.username, teams), tokens: { accessToken, refreshToken, idToken, sessionState, }, }; return configObj; };