@dojo/cli
Version:
150 lines • 6.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const fs_1 = require("fs");
const path_1 = require("path");
const readlineSync = require("readline-sync");
const detectIndent = require("detect-indent");
const json_extends_1 = require("@speedy/json-extends");
const pkgDir = require('pkg-dir');
const appPath = pkgDir.sync(process.cwd());
function getDojoRcConfigOption() {
const defaultDojoRc = '.dojorc';
const configIndex = process.argv.indexOf('--dojorc');
if (configIndex !== -1 && appPath) {
const customDojoRc = process.argv[configIndex + 1];
if (customDojoRc && fs_1.existsSync(path_1.join(appPath, customDojoRc))) {
return customDojoRc;
}
console.warn(chalk_1.default.yellow(`Specified dojorc file '${customDojoRc}' does not exist, using '.dojorc'`));
}
return defaultDojoRc;
}
exports.getDojoRcConfigOption = getDojoRcConfigOption;
let dojoRcPath;
let packageJsonPath;
if (appPath) {
dojoRcPath = path_1.join(appPath, '.dojorc');
packageJsonPath = path_1.join(appPath, 'package.json');
}
let canWriteToPackageJson;
const defaultIndent = 2;
function parseConfigs() {
const configWrapper = {};
if (fs_1.existsSync(dojoRcPath)) {
try {
const dojoRcFile = fs_1.readFileSync(dojoRcPath, 'utf8');
const extendedConfig = json_extends_1.json.readSync(dojoRcPath);
configWrapper.dojoRcIndent = detectIndent(dojoRcFile).indent;
configWrapper.dojoRcConfig = extendedConfig;
}
catch (error) {
throw Error(chalk_1.default.red(`Could not parse the .dojorc file to get config: ${error}`));
}
}
if (fs_1.existsSync(packageJsonPath)) {
try {
const packageJsonFile = fs_1.readFileSync(packageJsonPath, 'utf8');
const packageJson = json_extends_1.json.readSync(packageJsonPath);
configWrapper.packageJsonIndent = detectIndent(packageJsonFile).indent;
configWrapper.packageJsonConfig = packageJson.dojo;
}
catch (error) {
throw Error(chalk_1.default.red(`Could not parse the package.json file to get config: ${error}`));
}
}
return configWrapper;
}
function writePackageConfig(config, indent) {
if (canWriteToPackageJson === undefined) {
canWriteToPackageJson = Boolean(readlineSync.keyInYN('You are using a "dojo" configuration in your package.json. Saving the current settings will update your package.json. Continue? [ (N)o / (Y)es ]: ', { guide: false }));
}
if (canWriteToPackageJson) {
const packageJsonFile = fs_1.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonFile);
packageJson.dojo = config;
const newPackageJson = JSON.stringify(packageJson, null, indent);
fs_1.writeFileSync(packageJsonPath, newPackageJson);
}
}
function writeDojoRcConfig(config, indent) {
const json = JSON.stringify(config, null, indent);
fs_1.writeFileSync(dojoRcPath, json);
}
function getConfig() {
const { packageJsonConfig, dojoRcConfig } = parseConfigs();
const hasPackageConfig = typeof packageJsonConfig === 'object';
const hasDojoRcConfig = typeof dojoRcConfig === 'object';
if (!hasDojoRcConfig && hasPackageConfig) {
return packageJsonConfig;
}
else {
return dojoRcConfig;
}
}
exports.getConfig = getConfig;
function warnAboutMultiConfig() {
const warning = `Warning: Both a .dojorc configuration and a dojo configuration in your package.json were found. The .dojorc file will take precedent. It is recommended you stick to one configuration option.`;
console.warn(chalk_1.default.yellow(warning));
}
function checkForMultiConfig() {
const { dojoRcConfig, packageJsonConfig } = parseConfigs();
const hasPackageConfig = typeof packageJsonConfig === 'object';
const hasDojoRcConfig = typeof dojoRcConfig === 'object';
const usingDefaultojoRcConfig = getDojoRcConfigOption() === '.dojorc';
if (hasPackageConfig && hasDojoRcConfig && usingDefaultojoRcConfig) {
warnAboutMultiConfig();
}
}
exports.checkForMultiConfig = checkForMultiConfig;
class SingleCommandConfigurationHelper {
constructor(groupName, commandName) {
this._configurationKey = groupName;
if (commandName) {
this._configurationKey += `-${commandName}`;
}
}
/**
* Retrieves the configurationFactory object from the file system
*
* @param commandName - the command name that's accessing config
* @returns an object representation of .dojorc
*/
get(commandName = this._configurationKey) {
const config = getConfig() || {};
return config[commandName];
}
set(config, commandName) {
if (!dojoRcPath && !packageJsonPath) {
console.error(chalk_1.default.red('You cannot save a config outside of a project directory'));
return;
}
const { packageJsonConfig, packageJsonIndent, dojoRcConfig, dojoRcIndent } = parseConfigs();
const hasPackageConfig = typeof packageJsonConfig === 'object';
const hasDojoRcConfig = typeof dojoRcConfig === 'object';
const updateConfig = dojoRcConfig || packageJsonConfig || {};
const commmandConfig = updateConfig[this._configurationKey] || {};
Object.assign(commmandConfig, config);
Object.assign(updateConfig, { [this._configurationKey]: commmandConfig });
if (!hasDojoRcConfig && hasPackageConfig) {
writePackageConfig(updateConfig, packageJsonIndent || defaultIndent);
}
else {
writeDojoRcConfig(updateConfig, dojoRcIndent || defaultIndent);
}
}
}
class ConfigurationHelperFactory {
sandbox(groupName, commandName) {
if (!this._dojoRcName) {
this._dojoRcName = getDojoRcConfigOption();
if (appPath) {
dojoRcPath = path_1.join(appPath, this._dojoRcName);
}
}
return new SingleCommandConfigurationHelper(groupName, commandName);
}
}
exports.ConfigurationHelperFactory = ConfigurationHelperFactory;
exports.default = new ConfigurationHelperFactory();
//# sourceMappingURL=configurationHelper.js.map