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

Commit

Permalink
fix: make hook typing less strict (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Oct 3, 2018
1 parent e1e8529 commit b148583
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
"tslib": "^1.9.3"
},
"devDependencies": {
"@oclif/errors": "^1.2.0",
"@oclif/errors": "^1.2.1",
"@oclif/parser": "^3.6.1",
"@oclif/tslint": "^3.1.0",
"@types/chai": "^4.1.4",
"@types/chai": "^4.1.6",
"@types/globby": "^8.0.0",
"@types/indent-string": "^3.0.0",
"@types/lodash": "^4.14.116",
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"@types/node": "^10.11.4",
"@types/wrap-ansi": "^3.0.0",
"chai": "^4.1.2",
"chai": "^4.2.0",
"fancy-test": "^1.4.1",
"globby": "^8.0.1",
"lodash": "^4.17.11",
"mocha": "^5.2.0",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "^3.0.3"
"typescript": "^3.1.1"
},
"engines": {
"node": ">=8.0.0"
Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class Config implements IConfig {
}
}

async runHook<T extends Hooks, K extends Extract<keyof T, string>>(event: K, opts: T[K]) {
async runHook<T>(event: string, opts: T) {
debug('start %s hook', event)
const promises = this.plugins.map(p => {
const debug = require('debug')([this.bin, p.name, 'hooks', event].join(':'))
Expand All @@ -280,10 +280,10 @@ export class Config implements IConfig {
try {
const f = tsPath(p.root, hook)
debug('start', f)
const search = (m: any): Hook<K> => {
const search = (m: any): Hook<T> => {
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>
return Object.values(m).find((m: any) => typeof m === 'function') as Hook<T>
}

await search(require(f)).call(context, {...opts as any, config: this})
Expand Down
20 changes: 19 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,27 @@ export interface Hooks {
}
}

export type Hook<K extends keyof Hooks> = (this: Hook.Context, options: Hooks[K] & {config: Config.IConfig}) => any
export type HookKeyOrOptions<K> = K extends (keyof Hooks) ? Hooks[K] : K
export type Hook<T> = (this: Hook.Context, options: HookKeyOrOptions<T> & {config: Config.IConfig}) => any

export namespace Hook {
export type Init = Hook<{
id: string | undefined,
argv: string[],
}>
export type PluginsPreinstall = Hook<{
name: string,
tag: string,
type: 'npm'
}>
export type Prerun = Hook<{
Command: Config.Command.Class
argv: string[]
}>
export type CommandNotFound = Hook<{
id: string
}>

export interface Context {
config: Config.IConfig
exit(code?: number): void
Expand Down
67 changes: 39 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.2.tgz#54c5a964462be3d4d78af631363c18d6fa91ac26"
integrity sha512-yprFYuno9FtNsSHVlSWd+nRlmGoAbqbeCwOryP6sC/zoCjhpArcRMYp19EvpSUSizJAlsXEwJv+wcWS9XaXdMw==

"@oclif/errors@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.2.0.tgz#4166952699724c25af2ab4528fe223e930655e72"
integrity sha512-DIcWydwfESHVMwrZt3lj5qLAiX296vdfA6K7utCa2/6nmT1JgBc102iFcjpmNxUzDKBU67NKCVBPSMCBDD/1wg==
"@oclif/errors@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.2.1.tgz#3d655bec6ce7eb7df2f3cd39c23eb62f0470b77d"
integrity sha512-LOZBEraXrMuRPnUYrEFrmJ7m8to7pZ6E0W9rWvjr8PPlxc2PsqGQCeychbqeEWc6pHjsy7GF9M+Pjw16J2oC5A==
dependencies:
clean-stack "^1.3.0"
fs-extra "^7.0.0"
indent-string "^3.2.0"
strip-ansi "^4.0.0"
wrap-ansi "^3.0.1"
wrap-ansi "^4.0.0"

"@oclif/linewrap@^1.0.0":
version "1.0.0"
Expand Down Expand Up @@ -72,6 +72,11 @@
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.4.tgz#5ca073b330d90b4066d6ce18f60d57f2084ce8ca"
integrity sha512-h6+VEw2Vr3ORiFCyyJmcho2zALnUq9cvdB/IO8Xs9itrJVCenC7o26A6+m7D0ihTTr65eS259H5/Ghl/VjYs6g==

"@types/chai@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.6.tgz#1eb26c040e3a84205b1008ad55c800e5e8a94e34"
integrity sha512-CBk7KTZt3FhPsEkYioG6kuCIpWISw+YI8o+3op4+NXwTpvAPxE1ES8+PY8zfaK2L98b1z5oq03UHa4VYpeUxnw==

"@types/events@*":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"
Expand Down Expand Up @@ -126,6 +131,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.9.4.tgz#0f4cb2dc7c1de6096055357f70179043c33e9897"
integrity sha512-fCHV45gS+m3hH17zgkgADUSi2RR1Vht6wOZ0jyHP8rjiQra9f+mIcgwPQHllmDocYOstIEbKlxbFDYlgrTPYqw==

"@types/node@^10.11.4":
version "10.11.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.11.4.tgz#e8bd933c3f78795d580ae41d86590bfc1f4f389d"
integrity sha512-ojnbBiKkZFYRfQpmtnnWTMw+rzGp/JiystjluW9jgN3VzRwilXddJ6aGQ9V/7iuDG06SBgn7ozW9k3zcAnYjYQ==

"@types/sinon@^5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-5.0.2.tgz#7a9d64df40b26dd48d673f745addc2fdb2bda193"
Expand All @@ -151,7 +161,7 @@ ansi-styles@^2.2.1:
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"
integrity sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=

ansi-styles@^3.2.1:
ansi-styles@^3.2.0, ansi-styles@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==
Expand Down Expand Up @@ -202,7 +212,7 @@ arrify@^1.0.0, arrify@^1.0.1:
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
integrity sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=

assertion-error@^1.0.1:
assertion-error@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==
Expand Down Expand Up @@ -303,17 +313,17 @@ call-me-maybe@^1.0.1:
resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b"
integrity sha1-JtII6onje1y95gJQoV8DHBak1ms=

chai@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
integrity sha1-D2RYS6ZC8PKs4oBiefTwbKI61zw=
chai@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"
integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==
dependencies:
assertion-error "^1.0.1"
check-error "^1.0.1"
deep-eql "^3.0.0"
assertion-error "^1.1.0"
check-error "^1.0.2"
deep-eql "^3.0.1"
get-func-name "^2.0.0"
pathval "^1.0.0"
type-detect "^4.0.0"
pathval "^1.1.0"
type-detect "^4.0.5"

chalk@^1.1.3:
version "1.1.3"
Expand All @@ -335,7 +345,7 @@ chalk@^2.3.0, chalk@^2.4.1:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

check-error@^1.0.1:
check-error@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=
Expand Down Expand Up @@ -433,7 +443,7 @@ decode-uri-component@^0.2.0:
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=

deep-eql@^3.0.0:
deep-eql@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
integrity sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==
Expand Down Expand Up @@ -1115,7 +1125,7 @@ path-type@^3.0.0:
dependencies:
pify "^3.0.0"

pathval@^1.0.0:
pathval@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
integrity sha1-uULm1L3mUwBe9rcTYd74cn0GReA=
Expand Down Expand Up @@ -1462,15 +1472,15 @@ tsutils@^3.0.0:
dependencies:
tslib "^1.8.1"

type-detect@^4.0.0:
type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==

typescript@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.0.3.tgz#4853b3e275ecdaa27f78fda46dc273a7eb7fc1c8"
integrity sha512-kk80vLW9iGtjMnIv11qyxLqZm20UklzuR2tL0QAnDIygIUIemcZMxlMWudl9OOt76H3ntVzcTiddQ1/pAAJMYg==
typescript@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.1.tgz#3362ba9dd1e482ebb2355b02dfe8bcd19a2c7c96"
integrity sha512-Veu0w4dTc/9wlWNf2jeRInNodKlcdLgemvPsrNpfu5Pq39sgfFjvIIgTsvUHCoLBnMhPoUA+tFxsXjU6VexVRQ==

union-value@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -1505,11 +1515,12 @@ use@^3.1.0:
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==

wrap-ansi@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-3.0.1.tgz#288a04d87eda5c286e060dfe8f135ce8d007f8ba"
integrity sha1-KIoE2H7aXChuBg3+jxNc6NAH+Lo=
wrap-ansi@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-4.0.0.tgz#b3570d7c70156159a2d42be5cc942e957f7b1131"
integrity sha512-uMTsj9rDb0/7kk1PbcbCcwvHUxp60fGDB/NNXpVa0Q+ic/e7y5+BwTxKfQ33VYgDppSwi/FBzpetYzo8s6tfbg==
dependencies:
ansi-styles "^3.2.0"
string-width "^2.1.1"
strip-ansi "^4.0.0"

Expand Down

0 comments on commit b148583

Please sign in to comment.