Skip to content

Commit

Permalink
feat: Parse json or js config (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef authored Feb 3, 2020
1 parent 66805d0 commit 80449ac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
14 changes: 14 additions & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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
);
39 changes: 33 additions & 6 deletions src/utils/get-config.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/data/app-config-js/.testcafe-electron-rc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"mainWindowUrl": "../test-app-regular/index.html",
"appPath": "../test-app-regular"
}

0 comments on commit 80449ac

Please sign in to comment.