@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
71 lines (70 loc) • 2.65 kB
JavaScript
;
/**
* @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 });
exports.formatConfigObject = exports.includeRegistryHost = exports.clearConfig = exports.readConfig = exports.writeConfig = void 0;
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 = (0, debug_1.default)('ops:ConfigUtil');
const writeConfig = async (oldConfigObj = {}, newConfigObj, configDir) => {
debug('writing new config');
const mergedConfigObj = Object.assign(Object.assign({}, oldConfigObj), newConfigObj);
await (0, fs_extra_1.outputJson)(path.join(configDir, 'config.json'), mergedConfigObj);
return mergedConfigObj;
};
exports.writeConfig = writeConfig;
const readConfig = async (configDir) => {
debug('reading config');
return (0, 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.readConfig = readConfig;
const clearConfig = async (configDir) => {
debug('clearing config');
const configPath = path.join(configDir, 'config.json');
await (0, fs_extra_1.remove)(configPath);
};
exports.clearConfig = clearConfig;
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();
};
const includeRegistryHost = (debug) => debug ? { registryHost: env_1.OPS_REGISTRY_HOST, nodeEnv: env_1.NODE_ENV } : {};
exports.includeRegistryHost = includeRegistryHost;
const formatConfigObject = (signinData) => {
const { tokens: { accessToken, refreshToken, idToken, sessionState }, meResponse: { teams, me }, } = signinData;
const configObj = {
user: Object.assign(Object.assign({}, me), (0, exports.includeRegistryHost)(env_1.OPS_DEBUG)),
team: getTeam(me.username, teams),
tokens: {
accessToken,
refreshToken,
idToken,
sessionState,
},
};
return configObj;
};
exports.formatConfigObject = formatConfigObject;