@4awpawz/buster
Version:
A configurable cache buster called Buster. Buster busts your browser cache problems.
67 lines (59 loc) • 2.13 kB
JavaScript
;
const readFile = require("../utils/readFile");
const log = require("../utils/log");
const readConfig = async file => {
try {
const read = await readFile(file, "utf8");
if (!read) {
return {};
}
log(`file ${file} exists!`);
return JSON.parse(read);
} catch (error) {
return false;
}
};
const mergeConfigs = (conf1, conf2) => ({ ...conf1, ...conf2 });
const isConfigComplete = config => {
return config.directives && config.directives.length > 0 && config.options;
};
const setEnv = verbose => {
process.env.BUSTERVERBOSE = verbose ? "verbose" : "quiet";
};
/**
* This module is responsible for validating the configuration properties which can be passed from a
* user script as paramsConfig or from .buster.json when Buster is called from the command line.
* @param {object} paramsConfig - Only passed when processBuster is called from a user script and contains
* the configuration properties required to run Buster.
*/
module.exports = async (paramsConfig) => {
const defConfig = {
options: {
ignore: "",
manifest: false,
verbose: false
},
directives: []
};
log("processing configuration");
if (paramsConfig) {
// Buster is being called from a user script which is passing
// configuration properties in paramsConfig.
const config = mergeConfigs(defConfig, paramsConfig);
if (isConfigComplete(config)) {
log("using params configuration");
setEnv(config.options.verbose);
return config;
}
} else {
// Buster is being called from the command line (see cli.js) and
// will find its configuration properties in file .buster.json.
const config = mergeConfigs(defConfig, await readConfig(".buster.json"));
if (isConfigComplete(config)) {
log("using .buster.json configuration");
setEnv(config.options.verbose);
return config;
}
}
throw new Error("error: no valid configuration found");
};