Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active September 16, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/00fa33f93f309750bfbc423694829af7 to your computer and use it in GitHub Desktop.
Save isaacs/00fa33f93f309750bfbc423694829af7 to your computer and use it in GitHub Desktop.
diff --git a/lib/npm.js b/lib/npm.js
index 79d276722..7fb234cc9 100644
--- a/lib/npm.js
+++ b/lib/npm.js
@@ -3,6 +3,30 @@
// we define and instantiate the singleton ahead of loading any modules
// required for its methods.
+/* istanbul ignore next */ {
+ const timers = {}
+ process.on('time', name => {
+ if (timers[name]) {
+ throw new Error('conflicting timer! ' + name)
+ }
+ timers[name] = process.hrtime()
+ })
+ process.on('timeEnd', name => {
+ if (!timers[name]) {
+ throw new Error('timer not started! ' + name)
+ }
+ delete timers[name]
+ })
+ process.on('exit', () => {
+ for (const name of Object.keys(timers)) {
+ if (name !== 'npm') {
+ console.error('Dangling timer: ', name)
+ process.exitCode = 1
+ }
+ }
+ })
+}
+
// these are all dependencies used in the ctor
const EventEmitter = require('events')
const { resolve, dirname } = require('path')
@@ -108,6 +132,7 @@ const npm = module.exports = new class extends EventEmitter {
if (this.config.get('usage')) {
console.log(impl.usage)
cb()
+ process.emit('timeEnd', `command:${cmd}`)
} else {
impl(args, er => {
process.emit('timeEnd', `command:${cmd}`)
diff --git a/test/lib/npm.js b/test/lib/npm.js
index 296817849..754f974fe 100644
--- a/test/lib/npm.js
+++ b/test/lib/npm.js
@@ -288,6 +288,11 @@ t.test('npm.load', t => {
t.equal(process.execPath, resolve(dir, node))
})
+ // wait for async stuff to complete since the cb/promise interaction
+ // is a bit weird. can remove these awaits when we promisify the
+ // npm object's commands fully.
+ await new Promise(res => setTimeout(res))
+
await npm.commands.ll([], (er) => {
if (er) {
throw er
@@ -297,8 +302,12 @@ t.test('npm.load', t => {
npm.config.set('usage', false)
t.equal(npm.commands.ll, npm.commands.la, 'same command, different name')
logs.length = 0
+ consoleLogs.length = 0
})
+ await new Promise(res => setTimeout(res))
+ logs.length = 0
+
await npm.commands.get(['scope', '\u2010not-a-dash'], (er) => {
if (er) {
throw er
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment