ring-websites-toolbelt
Version:
Ring Publishing Platform tool to work with Ring Websites
159 lines (135 loc) • 5.62 kB
JavaScript
const fs = require('fs');
const prompts = require('prompts');
const homedir = require('os').homedir();
const RouterScriptAbstract = require(`./../../RouterScriptAbstract`);
class Setup extends RouterScriptAbstract {
constructor(options) {
super(options);
this.ucsConfig = {};
}
async execute() {
this.validateRouterJson();
console.info('Setting up environment varriables');
await this.loadExistingConfig();
await this.chooseNamespace();
await this.setupConnectionParams();
await this.validateApiKeys();
await this.setupS3Varriables();
console.info('Environment varriables successfuly set up! Use "website install" to download modules of your theme.');
}
async loadExistingConfig() {
if (fs.existsSync(this.paths.config)) {
console.info('This theme has already been set up in your environment. Confirm or edit required variables:');
try {
this.ucsConfig = JSON.parse(fs.readFileSync(this.paths.config, 'utf8'));
} catch (ex) {
throw new Error(`Invalid ${this.paths.config} content. Cannot parse file.`);
}
}
}
async chooseNamespace() {
const validate = value => !!value;
const allNamespaces = this.ucsConfig['___all_websites_configs'] || {};
const selectedConfiguration = await prompts([{
type: 'text',
name: 'namespaceId',
message: 'What is the namespaceId of your Website? [namespaceId]',
initial: '',
validate
}]);
if (selectedConfiguration.namespaceId) {
this.namespaceId = selectedConfiguration.namespaceId;
if (!allNamespaces[this.namespaceId]) {
allNamespaces[this.namespaceId] = {};
}
allNamespaces[this.namespaceId].namespaceId = this.namespaceId;
this.ucsConfig['___all_websites_configs'] = allNamespaces;
} else {
throw new Error(`Configuration is not fully set up. After you make required changes, please run "website setup" again.`);
}
}
async setupConnectionParams() {
const validate = value => !!value;
const allNamespaces = this.ucsConfig['___all_websites_configs'] || {};
const selectedConfiguration = await prompts([
{
type: 'text',
name: 'publicKey',
message: 'What is your public key to Website API of that namespace? [publicKey]',
initial: '',
validate
},
{
type: 'text',
name: 'secretKey',
message: 'What is the secret key to that private key? [secretKey]',
initial: '',
validate
}
]);
if (selectedConfiguration.publicKey && selectedConfiguration.secretKey) {
allNamespaces[this.namespaceId].publicKey = selectedConfiguration.publicKey;
allNamespaces[this.namespaceId].secretKey = selectedConfiguration.secretKey;
} else {
throw new Error(`Configuration is not fully set up. After you make required changes, please run "website setup" again.`);
}
}
async validateApiKeys() {
this.config = this.ucsConfig['___all_websites_configs'][this.namespaceId];
this.setUpProviders();
try {
await this.api.getThemes();
} catch (error) {
if (error.code === 403) {
throw new Error(`Authorization failed. Check validity of selected namespace and its authorization keys.`);
} else {
throw error;
}
}
}
async setupS3Varriables() {
const validate = value => !!value;
const allNamespaces = this.ucsConfig['___all_websites_configs'] || {};
const selectedConfiguration = await prompts([
{
type: 'text',
name: 's3BucketName',
message: 'What is your S3 bucket name where we can store your static files? [s3BucketName]',
initial: '',
validate
},
{
type: 'text',
name: 's3AccessKey',
message: 'What is the access key to that bucket? [s3AccessKey]',
initial: '',
validate
},
{
type: 'text',
name: 's3SecretKey',
message: 'What is the secret key to that access key? [s3SecretKey]',
initial: '',
validate
}
]);
if (selectedConfiguration.s3BucketName &&
selectedConfiguration.s3AccessKey &&
selectedConfiguration.s3SecretKey) {
allNamespaces[this.namespaceId] = {
...allNamespaces[this.namespaceId],
...selectedConfiguration
};
this.saveConfig();
} else {
throw new Error(`Configuration is not fully set up. After you make required changes, please run "website setup" again.`);
}
}
saveConfig() {
if (fs.existsSync(this.paths.config)) {
fs.unlinkSync(this.paths.config);
}
fs.appendFileSync(this.paths.config, JSON.stringify(this.ucsConfig), 'utf8');
}
}
module.exports = Setup;