From 7be08b6a7b0160363f90d4f5ca1970a230d3a266 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 30 Apr 2024 12:04:33 +0200 Subject: [PATCH 1/3] Use any config file: backstop.json, backstop.js --- cli/index.js | 4 +--- core/util/makeConfig.js | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 17 deletions(-) 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..9862ce545 100644 --- a/core/util/makeConfig.js +++ b/core/util/makeConfig.js @@ -1,32 +1,38 @@ const path = require('path'); +const fs = require('fs'); const extendConfig = require('./extendConfig'); 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; } + const configFiles = ['backstop.json', 'backstop.js']; - let customConfigPath = options && (options.backstopConfigFilePath || options.configPath); - if (options && typeof options.config === 'string' && !customConfigPath) { - customConfigPath = options.config; + 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) { + console.error('No config file found in project path: ', config.projectPath); } let userConfig = {}; From d0a147232c3f941e38d6fc6f94c79d9173896110 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 30 Apr 2024 12:06:42 +0200 Subject: [PATCH 2/3] Use any config file: backstop.json, backstop.js --- core/util/makeConfig.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/util/makeConfig.js b/core/util/makeConfig.js index 9862ce545..1b88e9698 100644 --- a/core/util/makeConfig.js +++ b/core/util/makeConfig.js @@ -15,8 +15,11 @@ function loadProjectConfig (command, options, config) { if (options.testReportFileName) { config.testReportFileName = options.testReportFileName; } + + // 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)); From 17a80450122d5ea5f693c252f474116a7175d5ff Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 30 Apr 2024 12:12:10 +0200 Subject: [PATCH 3/3] Use any config file: backstop.json, backstop.js --- core/util/makeConfig.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/util/makeConfig.js b/core/util/makeConfig.js index 1b88e9698..32e0397fd 100644 --- a/core/util/makeConfig.js +++ b/core/util/makeConfig.js @@ -1,6 +1,7 @@ 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']; @@ -35,7 +36,9 @@ function loadProjectConfig (command, options, config) { } if (!config.backstopConfigFileName) { - console.error('No config file found in project path: ', config.projectPath); + logger.error('No config file found in project path: ' + config.projectPath); + logger.error('Looked for: ' + configFiles.join(', ')); + process.exit(1); } let userConfig = {};