Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI command typo detection #34836

Merged
merged 5 commits into from Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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