rsnext/packages/next/lib/find-pages-dir.ts
Alexander Sviridov f5cb7bd829
fix(41456): check src/app folder too in getHasAppDir (#41458)
fixes https://github.com/vercel/next.js/issues/41456

When we check if app folder exists, check for src/app path too

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-10-17 15:32:44 -07:00

58 lines
1.3 KiB
TypeScript

import fs from 'fs'
import path from 'path'
export const existsSync = (f: string): boolean => {
try {
fs.accessSync(f, fs.constants.F_OK)
return true
} catch (_) {
return false
}
}
export function findDir(dir: string, name: 'pages' | 'app'): string | null {
// prioritize ./${name} over ./src/${name}
let curDir = path.join(dir, name)
if (existsSync(curDir)) return curDir
curDir = path.join(dir, 'src', name)
if (existsSync(curDir)) return curDir
return null
}
export function findPagesDir(
dir: string,
isAppDirEnabled: boolean
): {
pagesDir: string | undefined
appDir: string | undefined
} {
const pagesDir = findDir(dir, 'pages') || undefined
let appDir: undefined | string
if (isAppDirEnabled) {
appDir = findDir(dir, 'app') || undefined
}
const hasAppDir =
!!appDir && fs.existsSync(appDir) && fs.statSync(appDir).isDirectory()
if (hasAppDir && appDir == null && pagesDir == null) {
throw new Error(
"> Couldn't find any `pages` or `app` directory. Please create one under the project root"
)
}
if (!isAppDirEnabled) {
if (pagesDir == null) {
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
}
}
return {
pagesDir,
appDir,
}
}