rsnext/packages/next/lib/find-pages-dir.ts
Balázs Orbán aa51c26c2e
docs: add error link when missing appDir: true (#43293)
[Slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1669206360940399)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
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`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && 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: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
2022-11-23 12:07:14 -08:00

63 lines
1.8 KiB
TypeScript

import fs from 'fs'
import path from 'path'
import * as Log from '../build/output/log'
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
const appDir = findDir(dir, 'app') || undefined
if (isAppDirEnabled && 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 (appDir != null && pagesDir == null) {
throw new Error(
'> The `app` directory is experimental. To enable, add `appDir: true` to your `next.config.js` configuration under `experimental`. See https://nextjs.org/docs/messages/experimental-app-dir-config'
)
}
if (appDir != null && pagesDir != null) {
Log.warn(
'The `app` directory is experimental. To enable, add `appDir: true` to your `next.config.js` configuration under `experimental`. See https://nextjs.org/docs/messages/experimental-app-dir-config'
)
}
if (pagesDir == null) {
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
}
}
return {
pagesDir,
appDir: isAppDirEnabled ? appDir : undefined,
}
}