diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.js b/index.js new file mode 100644 index 0000000..e56e014 --- /dev/null +++ b/index.js @@ -0,0 +1,33 @@ +'use strict'; + +const isPromise = require('p-is-promise'); + +module.exports = (hermione, opts) => { + if (opts.enabled === false) { + return; + } + + const baseOnFn = hermione.on.bind(hermione); + + hermione.on = function(event, cb) { + const pluginName = 'hermione-plguins-profiler:' + parsePluginName() + `:event:${event}`; + + const fn = (...args) => { + console.time(pluginName); + + const res = cb(...args); + + if (isPromise(res)) { + return res.finally(() => console.timeEnd(pluginName)) + } else { + console.timeEnd(pluginName); + } + } + + return baseOnFn(event, fn); + }.bind(hermione); +}; + +function parsePluginName() { + return (new Error).stack.split('\n')[3].match(/\/node_modules\/(.+)\//)[1]; +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..54dff8d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "hermione-plugins-profiler", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..5c3182f --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "hermione-plugins-profiler", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/eGavr/hermione-plugins-profiler.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/eGavr/hermione-plugins-profiler/issues" + }, + "homepage": "https://github.com/eGavr/hermione-plugins-profiler#readme", + "dependencies": { + "p-is-promise": "^2.1.0" + } +}