Skip to content

Commit

Permalink
feat: (strf-8747) split .stencil file into 2 configs
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Oct 29, 2020
1 parent 7b371e8 commit 6f3d2dc
Show file tree
Hide file tree
Showing 10 changed files with 557 additions and 61 deletions.
107 changes: 99 additions & 8 deletions lib/StencilConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,56 @@ const fsUtilsModule = require('./utils/fsUtils');
const { THEME_PATH } = require('../constants');

class StencilConfigManager {
constructor({ themePath = THEME_PATH, fs = fsModule, fsUtils = fsUtilsModule } = {}) {
this.configFileName = '.stencil';
constructor({
themePath = THEME_PATH,
fs = fsModule,
fsUtils = fsUtilsModule,
logger = console,
} = {}) {
this.oldConfigFileName = '.stencil';
this.configFileName = 'config.stencil.json';
this.secretsFileName = 'secrets.stencil.json';

this.themePath = themePath;
this.oldConfigPath = path.join(themePath, this.oldConfigFileName);
this.configPath = path.join(themePath, this.configFileName);
this.secretsPath = path.join(themePath, this.secretsFileName);
this.secretFieldsSet = new Set(['accessToken', 'githubToken']);

this._fs = fs;
this._fsUtils = fsUtils;
this._logger = logger;
}

/**
* @returns {object|null}
* @param {boolean} ignoreFileNotExists
* @param {boolean} ignoreMissingFields
* @returns {object|null}
*/
async readStencilConfig(ignoreFileNotExists = false) {
if (this._fs.existsSync(this.configPath)) {
return this._fsUtils.parseJsonFile(this.configPath);
async read(ignoreFileNotExists = false, ignoreMissingFields = false) {
if (this._fs.existsSync(this.oldConfigPath)) {
let parsedConfig;
try {
parsedConfig = await this._fsUtils.parseJsonFile(this.oldConfigPath);
// Tolerate broken files. We should migrate the old config first
// and then validation will throw an error about missing fields
// eslint-disable-next-line no-empty
} catch {
parsedConfig = {};
}
await this._migrateOldConfig(parsedConfig);
return this._validateStencilConfig(parsedConfig, ignoreMissingFields);
}

const generalConfig = this._fs.existsSync(this.configPath)
? await this._fsUtils.parseJsonFile(this.configPath)
: null;
const secretsConfig = this._fs.existsSync(this.secretsPath)
? await this._fsUtils.parseJsonFile(this.secretsPath)
: null;
if (generalConfig || secretsConfig) {
const parsedConfig = { ...generalConfig, ...secretsConfig };
return this._validateStencilConfig(parsedConfig, ignoreMissingFields);
}

if (ignoreFileNotExists) {
Expand All @@ -35,8 +68,66 @@ class StencilConfigManager {
/**
* @param {object} config
*/
saveStencilConfig(config) {
this._fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2));
async save(config) {
const { generalConfig, secretsConfig } = this._splitStencilConfig(config);

await this._fs.promises.writeFile(this.configPath, JSON.stringify(generalConfig, null, 2));
await this._fs.promises.writeFile(this.secretsPath, JSON.stringify(secretsConfig, null, 2));
}

/**
* @private
* @param {object} config
*/
_splitStencilConfig(config) {
return Object.entries(config).reduce(
(res, [key, value]) => {
if (this.secretFieldsSet.has(key)) {
res.secretsConfig[key] = value;
} else {
res.generalConfig[key] = value;
}
return res;
},
{ secretsConfig: {}, generalConfig: {} },
);
}

/**
* @private
* @param {object} config
* @param {boolean} ignoreMissingFields
* @returns {object}
*/
_validateStencilConfig(config, ignoreMissingFields) {
if (!ignoreMissingFields && (!config.normalStoreUrl || !config.customLayouts)) {
throw new Error(
'Error: Your stencil config is outdated. Please run'.red +
' $ stencil init'.cyan +
' again.'.red,
);
}
return config;
}

/**
* @private
* @param {object} config
*/
async _migrateOldConfig(config) {
this._logger.log(
`Detected a deprecated ${this.oldConfigFileName.cyan} file.\n` +
`It will be replaced with ${this.configFileName.cyan} and ${this.secretsFileName.cyan}\n`,
);

await this.save(config);
await this._fs.promises.unlink(this.oldConfigPath);

this._logger.log(
`The deprecated ${this.oldConfigFileName.cyan} file was successfully replaced.\n` +
`Make sure to add ${this.secretsFileName.cyan} to .gitignore.\n` +
`${this.configFileName.cyan} can by tracked by git if you wish.\n`,
);
}
}

Expand Down
Loading

0 comments on commit 6f3d2dc

Please sign in to comment.