This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.ts
63 lines (50 loc) · 1.53 KB
/
account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import _ from 'lodash-firecloud';
import env from './env';
import {
Account,
Env
} from './types';
export let get = function({env}: {
env: Env;
}): {[key: string]: Account;} {
let awsAccountIdVars = _.filter(_.keys(env), function(varName) {
return _.endsWith(varName, '_AWS_ACCOUNT_ID');
});
let accounts = {};
_.forEach(awsAccountIdVars, function(awsAccountIdVar) {
let prefix = _.replace(awsAccountIdVar, /_AWS_ACCOUNT_ID$/, '');
let NAME = _.toLower(prefix);
let ID = env[awsAccountIdVar];
let account = {
NAME,
ID
} as Partial<Account>;
let prefixedEnvVars = _.pickBy(env, function(_value, key) {
return _.startsWith(key, `${prefix}_`);
});
prefixedEnvVars = _.mapKeys(prefixedEnvVars, function(_value, key) {
return _.replace(key, new RegExp(`^${prefix}_`), '');
});
_.merge(account, prefixedEnvVars);
account.NS = _.split(_.defaultTo(account.NS as unknown as string, ''), ',');
account = account as Account;
accounts[ID] = account;
accounts[prefix] = account;
accounts[NAME] = account;
});
_.assign(accounts, accounts[env.AWS_ACCOUNT_ID]);
return accounts;
};
export let current = {} as Env;
// lazy init
// eslint-disable-next-line fp/no-proxy
export let currentProxy = new Proxy(current, {
get: function(target, property, _receiver) {
property = property as Exclude<typeof property, symbol>;
if (_.isEmpty(current)) {
_.merge(current, get({env}));
}
return target[property];
}
});
export default currentProxy;