ring-websites-toolbelt
Version:
Ring Publishing Platform tool to work with Ring Websites
92 lines (71 loc) • 3.31 kB
JavaScript
const fs = require('fs');
const path = require('path');
const prompts = require('prompts');
const FilesProvider = require('./providers/FilesProvider');
const S3Provider = require('./providers/S3Provider');
const ExternalApiProvider = require('./providers/ExternalApiProvider');
class RouterScriptAbstract {
constructor(options) {
this.themeDirName = path.basename(process.cwd());
this.options = options;
this.paths = {
config: options.configPath,
router: process.cwd()
};
}
validateRouterJson() {
console.info('Validating router configuration');
if (!fs.existsSync('./router.json')) {
throw new Error('Missing ./router.json file');
}
let routerJson = null;
try {
routerJson = JSON.parse(fs.readFileSync('./router.json', 'utf8'));
} catch (ex) {
throw new Error('Invalid ./router.json content. Cannot parse file.');
}
if (!routerJson.name || !routerJson.version || !routerJson.routerAbstracts) {
throw new Error('./router.json does not contain one of these params: "name", "version" or "routerAbstracts"');
}
this.routerJson = routerJson;
}
async selectApiConfig() {
console.info('Loading router configuration');
if (!fs.existsSync(this.paths.config)) {
throw new Error(`Missing ${this.paths.config} file. Please run "website setup"`);
}
let ucsConfig = null;
try {
ucsConfig = JSON.parse(fs.readFileSync(this.paths.config, 'utf8'));
} catch (ex) {
throw new Error(`Invalid ${this.paths.config} content. Cannot parse JSON file.`);
}
if (!ucsConfig || !ucsConfig['___all_websites_configs'] || !Object.keys(ucsConfig['___all_websites_configs']).length) {
throw new Error(`Missing configuration. Please run "website setup"`);
}
const allNamespaces = ucsConfig['___all_websites_configs'];
const selectChoises = Object.keys(allNamespaces).map(namespaceId => ({ title: namespaceId, value: namespaceId }));
selectChoises.push({ title: '[New namespace configuration]', value: '[new]' });
const selectedConfiguration = await prompts([{
type: 'select',
name: 'namespaceId',
message: 'What is the namespaceId of your Website? [namespaceId]',
choices: selectChoises
}]);
if (!selectedConfiguration.namespaceId) {
throw new Error(`Configuration is not fully set up. After you make required changes, please run "website setup" again.`);
}
if (selectedConfiguration.namespaceId === '[new]') {
console.info(`To create new configuration please run "website setup"`);
process.exit(1);
}
this.config = allNamespaces[selectedConfiguration.namespaceId];
}
setUpProviders() {
console.info('Connecting to the API');
this.files = new FilesProvider(this.routerJson, this.config, this.options);
this.s3 = new S3Provider(this.routerJson, this.config, this.options);
this.api = new ExternalApiProvider(this.routerJson, this.config, this.options);
}
}
module.exports = RouterScriptAbstract;