Lessons from open-source: Algorithm behind `“next buuild” does not exist. Did you mean “next build”?`
Tags: javascript, node, opensource, nextjs
Published: 2024-03-20 21:12:16

This lesson is picked from [Next.js source code](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/detect-typo.ts#L2). In this article, you will learn how Next.js detects typo in your CLI commands such as build | dev | info | lint | start | telemetry.

Detect-typo
-----------

Ever wondered what is the logic behind detecing a typo in the CLI command you issue using [Nextjs CLI API](https://nextjs.org/docs/pages/api-reference/next-cli)? well, there is a utility file named [detect-typo.ts](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/detect-typo.ts#L2) in nextjs repository.

[detect-typo.ts](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/detect-typo.ts#L2) has two functions at the time of writing this article. They are:

1.  detectTypo
2.  minDistance

> [_Practice the exercises based on documentation_](https://tthroo.com/) _to become an expert in Next.js_

### detectTypo

![detectTypo](https://media.licdn.com/dms/image/D5612AQG1txCMdUpOyw/article-inline_image-shrink_1500_2232/0/1710968560208?e=1716422400&v=beta&t=WKDLjvvhC72KxAy9v59xC3cC-wPbEvfIYkR8j5rUD1o)

This is used in [Next CLI related files](https://github.com/vercel/next.js/blob/f25ae9a0f97b24cde4c25c571cfc73fec088b45f/packages/next/src/lib/get-project-dir.ts#L3)

![cli-1](https://media.licdn.com/dms/image/D5612AQEzu0NbEMN3jw/article-inline_image-shrink_1000_1488/0/1710968560224?e=1716422400&v=beta&t=K7kIawKvmVZI46BerPQH23GuX_kZBcxErw27DiyqxeM)

### minDistance

This function has the algorithm that detects if there is a typo in the CLI command issued.

```
// 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\]
}
```

Results:
--------

I intentionally added typos to the Next CLI API and the image below shows that

![results](https://media.licdn.com/dms/image/D5612AQEhdIaG18hhcw/article-inline_image-shrink_1500_2232/0/1710968557730?e=1716422400&v=beta&t=ubHsfEg8GLxLAfaUKuOMC176_uukd0iDIDYWgx_aayg)

Conclusion:
-----------

This simple minDistance algorithm to detect a typo in the Next CLI command makes me want to relearn and practice data structures and algorithms more in order to write some quality code. I have taken it for granted far too long now. Clean code isn’t just writing concise fancy function names and parameters, I am realising it also means using the proper data structures and algorithms.