UNPKG

2.27 kBJavaScriptView Raw
1"use strict";
2/**
3 * @author: JP Lew (jp@cto.ai)
4 * @date: Tuesday, 6th August 2019 3:07:48 pm
5 * @lastModifiedBy: JP Lew (jp@cto.ai)
6 * @lastModifiedTime: Wednesday, 4th September 2019 3:19:11 pm
7 * @copyright (c) 2019 CTO.ai
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10const tslib_1 = require("tslib");
11const debug_1 = tslib_1.__importDefault(require("debug"));
12const fs_extra_1 = require("fs-extra");
13const path = tslib_1.__importStar(require("path"));
14const env_1 = require("../constants/env");
15const debug = debug_1.default('ops:ConfigUtil');
16exports.writeConfig = async (oldConfigObj = {}, newConfigObj, configDir) => {
17 debug('writing new config');
18 const mergedConfigObj = Object.assign({}, oldConfigObj, newConfigObj);
19 await fs_extra_1.outputJson(path.join(configDir, 'config.json'), mergedConfigObj);
20 return mergedConfigObj;
21};
22exports.readConfig = async (configDir) => {
23 debug('reading config');
24 return fs_extra_1.readJson(path.join(configDir, 'config.json')).catch(err => {
25 if (err.code === 'ENOENT') {
26 debug(`No config file found at ${err.path}`);
27 }
28 else {
29 debug('%O', err);
30 }
31 return {};
32 });
33};
34exports.clearConfig = async (configDir) => {
35 debug('clearing config');
36 const configPath = path.join(configDir, 'config.json');
37 await fs_extra_1.remove(configPath);
38};
39const handleTeamNotFound = () => {
40 debug('team not found');
41 return {
42 id: '',
43 name: 'not found',
44 };
45};
46const getTeam = (username, teams) => {
47 const team = teams.find(({ name }) => name === username);
48 return team || handleTeamNotFound();
49};
50exports.includeRegistryHost = (debug) => debug ? { registryHost: env_1.OPS_REGISTRY_HOST, nodeEnv: env_1.NODE_ENV } : {};
51exports.formatConfigObject = (signinData) => {
52 const { tokens: { accessToken, refreshToken, idToken, sessionState }, meResponse: { teams, me }, } = signinData;
53 const configObj = {
54 user: Object.assign({}, me, exports.includeRegistryHost(env_1.OPS_DEBUG)),
55 team: getTeam(me.username, teams),
56 tokens: {
57 accessToken,
58 refreshToken,
59 idToken,
60 sessionState,
61 },
62 };
63 return configObj;
64};