Skip to content

Commit b406cc0

Browse files
committed
feat: implemented not found hook
1 parent 0146fd0 commit b406cc0

File tree

9 files changed

+126
-35
lines changed

9 files changed

+126
-35
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/lib
2+
/test/fixtures

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
"bugs": "https://github.com/jdxcode/not-found/issues",
77
"dependencies": {
88
"@dxcli/command": "^0.1.13",
9-
"cli-ux": "^3.1.3"
9+
"@dxcli/config": "^0.1.21",
10+
"@heroku-cli/color": "^1.1.1",
11+
"cli-ux": "^3.1.3",
12+
"string-similarity": "^1.2.0"
1013
},
1114
"devDependencies": {
1215
"@dxcli/dev-semantic-release": "^0.0.3",
13-
"@dxcli/dev-test": "^0.5.2",
16+
"@dxcli/dev-test": "^0.7.0",
1417
"@dxcli/dev-tslint": "^0.0.15",
18+
"@dxcli/version": "^0.1.4",
1519
"@types/node": "^9.3.0",
1620
"@types/read-pkg": "^3.0.0",
21+
"@types/supports-color": "^3.1.0",
1722
"eslint": "^4.16.0",
1823
"eslint-config-dxcli": "^1.1.4",
1924
"husky": "^0.14.3",
@@ -25,7 +30,9 @@
2530
"typescript": "^2.6.2"
2631
},
2732
"dxcli": {
28-
"commands": "./lib/commands"
33+
"hooks": {
34+
"command_not_found": "./lib/hooks/not_found"
35+
}
2936
},
3037
"engines": {
3138
"node": ">=8.0.0"

src/commands/hello.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/hooks/not_found.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Hooks, IHook} from '@dxcli/config'
2+
import {color} from '@heroku-cli/color'
3+
import cli from 'cli-ux'
4+
5+
const hook: IHook<Hooks['command_not_found']> = async opts => {
6+
function closest(cmd: string) {
7+
const DCE = require('string-similarity')
8+
return DCE.findBestMatch(cmd, opts.config.engine.commandIDs).bestMatch.target
9+
}
10+
11+
let binHelp = `${opts.config.bin} help`
12+
let idSplit = opts.id.split(':')
13+
if (await opts.config.engine.findTopic(idSplit[0])) {
14+
// if valid topic, update binHelp with topic
15+
binHelp = `${binHelp} ${idSplit[0]}`
16+
// if topic:COMMAND present, try closest for id
17+
// if (idSplit[1]) closest = this.closest(id)
18+
}
19+
20+
let perhaps = closest ? `Perhaps you meant ${color.yellow(closest(opts.id))}\n` : ''
21+
cli.error(
22+
`${color.yellow(opts.id)} is not a ${opts.config.bin} command.
23+
${perhaps}Run ${color.cmd(binHelp)} for a list of available commands.`,
24+
{exit: 127},
25+
)
26+
}
27+
28+
export default hook

test/commands/hello.test.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/fixtures/test/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@dxcli/test-not-found",
3+
"version": "0.0.0",
4+
"dxcli": {
5+
"bin": "mycli",
6+
"plugins": ["../.."],
7+
"commands": "./src/commands"
8+
},
9+
"engines": {
10+
"node": ">=8.0.0"
11+
},
12+
"files": [
13+
"/src"
14+
]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const {Command, flags} = require('@dxcli/command')
2+
const {cli} = require('cli-ux')
3+
4+
class CLI extends Command {
5+
async run() {
6+
const name = this.flags.name || 'world'
7+
cli.log(`hello ${name}!`)
8+
}
9+
}
10+
11+
CLI.flags = {
12+
name: flags.string({char: 'n', description: 'name to print'}),
13+
}
14+
15+
module.exports = CLI

test/hooks/not_found.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {describe, expect, testHook} from '@dxcli/dev-test'
2+
import color from '@heroku-cli/color'
3+
import * as path from 'path'
4+
5+
color.enabled = false
6+
7+
const root = path.join(__dirname, '../fixtures/test')
8+
9+
describe('command', () => {
10+
testHook('command_not_found', {id: 'hel'}, {root, stderr: true, exit: 127}, ({error}) => {
11+
expect(error!.message).to.equal(`hel is not a mycli command.
12+
Perhaps you meant hello
13+
Run mycli help for a list of available commands.`)
14+
})
15+
})

yarn.lock

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
dependencies:
103103
find-up "^2.1.0"
104104

105-
"@dxcli/command@^0.1.13":
105+
"@dxcli/command@^0.1.11", "@dxcli/command@^0.1.13":
106106
version "0.1.13"
107107
resolved "https://registry.yarnpkg.com/@dxcli/command/-/command-0.1.13.tgz#b4dec2e629076c2b50f4cf43c9324dd6ccb98382"
108108
dependencies:
@@ -122,6 +122,16 @@
122122
lodash "^4.17.4"
123123
read-pkg "^3.0.0"
124124

125+
"@dxcli/config@^0.1.21":
126+
version "0.1.21"
127+
resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.21.tgz#d3cf65e672c5d0e36ba64982199021ede4b7a11a"
128+
dependencies:
129+
debug "^3.1.0"
130+
fs-extra "^5.0.0"
131+
load-json-file "^4.0.0"
132+
lodash "^4.17.4"
133+
read-pkg "^3.0.0"
134+
125135
"@dxcli/dev-commitmsg@^0.0.3":
126136
version "0.0.3"
127137
resolved "https://registry.yarnpkg.com/@dxcli/dev-commitmsg/-/dev-commitmsg-0.0.3.tgz#8941971c9778b675dd14c407c1123b8bef397757"
@@ -146,9 +156,9 @@
146156
"@semantic-release/npm" "^2.6.1"
147157
semantic-release "^12.2.0"
148158

149-
"@dxcli/dev-test@^0.5.2":
150-
version "0.5.2"
151-
resolved "https://registry.yarnpkg.com/@dxcli/dev-test/-/dev-test-0.5.2.tgz#102e1f9a8922264a5050f6c16a8060ea26493152"
159+
"@dxcli/dev-test@^0.7.0":
160+
version "0.7.0"
161+
resolved "https://registry.yarnpkg.com/@dxcli/dev-test/-/dev-test-0.7.0.tgz#bdbed14ed36cf94d3a5956643535b607611333f9"
152162
dependencies:
153163
"@dxcli/dev-nyc-config" "^0.0.3"
154164
"@dxcli/engine" "^0.1.5"
@@ -221,6 +231,23 @@
221231
version "0.0.1"
222232
resolved "https://registry.yarnpkg.com/@dxcli/screen/-/screen-0.0.1.tgz#9af4e8d0e5a9475e9e4b5f2da775b0447ff72fc2"
223233

234+
"@dxcli/version@^0.1.4":
235+
version "0.1.4"
236+
resolved "https://registry.yarnpkg.com/@dxcli/version/-/version-0.1.4.tgz#6f4da6fb9e8dbb58afcc3cb73974ba32abd573d6"
237+
dependencies:
238+
"@dxcli/command" "^0.1.11"
239+
"@dxcli/config" "^0.1.20"
240+
cli-ux "^3.1.3"
241+
242+
"@heroku-cli/color@^1.1.1":
243+
version "1.1.1"
244+
resolved "https://registry.yarnpkg.com/@heroku-cli/color/-/color-1.1.1.tgz#a2c25239ff1196733a79cabc7a750cd46b96dc30"
245+
dependencies:
246+
ansi-styles "^3.2.0"
247+
chalk "^2.3.0"
248+
strip-ansi "^4.0.0"
249+
supports-color "^5.1.0"
250+
224251
"@heroku/linewrap@^1.0.0":
225252
version "1.0.0"
226253
resolved "https://registry.yarnpkg.com/@heroku/linewrap/-/linewrap-1.0.0.tgz#a9d4e99f0a3e423a899b775f5f3d6747a1ff15c6"
@@ -377,6 +404,10 @@
377404
version "0.0.30"
378405
resolved "https://registry.yarnpkg.com/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz#9aa30c04db212a9a0649d6ae6fd50accc40748a1"
379406

407+
"@types/supports-color@^3.1.0":
408+
version "3.1.0"
409+
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-3.1.0.tgz#3584b6b54f45333e988da2c29e6797eff5a20f8c"
410+
380411
JSONStream@^1.0.4:
381412
version "1.3.2"
382413
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.2.tgz#c102371b6ec3a7cf3b847ca00c20bb0fce4c6dea"
@@ -2837,7 +2868,7 @@ lodash@4.17.2:
28372868
version "4.17.2"
28382869
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
28392870

2840-
lodash@^4.0.0, lodash@^4.11.1, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.5.1:
2871+
lodash@^4.0.0, lodash@^4.11.1, lodash@^4.13.1, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.5.1:
28412872
version "4.17.4"
28422873
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
28432874

@@ -4229,6 +4260,12 @@ strict-uri-encode@^1.0.0:
42294260
version "1.1.0"
42304261
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
42314262

4263+
string-similarity@^1.2.0:
4264+
version "1.2.0"
4265+
resolved "https://registry.yarnpkg.com/string-similarity/-/string-similarity-1.2.0.tgz#d75153cb383846318b7a39a8d9292bb4db4e9c30"
4266+
dependencies:
4267+
lodash "^4.13.1"
4268+
42324269
string-width@^1.0.1, string-width@^1.0.2:
42334270
version "1.0.2"
42344271
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"

0 commit comments

Comments
 (0)