2019-09-24 17:15:14 +02:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
2022-10-24 22:42:11 +02:00
|
|
|
import * as Log from '../build/output/log'
|
2019-09-24 17:15:14 +02:00
|
|
|
|
2020-03-26 13:32:41 +01:00
|
|
|
export const existsSync = (f: string): boolean => {
|
2019-09-24 17:15:14 +02:00
|
|
|
try {
|
|
|
|
fs.accessSync(f, fs.constants.F_OK)
|
|
|
|
return true
|
|
|
|
} catch (_) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 00:32:44 +02:00
|
|
|
export function findDir(dir: string, name: 'pages' | 'app'): string | null {
|
2022-05-02 15:07:21 +02:00
|
|
|
// prioritize ./${name} over ./src/${name}
|
|
|
|
let curDir = path.join(dir, name)
|
2019-09-24 17:15:14 +02:00
|
|
|
if (existsSync(curDir)) return curDir
|
|
|
|
|
2022-05-02 15:07:21 +02:00
|
|
|
curDir = path.join(dir, 'src', name)
|
2019-09-24 17:15:14 +02:00
|
|
|
if (existsSync(curDir)) return curDir
|
|
|
|
|
2022-05-02 15:07:21 +02:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findPagesDir(
|
|
|
|
dir: string,
|
2022-10-05 00:16:44 +02:00
|
|
|
isAppDirEnabled: boolean
|
|
|
|
): {
|
|
|
|
pagesDir: string | undefined
|
|
|
|
appDir: string | undefined
|
|
|
|
} {
|
2022-09-03 02:13:47 +02:00
|
|
|
const pagesDir = findDir(dir, 'pages') || undefined
|
2022-10-24 22:42:11 +02:00
|
|
|
const appDir = findDir(dir, 'app') || undefined
|
2022-05-02 15:07:21 +02:00
|
|
|
|
2022-10-24 22:42:11 +02:00
|
|
|
if (isAppDirEnabled && appDir == null && pagesDir == null) {
|
2022-10-05 00:16:44 +02:00
|
|
|
throw new Error(
|
|
|
|
"> Couldn't find any `pages` or `app` directory. Please create one under the project root"
|
|
|
|
)
|
2022-05-02 15:07:21 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 00:16:44 +02:00
|
|
|
if (!isAppDirEnabled) {
|
2022-10-24 22:42:11 +02:00
|
|
|
if (appDir != null && pagesDir == null) {
|
|
|
|
throw new Error(
|
|
|
|
'> The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (appDir != null && pagesDir != null) {
|
|
|
|
Log.warn(
|
|
|
|
'The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
|
|
|
|
)
|
|
|
|
}
|
2022-09-03 02:13:47 +02:00
|
|
|
if (pagesDir == null) {
|
|
|
|
throw new Error(
|
|
|
|
"> Couldn't find a `pages` directory. Please create one under the project root"
|
|
|
|
)
|
|
|
|
}
|
2019-09-24 17:15:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 15:07:21 +02:00
|
|
|
return {
|
2022-10-05 00:16:44 +02:00
|
|
|
pagesDir,
|
2022-10-24 22:42:11 +02:00
|
|
|
appDir: isAppDirEnabled ? appDir : undefined,
|
2022-05-02 15:07:21 +02:00
|
|
|
}
|
2019-09-24 17:15:14 +02:00
|
|
|
}
|