forked from vercel/next.js
/
find-pages-dir.ts
46 lines (38 loc) · 982 Bytes
/
find-pages-dir.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
}
}
function findDir(dir: string, name: 'pages' | 'root'): 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,
root?: boolean
): { pages: string; root?: string } {
const pagesDir = findDir(dir, 'pages')
let rootDir: undefined | string
if (root) {
rootDir = findDir(dir, 'root') || undefined
}
// TODO: allow "root" dir without pages dir
if (pagesDir === null) {
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
}
return {
pages: pagesDir,
root: rootDir,
}
}