diff --git a/Gulpfile.js b/Gulpfile.js index eb40d3e..1c89a15 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -16,6 +16,7 @@ var APP_DIR = path.join(__dirname, 'test/data/test-app-regular'); var ASAR_ARCHIVE_PATH = path.join(__dirname, 'test/data/test-app.asar'); var CONFIG_PATH_REGULAR = path.join(__dirname, 'test/data/app-config-regular'); var CONFIG_PATH_ASAR = path.join(__dirname, 'test/data/app-config-asar'); +var CONFIG_PATH_JS = path.join(__dirname, 'test/data/app-config-js'); function clean () { return del(['lib', '.screenshots']); @@ -54,11 +55,24 @@ function testAsarApp () { return childProcess.spawn('testcafe', ['electron:' + CONFIG_PATH_ASAR, 'test/fixtures/**/*-test.js', '-s', '.screenshots'], { shell: true, stdio: 'inherit' }); } +function testJsConfig () { + delete process.env.ASAR_MODE; + + return childProcess.spawn('testcafe', ['electron:' + CONFIG_PATH_JS, 'test/fixtures/**/*-test.js', '-s', '.screenshots'], { shell: true, stdio: 'inherit' }); +} +function testJsConfigSpecifically () { + delete process.env.ASAR_MODE; + + return childProcess.spawn('testcafe', ['electron:' + CONFIG_PATH_JS + '/.testcafe-electron-rc.js', 'test/fixtures/**/*-test.js', '-s', '.screenshots'], { shell: true, stdio: 'inherit' }); +} + exports.lint = lint; exports.build = gulp.parallel(lint, gulp.series(clean, build)); exports.test = gulp.series( exports.build, testRegularApp, + testJsConfigSpecifically, + testJsConfig, 'pack-to-asar-archive', testAsarApp ); diff --git a/src/utils/get-config.js b/src/utils/get-config.js index f0c1d44..8321b88 100644 --- a/src/utils/get-config.js +++ b/src/utils/get-config.js @@ -1,4 +1,4 @@ -import { readFileSync, statSync } from 'fs'; +import { readFileSync, statSync, existsSync } from 'fs'; import path from 'path'; import resolveFileUrl from './resolve-file-url'; import isAbsolute from './is-absolute'; @@ -8,13 +8,40 @@ import CONSTANTS from '../constants'; const PROTOCOL_RE = /^([\w-]+?)(?=\:\/\/)/; export default function (id, mainPath) { - if (statSync(mainPath).isDirectory()) - mainPath = path.join(mainPath, CONSTANTS.configFileName); + let configPath = mainPath; + let config; + let mainDir; - var mainDir = path.dirname(mainPath); - var configString = readFileSync(mainPath).toString(); + if (statSync(mainPath).isDirectory()) { + mainDir = mainPath; - var config = JSON.parse(configString); + const allowedExtensions = [ '.js', '.json' ]; + + // get first allowed extension config file that exists. + allowedExtensions.some( ext => { + const possibleConfig = path.join(mainPath, CONSTANTS.configFileName + ext ); + + const exists = existsSync( possibleConfig ); + + if ( exists ) + configPath = possibleConfig; + + return exists; + }); + } + + mainDir = mainDir || path.dirname(mainPath); + + // check if we have a specific ext and can use require. + if ( path.extname( configPath ) ) + config = require( configPath ); + + else { + // fall back to .testcafe-electron-rc file w/ no extension + const configString = readFileSync(path.join(mainPath, CONSTANTS.configFileName )).toString(); + + config = JSON.parse(configString); + } if (config.appPath && !isAbsolute(config.appPath)) config.appPath = path.resolve(mainDir, config.appPath); diff --git a/test/data/app-config-js/.testcafe-electron-rc.js b/test/data/app-config-js/.testcafe-electron-rc.js new file mode 100644 index 0000000..07b4131 --- /dev/null +++ b/test/data/app-config-js/.testcafe-electron-rc.js @@ -0,0 +1,4 @@ +module.exports = { + "mainWindowUrl": "../test-app-regular/index.html", + "appPath": "../test-app-regular" +}