Skip to content

Commit

Permalink
When user pass an unknown command, try to suggest commands that are c…
Browse files Browse the repository at this point in the history
…lose
  • Loading branch information
An Nguyen committed Aug 31, 2018
1 parent c587586 commit d524c33
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"chalk": "^2.3.0",
"cli-table": "^0.3.1",
"commander": "^2.17.1",
"fuzzaldrin": "^2.1.0",
"gettext-parser": "^2.0.0",
"glob": "^7.1.2",
"inquirer": "^6.2.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/src/api/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs"
import path from "path"
import chalk from "chalk"
import { score } from "fuzzaldrin"

export function removeDirectory(dir, keep = false) {
if (!fs.existsSync(dir)) return
Expand Down Expand Up @@ -32,3 +33,27 @@ export function prettyOrigin(origins) {
return ""
}
}

export function fuzzValidateCommand(candidates = [], queries = []) {
const candidateNames = candidates.map(candidate => candidate.name())
for (let query of queries) {
if (!candidateNames.includes(query)) {
const commandScores = candidateNames
.map(name => ({
name: name,
score: score(name, query.slice(0, name.length))
}))
.filter(nameScore => nameScore.score > 0)
return `lingui: ${query} is unknown
${
commandScores.length
? `Do you mean: ${commandScores
.slice(0, 3)
.map(commandScore => chalk.inverse(commandScore.name))
.join(", ")} ?`
: ""
}`
}
}
return ""
}
4 changes: 4 additions & 0 deletions packages/cli/src/lingui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

import { fuzzValidateCommand } from "./api/utils"

const program = require("commander")

let version
Expand All @@ -19,3 +21,5 @@ program
.command("extract [files...]", "Extracts messages from source files")
.command("compile", "Compile message catalogs")
.parse(process.argv)

console.log(fuzzValidateCommand(program.commands, program.args))
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,10 @@ functional-red-black-tree@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"

fuzzaldrin@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fuzzaldrin/-/fuzzaldrin-2.1.0.tgz#90204c3e2fdaa6941bb28d16645d418063a90e9b"

gauge@~2.7.3:
version "2.7.4"
resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"
Expand Down

0 comments on commit d524c33

Please sign in to comment.