Skip to content

Commit

Permalink
Add CLI command typo detection (#34836)
Browse files Browse the repository at this point in the history
* Add CLI command typo detection

* Apply suggestions from code review

Co-authored-by: Steven <steven@ceriously.com>

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
ijjk and styfle committed Feb 26, 2022
1 parent 0aba307 commit ed43c03
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/bin/next.ts
Expand Up @@ -15,7 +15,7 @@ import { NON_STANDARD_NODE_ENV } from '../lib/constants'

const defaultCommand = 'dev'
export type cliCommand = (argv?: string[]) => void
const commands: { [command: string]: () => Promise<cliCommand> } = {
export const commands: { [command: string]: () => Promise<cliCommand> } = {
build: () => Promise.resolve(require('../cli/next-build').nextBuild),
start: () => Promise.resolve(require('../cli/next-start').nextStart),
export: () => Promise.resolve(require('../cli/next-export').nextExport),
Expand Down
44 changes: 44 additions & 0 deletions packages/next/lib/detect-typo.ts
@@ -0,0 +1,44 @@
// the minimum number of operations required to convert string a to string b.
function minDistance(a: string, b: string, threshold: number): number {
const m = a.length
const n = b.length

if (m < n) {
return minDistance(b, a, threshold)
}

if (n === 0) {
return m
}

let previousRow = Array.from({ length: n + 1 }, (_, i) => i)

for (let i = 0; i < m; i++) {
const s1 = a[i]
let currentRow = [i + 1]
for (let j = 0; j < n; j++) {
const s2 = b[j]
const insertions = previousRow[j + 1] + 1
const deletions = currentRow[j] + 1
const substitutions = previousRow[j] + Number(s1 !== s2)
currentRow.push(Math.min(insertions, deletions, substitutions))
}
previousRow = currentRow
}
return previousRow[previousRow.length - 1]
}

export function detectTypo(input: string, options: string[], threshold = 2) {
const potentialTypos = options
.map((o) => ({
option: o,
distance: minDistance(o, input, threshold),
}))
.filter(({ distance }) => distance <= threshold && distance > 0)
.sort((a, b) => a.distance - b.distance)

if (potentialTypos.length) {
return potentialTypos[0].option
}
return null
}
13 changes: 13 additions & 0 deletions packages/next/lib/get-project-dir.ts
@@ -1,6 +1,8 @@
import fs from 'fs'
import path from 'path'
import { commands } from '../bin/next'
import * as Log from '../build/output/log'
import { detectTypo } from './detect-typo'

export function getProjectDir(dir?: string) {
try {
Expand All @@ -19,6 +21,17 @@ export function getProjectDir(dir?: string) {
return realDir
} catch (err: any) {
if (err.code === 'ENOENT') {
if (typeof dir === 'string') {
const detectedTypo = detectTypo(dir, Object.keys(commands))

if (detectedTypo) {
Log.error(
`"next ${dir}" does not exist. Did you mean "next ${detectedTypo}"?`
)
process.exit(1)
}
}

Log.error(
`Invalid project directory provided, no such directory: ${path.resolve(
dir || '.'
Expand Down
20 changes: 20 additions & 0 deletions test/integration/cli/test/index.test.js
Expand Up @@ -57,6 +57,26 @@ describe('CLI Usage', () => {
'Invalid project directory provided, no such directory'
)
})

test('detects command typos', async () => {
const typos = [
['buidl', 'build'],
['buill', 'build'],
['biild', 'build'],
['exporr', 'export'],
['starr', 'start'],
['dee', 'dev'],
]

for (const check of typos) {
const output = await runNextCommand([check[0]], {
stderr: true,
})
expect(output.stderr).toContain(
`"next ${check[0]}" does not exist. Did you mean "next ${check[1]}"?`
)
}
})
})

describe('build', () => {
Expand Down

0 comments on commit ed43c03

Please sign in to comment.