ring-websites-toolbelt
Version:
Ring Publishing Platform tool to work with Ring Websites
108 lines (83 loc) • 3.48 kB
JavaScript
const fs = require('fs');
const path = require('path');
const FilesProvider = require('./providers/FilesProvider');
const S3Provider = require('./providers/S3Provider');
const ExternalApiProvider = require('./providers/ExternalApiProvider');
class ThemeScriptAbstract {
constructor(options) {
this.themeDirName = path.basename(process.cwd());
this.options = options;
this.paths = {
config: options.configPath,
theme: process.cwd(),
modules: path.join('..', this.themeDirName + '_modules'),
temp_build: path.join('..', this.themeDirName + '_temp_build'),
build: path.join('..', this.themeDirName + '_build'),
temp: path.join('..')
};
}
validateThemeJson() {
console.info('Validating theme configuration');
if (!fs.existsSync('./theme.json')) {
throw new Error('Missing ./theme.json file');
}
let themeJson = null;
try {
themeJson = JSON.parse(fs.readFileSync('./theme.json', 'utf8'));
} catch (ex) {
throw new Error('Invalid ./theme.json content. Cannot parse file.');
}
if (!themeJson.theme || !themeJson.version) {
throw new Error('./theme.json does not contain one of these params: "theme" or "version"');
}
this.themeJson = themeJson;
}
loadThemeVarriables() {
console.info('Loading up theme 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.`);
}
const themeName = this.themeJson.theme;
if (!ucsConfig || !ucsConfig[themeName]) {
throw new Error(`Missing theme configuration for "${themeName}" theme. Please run "website setup"`);
}
/**
* START: TO REMOVE --- only to support backward compatibility with 1.6.x
*/
if (ucsConfig && !ucsConfig['___all_websites_configs']) {
const allWebsitesConfig = {};
Object.keys(ucsConfig).forEach((themeName) => {
allWebsitesConfig[ucsConfig[themeName].namespaceId] = ucsConfig[themeName];
});
ucsConfig['___all_websites_configs'] = allWebsitesConfig;
if (fs.existsSync(this.paths.config)) {
fs.unlinkSync(this.paths.config);
}
fs.appendFileSync(this.paths.config, JSON.stringify(ucsConfig), 'utf8');
}
/**
* END: TO REMOVE
*/
this.config = ucsConfig[themeName];
}
setUpProviders() {
console.info('Connecting to the API');
this.files = new FilesProvider(this.themeJson, this.config, this.options);
this.s3 = new S3Provider(this.themeJson, this.config, this.options);
this.api = new ExternalApiProvider(this.themeJson, this.config, this.options);
}
async purgeTheme() {
console.info('Purging theme');
if (!this.api) {
throw new Error('Providers are not set up properly! Cannot purge theme.');
}
await this.api.purgeTheme();
}
}
module.exports = ThemeScriptAbstract;