Skip to content

Commit

Permalink
feat: replace handlebars with template strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
aorinevo committed Mar 28, 2020
1 parent cd0bd3b commit 4f63ec4
Show file tree
Hide file tree
Showing 17 changed files with 1,074 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .labrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
enableTemplateStringHTMLReporter: 'true'
};
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
- [Changelog](https://hapi.dev/family/lab/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)


# Notes
- Replacing Handlebars with template strings. Implement similar to https://medium.com/your-majesty-co/frameworkless-javascript-template-literals-the-best-thing-since-sliced-bread-d97f000ce955
21 changes: 19 additions & 2 deletions lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ const Handlebars = require('handlebars');
const Hoek = require('@hapi/hoek');
const SourceMap = require('source-map');
const SourceMapSupport = require('source-map-support');
const { reportTemplate } = require('./html/report.js');

const FindRc = require('find-rc');

const internals = {};

internals.rcPath = FindRc('lab');
/* $lab:coverage:off$ */
internals.rc = internals.rcPath ? require(internals.rcPath) : {};
/* $lab:coverage:on$ */

const { enableTemplateStringHTMLReporter } = internals.rc;


exports = module.exports = internals.Reporter = function (options) {

this.settings = options;

const filename = Path.join(__dirname, 'html', 'report.html');
const template = Fs.readFileSync(filename, 'utf8');

/* $lab:coverage:off$ */
// Display all valid numbers except zeros
Handlebars.registerHelper('number', (number) => {

Expand Down Expand Up @@ -60,6 +69,7 @@ exports = module.exports = internals.Reporter = function (options) {
const stack = err.stack.slice(err.stack.indexOf('\n') + 1).replace(/^\s*/gm, ' ');
return new Handlebars.SafeString(Hoek.escapeHtml(stack));
});
/* $lab:coverage:on$ */

const partialsPath = Path.join(__dirname, 'html', 'partials');
const partials = Fs.readdirSync(partialsPath);
Expand Down Expand Up @@ -284,7 +294,14 @@ internals.Reporter.prototype.end = async function (notebook) {
}, this);
}

this.report(this.view(context));
/* $lab:coverage:off$ */
if (enableTemplateStringHTMLReporter === 'true') {
this.report(reportTemplate(context));
}
else {
this.report(this.view(context));
}
/* $lab:coverage:on$ */
};

internals.findLint = function (lint, file) {
Expand Down
45 changes: 45 additions & 0 deletions lib/reporters/html/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const Hoek = require('@hapi/hoek');

exports.replace = (str, from, to, flags) => {

return str.replace(new RegExp(from, flags), to);
};

// Display all valid numbers except zeros
exports.number = (number) => {

return +number || '';
};

exports.join = (array, separator) => {

return array.join(separator);
};

exports.lintJoin = (array) => {

let str = '';

for (let i = 0; i < array.length; ++i) {
if (str) {
str += '&#xa;'; // This is a line break
}

str += Hoek.escapeHtml(array[i]);
}

return `${str}`;
};

exports.errorMessage = (err) => {

return `${Hoek.escapeHtml('' + err.message)}`;
};

exports.errorStack = (err) => {

const stack = err.stack.slice(err.stack.indexOf('\n') + 1).replace(/^\s*/gm, ' ');
return `${Hoek.escapeHtml(stack)}`;
};
26 changes: 26 additions & 0 deletions lib/reporters/html/partials/cov.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { file } = require('./file');

exports.cov = (coverage) => {

return `<div id="coverage">
<h1>Code Coverage Report</h1>
<div class="stats ${coverage.percentClass}">
<div class="percentage">${coverage.percent}%</div>
<div class="sloc">${coverage.cov.sloc}</div>
<div class="hits">${coverage.cov.hits}</div>
<div class="misses">${coverage.cov.misses}</div>
</div>
<div id="filters">
<input type="checkbox" checked="" onchange="filter(this)" value="generated" id="show-generated">
<label for="show-generated">Show generated files</label>
</div>
<div id="files">
${coverage.cov.files.map((item, i) => {
return file(item);
})}
</div>
</div>`;
};
Loading

0 comments on commit 4f63ec4

Please sign in to comment.