Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: improve hook debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Apr 20, 2018
1 parent a43f853 commit 076b825
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
},
"devDependencies": {
"@oclif/errors": "^1.0.4",
"@oclif/parser": "^3.3.2",
"@oclif/parser": "^3.3.3",
"@oclif/tslint": "^1.1.0",
"@types/chai": "^4.1.2",
"@types/chai": "^4.1.3",
"@types/globby": "^6.1.0",
"@types/indent-string": "^3.0.0",
"@types/lodash": "^4.14.107",
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.5",
"@types/mocha": "^5.2.0",
"@types/node": "^9.6.6",
"@types/wrap-ansi": "^3.0.0",
"chai": "^4.1.2",
"fancy-test": "^1.0.5",
"globby": "^8.0.1",
"lodash": "^4.17.5",
"mocha": "^5.1.0",
"mocha": "^5.1.1",
"ts-node": "^6.0.0",
"tslint": "^5.9.1",
"typescript": "^2.8.1"
"typescript": "^2.8.3"
},
"engines": {
"node": ">=8.0.0"
Expand Down
28 changes: 15 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,31 +256,33 @@ export class Config implements IConfig {

async runHook<T extends Hooks, K extends keyof T>(event: K, opts: T[K]) {
debug('start %s hook', event)
const context: Hook.Context = {
config: this,
debug: require('debug')([this.bin, 'hooks', event].join(':')),
exit(code = 0) { exit(code) },
log(message?: any, ...args: any[]) {
process.stdout.write(format(message, ...args) + '\n')
},
error(message, options: {code?: string, exit?: number} = {}) {
error(message, options)
},
warn(message: string) { warn(message) },
}
const promises = this.plugins.map(p => {
const debug = require('debug')([this.bin, p.name, 'hooks', event].join(':'))
const context: Hook.Context = {
config: this,
debug,
exit(code = 0) { exit(code) },
log(message?: any, ...args: any[]) {
process.stdout.write(format(message, ...args) + '\n')
},
error(message, options: {code?: string, exit?: number} = {}) {
error(message, options)
},
warn(message: string) { warn(message) },
}
return Promise.all((p.hooks[event] || [])
.map(async hook => {
try {
const f = tsPath(p.root, hook)
debug('hook', event, f)
debug('start', f)
const search = (m: any): Hook<K> => {
if (typeof m === 'function') return m
if (m.default && typeof m.default === 'function') return m.default
return Object.values(m).find((m: any) => typeof m === 'function') as Hook<K>
}

await search(require(f)).call(context, {...opts as any, config: this})
debug('done')
} catch (err) {
if (err && err.oclif && err.oclif.exit !== undefined) throw err
this.warn(err, `runHook ${event}`)
Expand Down
44 changes: 22 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
version "1.0.0"
resolved "https://registry.yarnpkg.com/@oclif/linewrap/-/linewrap-1.0.0.tgz#aedcb64b479d4db7be24196384897b5000901d91"

"@oclif/parser@^3.3.2":
version "3.3.2"
resolved "https://registry.yarnpkg.com/@oclif/parser/-/parser-3.3.2.tgz#81af56d4f97beae376abb59e461005489abd9e19"
"@oclif/parser@^3.3.3":
version "3.3.3"
resolved "https://registry.yarnpkg.com/@oclif/parser/-/parser-3.3.3.tgz#bfde499b836178eee2b6b29ccb7fb3f95851d3c6"
dependencies:
"@oclif/linewrap" "^1.0.0"
chalk "^2.3.2"
chalk "^2.4.0"

"@oclif/tslint@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@oclif/tslint/-/tslint-1.1.0.tgz#a2d494a61afa882a685fe5f4d866dafd18990728"
dependencies:
tslint-xo "^0.7.0"

"@types/chai@^4.1.2":
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.2.tgz#f1af664769cfb50af805431c407425ed619daa21"
"@types/chai@^4.1.3":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.3.tgz#b8a74352977a23b604c01aa784f5b793443fb7dc"

"@types/events@*":
version "1.1.0"
Expand Down Expand Up @@ -63,17 +63,17 @@
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"

"@types/mocha@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.0.0.tgz#a3014921991066193f6c8e47290d4d598dfd19e6"
"@types/mocha@^5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.0.tgz#b3c8e69f038835db1a7fdc0b3d879fc50506e29e"

"@types/node@*":
version "9.4.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.5.tgz#d2a90c634208173d1b1a0a6ba9f1df3de62edcf5"

"@types/node@^9.6.5":
version "9.6.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.5.tgz#ee700810fdf49ac1c399fc5980b7559b3e5a381d"
"@types/node@^9.6.6":
version "9.6.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.6.tgz#439b91f9caf3983cad2eef1e11f6bedcbf9431d2"

"@types/wrap-ansi@^3.0.0":
version "3.0.0"
Expand Down Expand Up @@ -253,9 +253,9 @@ chalk@^2.3.0:
escape-string-regexp "^1.0.5"
supports-color "^5.2.0"

chalk@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.2.tgz#250dc96b07491bfd601e648d66ddf5f60c7a5c65"
chalk@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.0.tgz#a060a297a6b57e15b61ca63ce84995daa0fe6e52"
dependencies:
ansi-styles "^3.2.1"
escape-string-regexp "^1.0.5"
Expand Down Expand Up @@ -800,9 +800,9 @@ [email protected], mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"

mocha@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.1.0.tgz#5ff11cc39c0bb65330ac6c41f9086634e3e3f686"
mocha@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.1.1.tgz#b774c75609dac05eb48f4d9ba1d827b97fde8a7b"
dependencies:
browser-stdout "1.3.1"
commander "2.11.0"
Expand Down Expand Up @@ -1179,9 +1179,9 @@ type-detect@^4.0.0:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

typescript@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.1.tgz#6160e4f8f195d5ba81d4876f9c0cc1fbc0820624"
typescript@^2.8.3:
version "2.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170"

union-value@^1.0.0:
version "1.0.0"
Expand Down

0 comments on commit 076b825

Please sign in to comment.