diff --git a/cli/index.js b/cli/index.js index 893ab7f21..6b982a0c5 100755 --- a/cli/index.js +++ b/cli/index.js @@ -11,9 +11,7 @@ function main () { const argsOptions = parseArgs(process.argv.slice(2), { boolean: ['h', 'help', 'v', 'version', 'i', 'docker'], string: ['config'], - default: { - config: 'backstop.json' - } + default: {} }); // Catch errors from failing promises diff --git a/core/util/makeConfig.js b/core/util/makeConfig.js index f7f87b608..32e0397fd 100644 --- a/core/util/makeConfig.js +++ b/core/util/makeConfig.js @@ -1,32 +1,44 @@ const path = require('path'); +const fs = require('fs'); const extendConfig = require('./extendConfig'); +const logger = require('../util/logger')('init'); const NON_CONFIG_COMMANDS = ['init', 'version', 'stop']; -function projectPath (config) { +function projectPath () { return process.cwd(); } function loadProjectConfig (command, options, config) { + options = options || {}; // make sure options is an object + // TEST REPORT FILE NAME - const customTestReportFileName = options && (options.testReportFileName || null); - if (customTestReportFileName) { - config.testReportFileName = options.testReportFileName || null; + if (options.testReportFileName) { + config.testReportFileName = options.testReportFileName; } - let customConfigPath = options && (options.backstopConfigFilePath || options.configPath); - if (options && typeof options.config === 'string' && !customConfigPath) { - customConfigPath = options.config; + // list of "config" files to search for + const configFiles = ['backstop.json', 'backstop.js']; + + // If a config file is specified in the options, add it to the list + if (options.config && typeof options.config === 'string') { + configFiles.unshift(options.config); + configFiles.unshift(path.join(config.projectPath, options.config)); } - if (customConfigPath) { - if (path.isAbsolute(customConfigPath)) { - config.backstopConfigFileName = customConfigPath; - } else { - config.backstopConfigFileName = path.join(config.projectPath, customConfigPath); + // Searching for the first existing config file + for (const configFile of configFiles) { + const configPath = path.join(config.projectPath, configFile); + if (fs.existsSync(configPath)) { + config.backstopConfigFileName = configPath; + break; // Stop searching once a valid config file is found } - } else { - config.backstopConfigFileName = path.join(config.projectPath, 'backstop.json'); + } + + if (!config.backstopConfigFileName) { + logger.error('No config file found in project path: ' + config.projectPath); + logger.error('Looked for: ' + configFiles.join(', ')); + process.exit(1); } let userConfig = {};