1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const tslib_1 = require("tslib");
|
11 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
12 | const fs_extra_1 = require("fs-extra");
|
13 | const path = tslib_1.__importStar(require("path"));
|
14 | const env_1 = require("../constants/env");
|
15 | const debug = debug_1.default('ops:ConfigUtil');
|
16 | exports.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 | };
|
22 | exports.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 | };
|
34 | exports.clearConfig = async (configDir) => {
|
35 | debug('clearing config');
|
36 | const configPath = path.join(configDir, 'config.json');
|
37 | await fs_extra_1.remove(configPath);
|
38 | };
|
39 | const handleTeamNotFound = () => {
|
40 | debug('team not found');
|
41 | return {
|
42 | id: '',
|
43 | name: 'not found',
|
44 | };
|
45 | };
|
46 | const getTeam = (username, teams) => {
|
47 | const team = teams.find(({ name }) => name === username);
|
48 | return team || handleTeamNotFound();
|
49 | };
|
50 | exports.includeRegistryHost = (debug) => debug ? { registryHost: env_1.OPS_REGISTRY_HOST, nodeEnv: env_1.NODE_ENV } : {};
|
51 | exports.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 | };
|