Skip to content

Commit 852477e

Browse files
authored
feat: allow plugins to use the userConfig object to set arbitrary data (#118)
1 parent c89e594 commit 852477e

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/base-commands/base-command.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { CLIError } = require('@oclif/errors');
33

44
const pkg = require('../../package.json');
55
const MessageTemplates = require('../services/messaging/templates');
6-
const { Config, ConfigData } = require('../services/config');
6+
const { Config, ConfigData, PluginConfig } = require('../services/config');
77
const { TwilioCliError } = require('../services/error');
88
const { logger, LoggingLevel } = require('../services/messaging/logging');
99
const { OutputFormats } = require('../services/output-formats');
@@ -170,6 +170,22 @@ class BaseCommand extends Command {
170170
async install(name) {
171171
return requireInstall(name, this);
172172
}
173+
174+
get pluginConfig() {
175+
if (!this._pluginConfig) {
176+
const pluginName = getCommandPlugin(this);
177+
this._pluginConfig = new PluginConfig(this.config.configDir, pluginName);
178+
}
179+
return this._pluginConfig;
180+
}
181+
182+
async getPluginConfig() {
183+
return this.pluginConfig.getConfig();
184+
}
185+
186+
async setPluginConfig(config) {
187+
return this.pluginConfig.setConfig(config);
188+
}
173189
}
174190

175191
BaseCommand.flags = {

src/services/config.js

+24
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,32 @@ class Config {
192192
}
193193
}
194194

195+
class PluginConfig {
196+
constructor(configDir, pluginName) {
197+
this.filePath = path.join(configDir, 'plugins', pluginName, 'config.json');
198+
}
199+
200+
async getConfig() {
201+
try {
202+
return await fs.readJSON(this.filePath, { encoding: 'utf-8' });
203+
} catch (error) {
204+
return {};
205+
}
206+
}
207+
208+
async setConfig(config) {
209+
try {
210+
await fs.writeJSON(this.filePath, config);
211+
} catch (error) {
212+
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
213+
await fs.writeJSON(this.filePath, config);
214+
}
215+
}
216+
}
217+
195218
module.exports = {
196219
CLI_NAME,
197220
Config,
198221
ConfigData,
222+
PluginConfig,
199223
};

test/services/config.test.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path');
33
const tmp = require('tmp');
44
const { expect, test, constants } = require('@twilio/cli-test');
55

6-
const { Config, ConfigData } = require('../../src/services/config');
6+
const { Config, ConfigData, PluginConfig } = require('../../src/services/config');
77

88
const FAKE_AUTH_TOKEN = '1234567890abcdefghijklmnopqrstuvwxyz';
99

@@ -231,5 +231,35 @@ describe('services', () => {
231231
expect(saveMessage).to.contain(`${nestedConfig}${path.sep}config.json`);
232232
});
233233
});
234+
235+
describe('PluginConfig', () => {
236+
let tempConfigDir;
237+
beforeEach(() => {
238+
tempConfigDir = tmp.dirSync({ unsafeCleanup: true });
239+
});
240+
afterEach(() => {
241+
tempConfigDir.removeCallback();
242+
});
243+
244+
test.it("loads an empty object when the plugin directory doesn't exist", async () => {
245+
const pluginConfig = new PluginConfig(tempConfigDir.name, 'test-plugin');
246+
expect(await pluginConfig.getConfig()).to.deep.equal({});
247+
});
248+
249+
test.it("saves config to the plugin directory when it doesn't exist", async () => {
250+
const pluginConfig = new PluginConfig(tempConfigDir.name, 'test-plugin');
251+
await pluginConfig.setConfig({ foo: 'bar' });
252+
expect(await pluginConfig.getConfig()).to.deep.equal({ foo: 'bar' });
253+
});
254+
255+
test.it('overwrites config when it already exists', async () => {
256+
const pluginConfig = new PluginConfig(tempConfigDir.name, 'test-plugin');
257+
await pluginConfig.setConfig({ hello: 'world' });
258+
259+
const pluginConfig2 = new PluginConfig(tempConfigDir.name, 'test-plugin');
260+
await pluginConfig2.setConfig({ foo: 'bar' });
261+
expect(await pluginConfig2.getConfig()).to.deep.equal({ foo: 'bar' });
262+
});
263+
});
234264
});
235265
});

0 commit comments

Comments
 (0)