Skip to content

Commit

Permalink
docs and build [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgruber committed Mar 24, 2017
1 parent 8ac5b26 commit dfe5044
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Changelog

###1.1.2
- Fix an issue where autoOpen did not work on Windows [mochawesome #142](https://github.com/adamgruber/mochawesome/issues/144)
- Add `autoOpen` option to CLI

###1.1.1
- Fix an inconsistency between the diff output in the console and the diff output in the report [mochawesome #142](https://github.com/adamgruber/mochawesome/issues/142)
- Fix an issue where the report assets would not get updated after upgrading package version. [mochawesome #138](https://github.com/adamgruber/mochawesome/issues/138)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Flag | Type | Default | Description
-i, --inline | boolean | false | Inline report assets (scripts, styles)
--charts | boolean | true | Display Suite charts
--code | boolean | true | Display test code
--autoOpen | boolean | false | Automatically open the report
--dev | boolean | false | Enable dev mode (requires local webpack dev server)
-h, --help | | | Show CLI help

Expand Down
9 changes: 9 additions & 0 deletions bin/cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var ERRORS = {
/**
* Validate the data file
*
* @param {string} dataInFile Filename of test data
*
* @return {Object} JSON test data if valid, otherwise error object { err: message }
*/
function validateInFile(dataInFile) {
var dataIn = void 0;
Expand Down Expand Up @@ -60,6 +63,9 @@ function validateInFile(dataInFile) {
/**
* Get options to send to report generator
*
* @param {Object} args Arguments passed in
*
* @return {Object} Options to pass to report generator
*/
function getOptions(args) {
var reportFilename = args.reportFilename,
Expand All @@ -69,6 +75,7 @@ function getOptions(args) {
inlineAssets = args.inlineAssets,
enableCharts = args.enableCharts,
enableCode = args.enableCode,
autoOpen = args.autoOpen,
dev = args.dev;

var filename = reportFilename.replace(fileExtRegex, '') + '.html';
Expand All @@ -79,13 +86,15 @@ function getOptions(args) {
inlineAssets: inlineAssets,
enableCharts: enableCharts,
enableCode: enableCode,
autoOpen: autoOpen,
dev: dev
};
}

/**
* Main CLI Program
*
* @param {Object} processArgs CLI arguments
*/
function mareport(processArgs) {
var args = processArgs || { _: [] };
Expand Down
6 changes: 6 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var mareport = require('./cli-main');
* @property {boolean} inlineAssets Should assets be inlined into HTML file (default: false)
* @property {boolean} charts Should charts be enabled (default: true)
* @property {boolean} code Should test code output be enabled (default: true)
* @property {boolean} autoOpen Open the report after creation (default: false)
* @property {boolean} dev Enable dev mode in the report,
* asssets loaded via webpack (default: false)
*/
Expand Down Expand Up @@ -70,6 +71,11 @@ yargs.usage('Usage: $0 [data-in-file] [options]').options({
describe: 'Display test code',
boolean: true
},
autoOpen: {
default: false,
describe: 'Automatically open the report HTML',
boolean: true
},
dev: {
default: false,
describe: 'Enable dev mode',
Expand Down
87 changes: 54 additions & 33 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@ var inlineAssetsDir = path.join(distDir, 'assets', 'inline');
var externalAssetsDir = path.join(distDir, 'assets', 'external');

/**
* Private functions
* Saves a file
*
* @param {string} filename Name of file to save
* @param {string} data Data to be saved
*
* @return {Promise} Resolves if file has been successfully saved
*/
function saveFile(filename, data) {
return new Promise(function (resolve, reject) {
fs.outputFile(filename, data, function (err) {
return err === null ? resolve(true) : reject(err);
});
});
}

/**
* Saves a file
* Opens a file
*
* @param {string} filename Name of file to open
*
* @param {String} filename
* @param {String} data
* @returns {Promise}
* @return {Promise} Resolves if file has been successfully opened
*/

function saveFile(filename, data) {
function openFile(filename) {
return new Promise(function (resolve, reject) {
fs.outputFile(filename, data, function (err) {
opener(filename, null, function (err) {
return err === null ? resolve(true) : reject(err);
});
});
Expand All @@ -38,10 +48,10 @@ function saveFile(filename, data) {
/**
* Synchronously loads a file with utf8 encoding
*
* @param {String} filename
* @returns {String}
* @param {string} filename Name of file to load
*
* @return {string} File data as string
*/

function loadFile(filename) {
return fs.readFileSync(filename, 'utf8');
}
Expand All @@ -50,10 +60,10 @@ function loadFile(filename) {
* Get report options by extending base options
* with user provided options
*
* @param {Object} opts
* @returns {Object}
* @param {Object} opts Report options
*
* @return {Object} User options merged with default options
*/

function getOptions(opts) {
var userOptions = opts || {};
var baseOptions = {
Expand All @@ -76,10 +86,8 @@ function getOptions(opts) {
/**
* Get the report assets for inline use
*
* @param {Object} opts
* @returns {Object}
* @return {Object} Object with styles and scripts as strings
*/

function getAssets() {
return {
styles: loadFile(path.join(inlineAssetsDir, 'app.css')),
Expand All @@ -98,10 +106,9 @@ function getAssets() {
* - Asset version is not found -> copy assets
* - Asset version differs from current version -> copy assets
*
* @param {Object} opts
* @returns {Object}
* @param {Object} opts Report options
*
*/

function copyAssets(opts) {
var assetsDir = path.join(opts.reportDir, 'assets');
var assetsExist = fs.existsSync(assetsDir);
Expand All @@ -123,25 +130,25 @@ function copyAssets(opts) {
/**
* Renders the main report React component
*
* @param {JSON} data
* @param {Object} options
* @param {String} styles
* @param {String} scripts
* @returns {String} html
* @param {Object} data JSON test data
* @param {Object} options Report options
* @param {string} styles Inline stylesheet
* @param {string} scripts Inline script
*
* @return {string} Rendered HTML string
*/

function renderHtml(data, options, styles, scripts) {
return render(React.createElement(MainHTML, { data: data, options: options, styles: styles, scripts: scripts }));
}

/**
* Prepare options, assets, and html for saving
*
* @param {String} data
* @param {Object} opts
* @returns {Object}
* @param {string} reportData JSON test data
* @param {Object} opts Report options
*
* @return {Object} Prepared data for saving
*/

function prepare(reportData, opts) {
// Stringify the data if needed
var data = reportData;
Expand Down Expand Up @@ -174,21 +181,31 @@ function prepare(reportData, opts) {
}

/**
* Exposed functions
* Create the report
*
* @param {string} data JSON test data
* @param {Object} opts Report options
*
* @return {Promise} Resolves if report was created successfully
*/

function create(data, opts) {
var _prepare = prepare(data, opts),
html = _prepare.html,
reportFilename = _prepare.reportFilename,
options = _prepare.options;

return saveFile(reportFilename, html).then(function () {
if (options.autoOpen) opener(reportFilename);
return options.autoOpen && openFile(reportFilename);
});
}

/**
* Create the report synchronously
*
* @param {string} data JSON test data
* @param {Object} opts Report options
*
*/
function createSync(data, opts) {
var _prepare2 = prepare(data, opts),
html = _prepare2.html,
Expand All @@ -199,4 +216,8 @@ function createSync(data, opts) {
if (options.autoOpen) opener(reportFilename);
}

/**
* Expose create/createSync functions
*
*/
module.exports = { create: create, createSync: createSync };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mochawesome-report-generator",
"version": "1.1.1",
"version": "1.1.2",
"description": "Generates gorgeous HTML reports from mochawesome reporter.",
"scripts": {
"lint": "eslint bin/src lib/src src/js src/components/**/*.js src/components/**/*.jsx test/**/*.js test/**/*.jsx",
Expand Down

0 comments on commit dfe5044

Please sign in to comment.