sda
Version:
Software development assistant
73 lines • 3.29 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const lodash_1 = __importDefault(require("lodash"));
const Log_1 = __importDefault(require("../Log"));
const Constants_1 = require("./Constants");
const getConfigPaths_1 = require("./getConfigPaths");
const replaceConfigWithAbsolutePaths_1 = __importDefault(require("./replaceConfigWithAbsolutePaths"));
const warnOldConfigFilePaths_1 = __importDefault(require("./warnOldConfigFilePaths"));
function getConfig(rootDir, argsConfigPath) {
// Warn about old config file name for backwards compatibility
warnOldConfigFilePaths_1.default(rootDir);
const paths = getConfigPaths_1.getAllConfigPaths(rootDir, argsConfigPath);
// Make a copy of the empty config, as this keeps geing updated in-place
let config = lodash_1.default.cloneDeep(Constants_1.EMPTY_CONFIG);
for (const filePath of paths) {
config = mergeConfig(config, filePath);
}
config = backfillOrphanEnvs(config);
return config;
}
exports.default = getConfig;
/**
* An environment may point at a template defined elsehere.
* If missing try to get it from the environment folder.
*/
function backfillOrphanEnvs(config) {
Object.keys(config.environments).forEach((envId) => {
const env = config.environments[envId];
if (!config.templates[env.templateId]) {
Log_1.default.verbose(`Environment "${envId}" is missing the template. Looking in the environment folder.`);
const configPathsFromEnv = getConfigPaths_1.getConfigPaths(env.path);
for (const filePath of configPathsFromEnv) {
config = mergeConfig(config, filePath, true);
}
}
});
return config;
}
/**
* Merges the contents of a config file with an IConfig object.
* In case of conflicts, the contents of the config file are used.
* If backfill flag is on, the new config file won't take priority over the existing config.
*/
function mergeConfig(config, filePath, backfill) {
try {
const configFile = fs.readFileSync(filePath, 'utf8');
const newConfig = JSON.parse(configFile);
if (!backfill) {
Log_1.default.verbose(`Merging config with file "${filePath}"`);
config = lodash_1.default.merge(config, replaceConfigWithAbsolutePaths_1.default(newConfig, filePath));
}
else {
Log_1.default.verbose(`Merging config with file "${filePath}" in shy mode`);
config = lodash_1.default.merge(replaceConfigWithAbsolutePaths_1.default(newConfig, filePath), config);
}
}
catch (error) {
Log_1.default.log(`WARNING: File "${filePath}" is invalid.`);
}
return config;
}
//# sourceMappingURL=getConfig.js.map