stryker
Version:
The extendable JavaScript mutation testing framework
88 lines • 3.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var fs = require("fs");
var path = require("path");
var config_1 = require("stryker-api/config");
var util_1 = require("@stryker-mutator/util");
var di_1 = require("../di");
var plugin_1 = require("stryker-api/plugin");
exports.CONFIG_SYNTAX_HELP = ' module.exports = function(config) {\n' +
' config.set({\n' +
' // your config\n' +
' });\n' +
' };';
var DEFAULT_CONFIG_FILE = 'stryker.conf.js';
var ConfigReader = /** @class */ (function () {
function ConfigReader(cliOptions, log) {
this.cliOptions = cliOptions;
this.log = log;
}
ConfigReader.prototype.readConfig = function () {
var configModule = this.loadConfigModule();
var config = new config_1.Config();
try {
configModule(config);
}
catch (e) {
throw new util_1.StrykerError('Error in config file!', e);
}
// merge the config from config file and cliOptions (precedence)
config.set(this.cliOptions);
if (config.reporter.length) {
if (Array.isArray(config.reporter)) {
config.reporters = config.reporter;
}
else {
config.reporters = [config.reporter];
}
this.log.warn("DEPRECATED: please change the config setting 'reporter: " + JSON.stringify(config.reporter) + "' into 'reporters: " + JSON.stringify(config.reporters) + "'");
}
if (config.timeoutMs) {
config.timeoutMS = config.timeoutMs;
this.log.warn("DEPRECATED: please change the config setting 'timeoutMs: " + config.timeoutMs + "' into 'timeoutMS: " + config.timeoutMS + "'");
}
return config;
};
ConfigReader.prototype.loadConfigModule = function () {
// Dummy module to be returned if no config file is loaded.
var configModule = function () { };
if (!this.cliOptions.configFile) {
try {
fs.accessSync(path.resolve("./" + DEFAULT_CONFIG_FILE));
this.log.info("Using " + DEFAULT_CONFIG_FILE + " in the current working directory.");
this.cliOptions.configFile = DEFAULT_CONFIG_FILE;
}
catch (e) {
this.log.info('No config file specified. Running with command line arguments.');
this.log.info('Use `stryker init` command to generate your config file.');
}
}
if (this.cliOptions.configFile) {
this.log.debug("Loading config " + this.cliOptions.configFile);
var configFileName = path.resolve(this.cliOptions.configFile);
try {
configModule = require(configFileName);
}
catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(this.cliOptions.configFile) !== -1) {
throw new util_1.StrykerError("File " + configFileName + " does not exist!", e);
}
else {
this.log.info('Stryker can help you setup a `stryker.conf` file for your project.');
this.log.info('Please execute `stryker init` in your project\'s root directory.');
throw new util_1.StrykerError('Invalid config file', e);
}
}
if (!_.isFunction(configModule)) {
this.log.fatal('Config file must export a function!\n' + exports.CONFIG_SYNTAX_HELP);
throw new util_1.StrykerError('Config file must export a function!');
}
}
return configModule;
};
ConfigReader.inject = plugin_1.tokens(di_1.coreTokens.cliOptions, plugin_1.commonTokens.logger);
return ConfigReader;
}());
exports.default = ConfigReader;
//# sourceMappingURL=ConfigReader.js.map
;