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 authored and dephiros committed Sep 1, 2018
1 parent c587586 commit 78b7755
Show file tree
Hide file tree
Showing 6 changed files with 84 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
16 changes: 16 additions & 0 deletions packages/cli/src/api/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fuzzValidateCommand should return command is invalid 1`] = `
lingui: compilesss is unknown
Do you mean: compile ?
`;

exports[`fuzzValidateCommand should return command is invalid if no commands are configured 1`] = `
lingui: compile is unknown
`;

exports[`fuzzValidateCommand should return suggestion for the first command if user passes multiple commands 1`] = `
lingui: add is unknown
Do you mean: add-strings, add-locales ?
`;
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(commands = [], userCommands = []) {
const commandNames = commands.map(command => command.name())
for (let userCommand of userCommands) {
if (!commandNames.includes(userCommand)) {
const commandScores = commandNames
.map(name => ({
name: name,
score: score(name, userCommand.slice(0, name.length))
}))
.filter(nameScore => nameScore.score > 0)
return `lingui: ${userCommand} is unknown
${
commandScores.length
? `Do you mean: ${commandScores
.slice(0, 3)
.map(commandScore => chalk.inverse(commandScore.name))
.join(", ")} ?`
: ""
}`
}
}
return ""
}
34 changes: 34 additions & 0 deletions packages/cli/src/api/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { fuzzValidateCommand } from "./utils"

describe("fuzzValidateCommand", function() {
function runFuzzValidateCommand(commandNames, userCommands) {
return fuzzValidateCommand(
commandNames.map(commandName => ({ name: () => commandName })),
userCommands
)
}

it("should return empty string if user command is valid", function() {
expect(runFuzzValidateCommand(["compile"], ["compile"])).toEqual("")
})

it("should return empty string if user passes no command", function() {
expect(runFuzzValidateCommand(["compile"], [])).toEqual("")
})

it("should return command is invalid if no commands are configured", function() {
expect(runFuzzValidateCommand([], ["compile"])).toMatchSnapshot()
})

it("should return command is invalid", function() {
expect(
runFuzzValidateCommand(["compile"], ["compilesss"])
).toMatchSnapshot()
})

it("should return suggestion for the first command if user passes multiple commands", function() {
expect(
runFuzzValidateCommand(["add-strings", "add-locales"], ["add", "com"])
).toMatchSnapshot()
})
})
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 78b7755

Please sign in to comment.