diff --git a/CHANGELOG.md b/CHANGELOG.md index 43c8770..fcb5a9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.3.2 (05/23/2018) + +- cleans up timing logic + # 1.3.1 (04/11/2018) - fixes issue with `development-toast` being hidden behind page text diff --git a/bin/sweeney.js b/bin/sweeney.js index f6d1747..eba3520 100755 --- a/bin/sweeney.js +++ b/bin/sweeney.js @@ -8,7 +8,7 @@ const { version } = require('../package.json') const Site = require('../lib/site') const Serve = require('../lib/serve/serve') -const { ms, getConfig, copyDirectory, renderSubDepends } = require('../lib/util') +const { time, getConfig, copyDirectory, renderSubDepends } = require('../lib/util') process.on('unhandledRejection', (error) => { console.error(`Error: \n ${error.stack}`); // eslint-disable-line @@ -82,13 +82,13 @@ async function getConfigInDirectory () { (async function () { try { if (program.new) { - const start = process.hrtime() const directory = process.cwd() - await copyDirectory(path.resolve(__dirname, '..', 'example'), directory) - const end = process.hrtime(start) + const _time = await time(async () => { + await copyDirectory(path.resolve(__dirname, '..', 'example'), directory) + }) - process.stdout.write(`application bootstrapped in ${directory} [${ms(((end[0] * 1e9) + end[1]) / 1e6)}]`) + process.stdout.write(`application bootstrapped in ${directory} [${_time}]`) process.exit(1) } @@ -100,21 +100,19 @@ async function getConfigInDirectory () { const site = new Site(config) if (program.build) { - const start = process.hrtime() - - await site.build() - - if (config.include) { - config.include.forEach((i) => { - copyDirectory(path.resolve(config.source, i), config.output + i.substr(i.lastIndexOf('/'), i.length)) - }) - } + const _time = await time(async () => { + await site.build() - const end = process.hrtime(start) + if (config.include) { + config.include.forEach((i) => { + copyDirectory(path.resolve(config.source, i), config.output + i.substr(i.lastIndexOf('/'), i.length)) + }) + } + }) // replace the source path with nothing so that we don't get a bunch of duplicate strings process.stdout.write(` - site built at ${config.output} [${ms(((end[0] * 1e9) + end[1]) / 1e6)}] + site built at ${config.output} [${_time}] ${site.rendered.map((top) => '\n' + renderSubDepends(top, 0).replace(new RegExp(config.source + '/', 'g'), '')).join('')} `.trim() + '\n\n') } @@ -151,14 +149,12 @@ async function getConfigInDirectory () { // we don't want to rebuild the output directory because this is expected to change if (file.substring(0, file.lastIndexOf('/')) !== site.output.substring(site.output.lastIndexOf('/') + 1, site.output.length) && file.indexOf('.git') === -1) { - const start = process.hrtime() - - await site.build() - - const end = process.hrtime(start) + const _time = await time(async () => { + await site.build() + }) process.stdout.write(` - site rebuilt in [${ms(((end[0] * 1e9) + end[1]) / 1e6)}] because of ${ev} of ${file} + site rebuilt in [${_time}] because of ${ev} of ${file} ${site.rendered.map((top) => '\n' + renderSubDepends(top, 0).replace(new RegExp(config.source + '/', 'g'), '')).join('')} `.trim() + '\n\n') } diff --git a/lib/defaultPlugins.js b/lib/defaultPlugins.js index c62d77a..42a1fba 100644 --- a/lib/defaultPlugins.js +++ b/lib/defaultPlugins.js @@ -3,7 +3,7 @@ const path = require('path') const { render, parse } = require('./util') module.exports.editable = { - parse: async function (filePath, content) { + async parse (filePath, content) { const reg = /{{-- editable (.+?) (string|number|color) --}}/g if (content.match(reg)) { @@ -45,7 +45,7 @@ module.exports.editable = { } module.exports.includes = { - parse: async function (filePath, content) { + async parse (filePath, content) { const reg = /{{-- includes (.+?) --}}/g if (content.match(reg)) { @@ -65,7 +65,7 @@ module.exports.includes = { } return false }, - render: async function (plugins, filePath, content, templates, data, found) { + async render (plugins, filePath, content, templates, data, found) { const start = process.hrtime() const ext = found.substring(found.lastIndexOf('.') + 1, found.length) const name = found.substring(found.lastIndexOf('/') + 1, found.length - 3) diff --git a/lib/util.js b/lib/util.js index 400a004..15223bd 100644 --- a/lib/util.js +++ b/lib/util.js @@ -31,6 +31,22 @@ async function ensureDirectoryExists (directory) { } } +/** + * executes a function and returns the time in human readable format + * @method time + * @param {Function} func - the function to be executed + * @return {String} - human readable time string + */ +async function time (func) { + const start = process.hrtime() + + await func() + + const end = process.hrtime(start) + + return ms(((end[0] * 1e9) + end[1]) / 1e6) +} + /** * recursively copies the content of one directory to another * @method copyDirectory @@ -455,6 +471,7 @@ module.exports = { parseString, render, ms, + time, getConfig, escapeRegexValues, ensureDirectoryExists, diff --git a/package.json b/package.json index 1b25185..dd7e6f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sweeney", - "version": "1.3.1", + "version": "1.3.2", "description": "💈 A static site generator that cuts the way you want it to", "author": "Gabriel J. Csapo ", "license": "Apache-2.0", diff --git a/test/util.js b/test/util.js index 9b5f306..10c385f 100644 --- a/test/util.js +++ b/test/util.js @@ -12,6 +12,7 @@ const readdir = promisify(fs.readdir) const { ms, + time, merge, set, get, @@ -717,4 +718,28 @@ test('util', (t) => { t.end() }) }) + + t.test('time', (t) => { + t.plan(2) + + t.test('should work with async function', async (t) => { + const _time = await time(async () => { + return new Promise(function (resolve, reject) { + setTimeout(() => { + resolve() + }, 3000) + }) + }) + t.equal(_time, '3s') + t.end() + }) + + t.test('should work with sync function', async (t) => { + const _time = await time(() => { + // doing nothing + }) + t.ok(_time.substring(0, 3), '0.0') + t.end() + }) + }) })