UNPKG

1.97 kBJavaScriptView Raw
1const ConfigFile = require('./abstract-config-file');
2
3// instance which stores the singleton
4let instance = null;
5
6module.exports = class AppConfig extends ConfigFile {
7 /**
8 * Constructor for GlobalConfig class
9 * @param {string} filePath
10 * @throws {Error}
11 */
12 constructor(filePath) {
13 if (instance && instance.path === filePath) {
14 return instance;
15 }
16 // init by calling super() if instance not exists
17 super(filePath);
18 instance = this;
19 }
20
21 static getInstance() {
22 return instance;
23 }
24
25 static dispose() {
26 instance = null;
27 }
28
29 // getter and setter
30
31 getAwsProfile(profile) {
32 return this.getProperty(['profiles', profile, 'aws_profile']);
33 }
34
35 setAwsProfile(profile, awsProfile) {
36 this.setProperty(['profiles', profile, 'aws_profile'], awsProfile);
37 }
38
39 getToken(profile) {
40 return this.getProperty(['profiles', profile, 'token']);
41 }
42
43 setToken(profile, tokenObject) {
44 this.setProperty(['profiles', profile, 'token'], tokenObject);
45 }
46
47 getVendorId(profile) {
48 return this.getProperty(['profiles', profile, 'vendor_id']);
49 }
50
51 setVendorId(profile, vendorId) {
52 this.setProperty(['profiles', profile, 'vendor_id'], vendorId);
53 }
54
55 /**
56 * Returns all profile names and their associated aws profile names (if any) as list of objects.
57 * return profilesList. Eg: [{ askProfile: 'askProfile1', awsProfile: 'awsProfile1'}, { askProfile: 'askProfile2', awsProfile: 'awsProfile2'}].
58 */
59 getProfilesList() {
60 const profilesList = [];
61 const profilesObj = this.getProperty(['profiles']);
62 for (const profile of Object.getOwnPropertyNames(profilesObj)) {
63 profilesList.push({
64 askProfile: profile,
65 awsProfile: profilesObj[profile].aws_profile
66 });
67 }
68 return profilesList;
69 }
70};