diff --git a/package.json b/package.json index 6a48fe2..b53eb01 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "prepublishOnly": "npm run build", "lint": "eslint . --ext=.ts", "sync-labels": "github-label-sync --labels .github/labels.json poppinss/hooks", - "quick:test": "node --loader=ts-node/esm bin/test.ts" + "quick:test": "cross-env NODE_DEBUG=poppinss:hooks node --loader=ts-node/esm bin/test.ts" }, "keywords": [ "hooks", @@ -50,6 +50,7 @@ "@swc/core": "^1.3.100", "@types/node": "^20.10.4", "c8": "^8.0.1", + "cross-env": "^7.0.3", "del-cli": "^5.1.0", "eslint": "^8.55.0", "github-label-sync": "^2.3.1", diff --git a/src/runner.ts b/src/runner.ts index 8e6e50d..c626b32 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -7,8 +7,11 @@ * file that was distributed with this source code. */ +import { debuglog } from 'node:util' import { HookHandler, CleanupHandler, HookHandlerProvider } from './types.js' +const debug = debuglog('poppinss:hooks') + /** * Runner allows running a set of specific hook handlers for a given * event. You can grab the instance of the runner using the "hook.runner" method. @@ -79,9 +82,11 @@ export class Runner { */ without(handlersToIgnore?: string[]): this { if (!handlersToIgnore) { + debug('skipping all hooks') this.#skipAllHooks = true } else { this.#skipAllHooks = false + debug('skipping %O hooks', handlersToIgnore) this.#handlersToIgnore = handlersToIgnore } @@ -101,14 +106,23 @@ export class Runner { return } + debug('running hooks') + const handlers = reverse ? Array.from(this.#hookHandlers).reverse() : this.#hookHandlers for (let handler of handlers) { if (this.#filter(handler.name)) { + if (handler.name) { + debug('running hook %s', handler.name) + } + const result = await (typeof handler === 'function' ? handler(...data) : handler.handle(this.action, ...data)) if (typeof result === 'function') { + if (handler.name) { + debug('cleanup scheduled by %s hook', handler.name) + } this.#cleanupHandlers.push(result) } } @@ -138,6 +152,7 @@ export class Runner { } this.#state = 'cleanup_initiated' + debug('performing cleanup') let startIndex = this.#cleanupHandlers.length while (startIndex--) {