rsnext/packages/next/lib/get-project-dir.ts
JJ Kasper ed43c03ebb
Add CLI command typo detection (#34836)
* 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>
2022-02-26 07:05:26 -08:00

44 lines
1.2 KiB
TypeScript

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 {
const resolvedDir = path.resolve(dir || '.')
const realDir = fs.realpathSync.native(resolvedDir)
if (
resolvedDir !== realDir &&
resolvedDir.toLowerCase() === realDir.toLowerCase()
) {
Log.warn(
`Invalid casing detected for project dir, received ${resolvedDir} actual path ${realDir}, see more info here https://nextjs.org/docs/messages/invalid-project-dir-casing`
)
}
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 || '.'
)}`
)
process.exit(1)
}
throw err
}
}