Skip to content

Commit

Permalink
refactor(bin/options.js): Refactor and improve documentation (#3533)
Browse files Browse the repository at this point in the history
  • Loading branch information
plroebuck authored Oct 30, 2018
1 parent 741c4c7 commit 835ac33
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions bin/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,54 @@ const fs = require('fs');
module.exports = getOptions;

/**
* Get options.
* Default pathname for run-control file.
*
* @constant
* @type {string}
* @default
*/
const defaultPathname = 'test/mocha.opts';

/**
* Reads contents of the run-control file.
*
* @private
* @param {string} pathname - Pathname of run-control file.
* @returns {string} file contents
*/
function readOptionsFile(pathname) {
return fs.readFileSync(pathname, 'utf8');
}

/**
* Parses options read from run-control file.
*
* @private
* @param {string} content - Content read from run-control file.
* @returns {string[]} cmdline options (and associated arguments)
*/
function parseOptions(content) {
/*
* Replaces comments with empty strings
* Replaces escaped spaces (e.g., 'xxx\ yyy') with HTML space
* Splits on whitespace, creating array of substrings
* Filters empty string elements from array
* Replaces any HTML space with space
*/
return content
.replace(/^#.*$/gm, '')
.replace(/\\\s/g, '%20')
.split(/\s/)
.filter(Boolean)
.map(value => value.replace(/%20/g, ' '));
}

/**
* Prepends options from run-control file to the command line arguments.
*
* @public
* @see {@link https://mochajs.org/#mochaopts|mocha.opts}
*/
function getOptions() {
if (
process.argv.length === 3 &&
Expand All @@ -26,17 +71,11 @@ function getOptions() {

const optsPath =
process.argv.indexOf('--opts') === -1
? 'test/mocha.opts'
? defaultPathname
: process.argv[process.argv.indexOf('--opts') + 1];

try {
const opts = fs
.readFileSync(optsPath, 'utf8')
.replace(/^#.*$/gm, '')
.replace(/\\\s/g, '%20')
.split(/\s/)
.filter(Boolean)
.map(value => value.replace(/%20/g, ' '));
const opts = parseOptions(readOptionsFile(optsPath));

process.argv = process.argv
.slice(0, 2)
Expand Down

0 comments on commit 835ac33

Please sign in to comment.