Skip to content

Commit

Permalink
fix backwards compatibility with node 4
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgruber committed Dec 28, 2016
1 parent be4e58b commit de62f06
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
webpack/
coverage/
dist/
lib/main-html.js
lib/main-html.js
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#Changelog

###1.0.2
- Transpile `bin` and `lib` for compatibility with node 4

###1.0.1
- Better url handling in context

Expand Down
50 changes: 29 additions & 21 deletions bin/cli-main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

/* eslint-disable no-console */
const path = require('path');
const fs = require('fs-extra');
const report = require('../lib/main');
var path = require('path');
var fs = require('fs-extra');
var report = require('../lib/main');

const JsonErrRegex = /^Unexpected token .* in JSON/;
const fileExtRegex = /\.[^.]*?$/;
const ERRORS = {
var JsonErrRegex = /^Unexpected token .* in JSON/;
var fileExtRegex = /\.[^.]*?$/;
var ERRORS = {
NO_FILE: 'You must supply a mochawesome data file to create a report.',
BAD_JSON: 'There was a problem parsing mochawesome data. Please ensure the JSON file is valid.',
GENERIC: 'There was a problem loading mochawesome data.'
Expand All @@ -16,7 +18,7 @@ const ERRORS = {
*
*/
function validateInFile(dataInFile) {
let dataIn;
var dataIn = void 0;
// Was a JSON file provided?
if (!dataInFile) {
return { err: ERRORS.NO_FILE };
Expand All @@ -27,7 +29,7 @@ function validateInFile(dataInFile) {
dataIn = JSON.parse(fs.readFileSync(dataInFile, 'utf-8'));
} catch (err) {
if (err.code === 'ENOENT') {
return { err: `The data file: ${dataInFile} could not be found.` };
return { err: 'The data file: ' + dataInFile + ' could not be found.' };
} else if (JsonErrRegex.test(err.message)) {
return { err: ERRORS.BAD_JSON };
}
Expand All @@ -42,17 +44,24 @@ function validateInFile(dataInFile) {
*
*/
function getOptions(args) {
const { reportFilename, reportDir, reportTitle, reportPageTitle,
inlineAssets, enableCharts, enableCode, dev } = args;
const filename = `${reportFilename.replace(fileExtRegex, '')}.html`;
var reportFilename = args.reportFilename,
reportDir = args.reportDir,
reportTitle = args.reportTitle,
reportPageTitle = args.reportPageTitle,
inlineAssets = args.inlineAssets,
enableCharts = args.enableCharts,
enableCode = args.enableCode,
dev = args.dev;

var filename = reportFilename.replace(fileExtRegex, '') + '.html';
return {
reportHtmlFile: path.join(reportDir, filename),
reportTitle,
reportPageTitle,
inlineAssets,
enableCharts,
enableCode,
dev
reportTitle: reportTitle,
reportPageTitle: reportPageTitle,
inlineAssets: inlineAssets,
enableCharts: enableCharts,
enableCode: enableCode,
dev: dev
};
}

Expand All @@ -61,11 +70,10 @@ function getOptions(args) {
*
*/
function mareport(processArgs) {
const args = processArgs || { _: [] };
// console.log(args);
var args = processArgs || { _: [] };

// Try to load the test data
const reportData = validateInFile(args._[0]);
var reportData = validateInFile(args._[0]);

// Check for error in data load
/* istanbul ignore else */
Expand All @@ -79,4 +87,4 @@ function mareport(processArgs) {
report.createSync(reportData, getOptions(args));
}

module.exports = mareport;
module.exports = mareport;
124 changes: 60 additions & 64 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node
const path = require('path');
const yargs = require('yargs');
const mareport = require('./cli-main');
'use strict';

var path = require('path');
var yargs = require('yargs');
var mareport = require('./cli-main');

/* Required Args
* @argument {string} test-data Data to use for rendering report
Expand All @@ -20,66 +22,60 @@ const mareport = require('./cli-main');
*/

// Setup yargs
yargs
.usage('Usage: $0 [data-in-file] [options]')
.options({
f: {
alias: [ 'reportFilename' ],
default: 'mochawesome',
describe: 'Filename of saved report',
string: true,
requiresArg: true
},
o: {
alias: [ 'reportDir' ],
default: path.join(process.cwd(), 'mochawesome-report'),
describe: 'Path to save report',
string: true,
normalize: true,
requiresArg: true
},
t: {
alias: [ 'reportTitle' ],
default: 'mochawesome',
describe: 'Report title',
string: true,
requiresArg: true
},
p: {
alias: [ 'reportPageTitle' ],
default: 'mochawesome-report',
describe: 'Browser title',
string: true,
requiresArg: true
},
i: {
alias: [ 'inline', 'inlineAssets' ],
default: false,
describe: 'Inline report assets (styles, scripts)',
boolean: true
},
charts: {
alias: [ 'enableCharts' ],
default: true,
describe: 'Display charts',
boolean: true
},
code: {
alias: [ 'enableCode' ],
default: true,
describe: 'Display test code',
boolean: true
},
dev: {
default: false,
describe: 'Enable dev mode',
boolean: true
}
})
.help('help')
.alias('h', 'help')
.epilog('Copyright 2016 Adam Gruber')
.argv;
yargs.usage('Usage: $0 [data-in-file] [options]').options({
f: {
alias: ['reportFilename'],
default: 'mochawesome',
describe: 'Filename of saved report',
string: true,
requiresArg: true
},
o: {
alias: ['reportDir'],
default: path.join(process.cwd(), 'mochawesome-report'),
describe: 'Path to save report',
string: true,
normalize: true,
requiresArg: true
},
t: {
alias: ['reportTitle'],
default: 'mochawesome',
describe: 'Report title',
string: true,
requiresArg: true
},
p: {
alias: ['reportPageTitle'],
default: 'mochawesome-report',
describe: 'Browser title',
string: true,
requiresArg: true
},
i: {
alias: ['inline', 'inlineAssets'],
default: false,
describe: 'Inline report assets (styles, scripts)',
boolean: true
},
charts: {
alias: ['enableCharts'],
default: true,
describe: 'Display charts',
boolean: true
},
code: {
alias: ['enableCode'],
default: true,
describe: 'Display test code',
boolean: true
},
dev: {
default: false,
describe: 'Enable dev mode',
boolean: true
}
}).help('help').alias('h', 'help').epilog('Copyright 2016 Adam Gruber').argv;

// Call the main cli program
mareport(yargs.argv);
mareport(yargs.argv);
81 changes: 81 additions & 0 deletions bin/src/cli-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable no-console */
const path = require('path');
const fs = require('fs-extra');
const report = require('../lib/main');

const JsonErrRegex = /^Unexpected token .* in JSON/;
const fileExtRegex = /\.[^.]*?$/;
const ERRORS = {
NO_FILE: 'You must supply a mochawesome data file to create a report.',
BAD_JSON: 'There was a problem parsing mochawesome data. Please ensure the JSON file is valid.',
GENERIC: 'There was a problem loading mochawesome data.'
};

/**
* Validate the data file
*
*/
function validateInFile(dataInFile) {
let dataIn;
// Was a JSON file provided?
if (!dataInFile) {
return { err: ERRORS.NO_FILE };
}

// Try to read and parse the file
try {
dataIn = JSON.parse(fs.readFileSync(dataInFile, 'utf-8'));
} catch (err) {
if (err.code === 'ENOENT') {
return { err: `The data file: ${dataInFile} could not be found.` };
} else if (JsonErrRegex.test(err.message)) {
return { err: ERRORS.BAD_JSON };
}
return { err: ERRORS.GENERIC };
}

return dataIn;
}

/**
* Get options to send to report generator
*
*/
function getOptions(args) {
const { reportFilename, reportDir, reportTitle, reportPageTitle,
inlineAssets, enableCharts, enableCode, dev } = args;
const filename = `${reportFilename.replace(fileExtRegex, '')}.html`;
return {
reportHtmlFile: path.join(reportDir, filename),
reportTitle,
reportPageTitle,
inlineAssets,
enableCharts,
enableCode,
dev
};
}

/**
* Main CLI Program
*
*/
function mareport(processArgs) {
const args = processArgs || { _: [] };

// Try to load the test data
const reportData = validateInFile(args._[0]);

// Check for error in data load
/* istanbul ignore else */
if (reportData && reportData.err) {
console.log(reportData.err);
process.exitCode = 1;
return;
}

// Good so far, now generate the report
report.createSync(reportData, getOptions(args));
}

module.exports = mareport;
Loading

0 comments on commit de62f06

Please sign in to comment.