mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-08 05:31:55 +00:00
Turn provider setup into a class
This commit is contained in:
104
lib/cli.js
104
lib/cli.js
@@ -27,7 +27,7 @@ export default function() {
|
||||
program
|
||||
.command('setup [provider]')
|
||||
.description('Sets up a new HomeBridge provider or re-configures an existing one.')
|
||||
.action(setupProvider);
|
||||
.action((providerName, options) => new CliProviderSetup(providerName, options).setup());
|
||||
|
||||
// Parse options and execute HomeBridge
|
||||
program.parse(process.argv);
|
||||
@@ -57,62 +57,74 @@ function listInstalledProviders(options) {
|
||||
Provider.installed().forEach((provider) => console.log(provider.name));
|
||||
}
|
||||
|
||||
function setupProvider(providerName, options) {
|
||||
|
||||
// if you didn't specify a provider, print help
|
||||
if (!providerName) {
|
||||
log.error("You must specify the name of the provider to setup. Type 'homebridge providers' to list the providers currently installed.");
|
||||
program.help();
|
||||
// Encapsulates configuring a provider via the command line.
|
||||
class CliProviderSetup {
|
||||
constructor(providerName: string, options:object) {
|
||||
|
||||
// if you didn't specify a provider, print help
|
||||
if (!providerName) {
|
||||
log.error("You must specify the name of the provider to setup. Type 'homebridge providers' to list the providers currently installed.");
|
||||
program.help();
|
||||
}
|
||||
|
||||
this.providerName = providerName;
|
||||
this.options = options; // command-line options (currently none)
|
||||
}
|
||||
|
||||
try {
|
||||
let provider = new Provider(providerName);
|
||||
let providerModule:object = provider.load({skipConfigCheck: true});
|
||||
|
||||
if (providerModule.config) {
|
||||
setup() {
|
||||
try {
|
||||
let provider = new Provider(this.providerName);
|
||||
this.providerModule = provider.load({skipConfigCheck: true});
|
||||
|
||||
prompt.message = "";
|
||||
prompt.delimiter = "";
|
||||
prompt.start();
|
||||
prompt.get(buildPromptSchema(providerName, providerModule.config), (err, result) => {
|
||||
if (this.providerModule.config) {
|
||||
|
||||
// apply configuration values entered by the user
|
||||
for (let key:string in result) {
|
||||
let value:object = result[key];
|
||||
prompt.message = "";
|
||||
prompt.delimiter = "";
|
||||
prompt.start();
|
||||
prompt.get(this.buildPromptSchema(), (err, result) => {
|
||||
|
||||
User.config.set(`${providerName}.${key}`, value);
|
||||
}
|
||||
|
||||
providerModule.validateConfig();
|
||||
});
|
||||
// apply configuration values entered by the user
|
||||
for (let key:string in result) {
|
||||
let value:object = result[key];
|
||||
|
||||
User.config.set(`${this.providerName}.${key}`, value);
|
||||
}
|
||||
|
||||
this.validateProviderConfig();
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.validateProviderConfig();
|
||||
}
|
||||
}
|
||||
else {
|
||||
providerModule.validateConfig();
|
||||
catch (err) {
|
||||
log.error(`Setup failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
log.error(`Setup failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// builds a "schema" obejct for the prompt lib based on the provider's config spec
|
||||
function buildPromptSchema(providerName: string, providerConfig: object): object {
|
||||
let properties = {};
|
||||
|
||||
for (let key:string in providerConfig) {
|
||||
let spec:object = providerConfig[key];
|
||||
validateProviderConfig() {
|
||||
this.providerModule.validateConfig();
|
||||
}
|
||||
|
||||
// builds a "schema" obejct for the prompt lib based on the provider's config spec
|
||||
buildPromptSchema(): object {
|
||||
let properties = {};
|
||||
|
||||
// do we have a value for this config key currently?
|
||||
let currentValue = User.config.get(`${providerName}.${key}`);
|
||||
for (let key:string in this.providerModule.config) {
|
||||
let spec:object = this.providerModule.config[key];
|
||||
|
||||
// do we have a value for this config key currently?
|
||||
let currentValue = User.config.get(`${this.providerName}.${key}`);
|
||||
|
||||
// copy over config spec with some modifications
|
||||
properties[key] = {
|
||||
description: `\n${spec.description}\n${camelCaseToRegularForm(key).white}:`,
|
||||
type: spec.type,
|
||||
required: spec.required,
|
||||
default: currentValue
|
||||
// copy over config spec with some modifications
|
||||
properties[key] = {
|
||||
description: `\n${spec.description}\n${camelCaseToRegularForm(key).white}:`,
|
||||
type: spec.type,
|
||||
required: spec.required,
|
||||
default: currentValue
|
||||
}
|
||||
}
|
||||
|
||||
return { properties };
|
||||
}
|
||||
|
||||
return { properties };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user