forked from twilio/twilio-cli-core
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
223 lines (185 loc) · 5.17 KB
/
config.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* eslint-disable max-classes-per-file */
const path = require('path');
const fs = require('fs-extra');
const MessageTemplates = require('./messaging/templates');
const CLI_NAME = 'twilio-cli';
class ConfigDataProfile {
constructor(id, accountSid, region) {
this.id = id;
this.accountSid = accountSid;
this.region = region;
}
}
class ConfigData {
constructor() {
this.edge = undefined;
this.email = {};
this.prompts = {};
this.profiles = [];
this.activeProfile = null;
}
getProfileFromEnvironment() {
const { TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_API_KEY, TWILIO_API_SECRET, TWILIO_REGION } = process.env;
if (!TWILIO_ACCOUNT_SID) {
return undefined;
}
if (TWILIO_API_KEY && TWILIO_API_SECRET) {
return {
// eslint-disable-next-line no-template-curly-in-string
id: '${TWILIO_API_KEY}/${TWILIO_API_SECRET}',
accountSid: TWILIO_ACCOUNT_SID,
apiKey: TWILIO_API_KEY,
apiSecret: TWILIO_API_SECRET,
region: TWILIO_REGION,
};
}
if (TWILIO_AUTH_TOKEN) {
return {
// eslint-disable-next-line no-template-curly-in-string
id: '${TWILIO_ACCOUNT_SID}/${TWILIO_AUTH_TOKEN}',
accountSid: TWILIO_ACCOUNT_SID,
apiKey: TWILIO_ACCOUNT_SID,
apiSecret: TWILIO_AUTH_TOKEN,
region: TWILIO_REGION,
};
}
return undefined;
}
getProfileById(profileId) {
let profile;
if (!profileId) {
profile = this.getProfileFromEnvironment();
}
if (!profile) {
if (profileId) {
// Clean the profile ID.
profileId = this.sanitize(profileId);
profile = this.profiles.find((p) => p.id === profileId);
} else {
profile = this.getActiveProfile();
}
}
return profile;
}
setActiveProfile(profileId) {
if (profileId) {
const profile = this.getProfileById(profileId);
if (profile) {
this.activeProfile = profile.id;
return profile;
}
}
return undefined;
}
getActiveProfile() {
let profile;
if (this.profiles.length > 0) {
if (this.activeProfile) {
profile = this.profiles.find((p) => p.id === this.activeProfile);
}
if (!profile) {
profile = this.profiles[0];
}
}
return profile;
}
removeProfile(profileToRemove) {
this.profiles = this.profiles.filter((profile) => {
return profile.id !== profileToRemove.id;
});
if (profileToRemove.id === this.activeProfile) {
this.activeProfile = null;
}
}
addProfile(id, accountSid, region) {
// Clean all the inputs.
id = this.sanitize(id);
accountSid = this.sanitize(accountSid);
region = this.sanitize(region);
const existing = this.getProfileById(id);
if (existing) {
existing.accountSid = accountSid;
existing.region = region;
} else {
this.profiles.push(new ConfigDataProfile(id, accountSid, region));
}
}
isPromptAcked(promptId) {
const prompt = this.prompts[promptId];
return Boolean(prompt && prompt.acked);
}
ackPrompt(promptId) {
let prompt = this.prompts[promptId];
if (!prompt) {
prompt = {};
this.prompts[promptId] = prompt;
}
prompt.acked = true;
}
loadFromObject(configObj) {
this.edge = configObj.edge;
this.email = configObj.email || {};
this.prompts = configObj.prompts || {};
// Note the historical 'projects' naming.
configObj.profiles = configObj.projects || [];
configObj.profiles.forEach((profile) => this.addProfile(profile.id, profile.accountSid, profile.region));
this.setActiveProfile(configObj.activeProject);
}
sanitize(string) {
// Trim whitespace if given a non-null string.
return string ? string.trim() : string;
}
}
class Config {
constructor(configDir) {
this.configDir = configDir;
this.filePath = path.join(configDir, 'config.json');
}
async load() {
const configData = new ConfigData();
if (!fs.existsSync(this.filePath)) {
return configData;
}
configData.loadFromObject(await fs.readJSON(this.filePath));
return configData;
}
async save(configData) {
configData = {
edge: configData.edge,
email: configData.email,
prompts: configData.prompts,
// Note the historical 'projects' naming.
projects: configData.profiles,
activeProject: configData.activeProfile,
};
fs.mkdirSync(this.configDir, { recursive: true });
await fs.writeJSON(this.filePath, configData, { flag: 'w' });
return MessageTemplates.configSaved({ path: this.filePath });
}
}
class PluginConfig {
constructor(configDir, pluginName) {
this.filePath = path.join(configDir, 'plugins', pluginName, 'config.json');
}
async getConfig() {
try {
return await fs.readJSON(this.filePath, { encoding: 'utf-8' });
} catch (error) {
return {};
}
}
async setConfig(config) {
try {
await fs.writeJSON(this.filePath, config);
} catch (error) {
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
await fs.writeJSON(this.filePath, config);
}
}
}
module.exports = {
CLI_NAME,
Config,
ConfigData,
PluginConfig,
};