-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
get-config-file.ts
134 lines (118 loc) · 3.7 KB
/
get-config-file.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { distance as levenshtein } from "fastest-levenshtein"
import fs from "fs-extra"
import { testRequireError } from "../utils/test-require-error"
import report from "gatsby-cli/lib/reporter"
import path from "path"
import { sync as existsSync } from "fs-exists-cached"
import { COMPILED_CACHE_DIR } from "../utils/parcel/compile-gatsby-files"
export function isNearMatch(
fileName: string | undefined,
configName: string,
distance: number
): boolean {
if (!fileName) return false
return levenshtein(fileName, configName) <= distance
}
export async function getConfigFile(
siteDirectory: string,
configName: string,
distance: number = 3
): Promise<{
configModule: any
configFilePath: string
}> {
let configPath = ``
let configFilePath = ``
let configModule: any
// Attempt to find compiled gatsby-config.js in .cache/compiled/gatsby-config.js
try {
configPath = path.join(`${siteDirectory}/${COMPILED_CACHE_DIR}`, configName)
configFilePath = require.resolve(configPath)
configModule = require(configFilePath)
} catch (outerError) {
// Not all plugins will have a compiled file, so the err.message can look like this:
// "Cannot find module '<root>/node_modules/gatsby-source-filesystem/.cache/compiled/gatsby-config'"
// But the compiled file can also have an error like this:
// "Cannot find module 'foobar'"
// So this is trying to differentiate between an error we're fine ignoring and an error that we should throw
const isModuleNotFoundError = outerError.code === `MODULE_NOT_FOUND`
const isThisFileRequireError =
outerError?.requireStack?.[0]?.includes(`get-config-file`) ?? true
// User's module require error inside gatsby-config.js
if (!(isModuleNotFoundError && isThisFileRequireError)) {
report.panic({
id: `11902`,
error: outerError,
context: {
configName,
message: outerError.message,
},
})
}
// Attempt to find uncompiled gatsby-config.js in root dir
configPath = path.join(siteDirectory, configName)
try {
configFilePath = require.resolve(configPath)
configModule = require(configFilePath)
} catch (innerError) {
// Some other error that is not a require error
if (!testRequireError(configPath, innerError)) {
report.panic({
id: `10123`,
error: innerError,
context: {
configName,
message: innerError.message,
},
})
}
const files = await fs.readdir(siteDirectory)
let tsConfig = false
let nearMatch = ``
for (const file of files) {
if (tsConfig || nearMatch) {
break
}
const { name, ext } = path.parse(file)
if (name === configName && ext === `.ts`) {
tsConfig = true
break
}
if (isNearMatch(name, configName, distance)) {
nearMatch = file
}
}
// gatsby-config.ts exists but compiled gatsby-config.js does not
if (tsConfig) {
report.panic({
id: `10127`,
error: innerError,
context: {
configName,
},
})
}
// gatsby-config is misnamed
if (nearMatch) {
report.panic({
id: `10124`,
error: innerError,
context: {
configName,
nearMatch,
},
})
}
// gatsby-config.js is incorrectly located in src/gatsby-config.js
if (existsSync(path.join(siteDirectory, `src`, configName + `.js`))) {
report.panic({
id: `10125`,
context: {
configName,
},
})
}
}
}
return { configModule, configFilePath }
}