1 |
|
2 | 'use strict'
|
3 |
|
4 | require('./utils/polyfills')
|
5 |
|
6 | const { getApiClient } = require('./api/client')
|
7 | const { getSiteInfo } = require('./api/site_info')
|
8 | const { getInitialBase, getBase, addBase } = require('./base')
|
9 | const { getBuildDir } = require('./build_dir')
|
10 | const { getCachedConfig } = require('./cached_config')
|
11 | const { normalizeContextProps, mergeContext } = require('./context')
|
12 | const { parseDefaultConfig } = require('./default')
|
13 | const { getEnv } = require('./env/main')
|
14 | const { EVENTS } = require('./events')
|
15 | const { resolveConfigPaths } = require('./files')
|
16 | const { getHeadersPath, addHeaders } = require('./headers')
|
17 | const { getInlineConfig } = require('./inline_config')
|
18 | const { cleanupConfig } = require('./log/cleanup')
|
19 | const { logResult } = require('./log/main')
|
20 | const { mergeConfigs } = require('./merge')
|
21 | const { normalizeBeforeConfigMerge, normalizeAfterConfigMerge } = require('./merge_normalize')
|
22 | const { updateConfig, restoreConfig } = require('./mutations/update')
|
23 | const { addDefaultOpts, normalizeOpts } = require('./options/main')
|
24 | const { UI_ORIGIN, CONFIG_ORIGIN, INLINE_ORIGIN } = require('./origin')
|
25 | const { parseConfig } = require('./parse')
|
26 | const { getConfigPath } = require('./path')
|
27 |
|
28 | const { getRedirectsPath, addRedirects } = require('./redirects')
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const resolveConfig = async function (opts) {
|
34 | const { cachedConfig, cachedConfigPath, host, scheme, pathPrefix, testOpts, token, offline, ...optsA } =
|
35 | addDefaultOpts(opts)
|
36 |
|
37 | const api = getApiClient({ token, offline, host, scheme, pathPrefix, testOpts })
|
38 |
|
39 | const parsedCachedConfig = await getCachedConfig({ cachedConfig, cachedConfigPath, token, api })
|
40 | if (parsedCachedConfig !== undefined) {
|
41 | return parsedCachedConfig
|
42 | }
|
43 |
|
44 | const {
|
45 | config: configOpt,
|
46 | defaultConfig,
|
47 | inlineConfig,
|
48 | configMutations,
|
49 | cwd,
|
50 | context,
|
51 | repositoryRoot,
|
52 | base,
|
53 | branch,
|
54 | siteId,
|
55 | deployId,
|
56 | buildId,
|
57 | baseRelDir,
|
58 | mode,
|
59 | debug,
|
60 | logs,
|
61 | } = await normalizeOpts(optsA)
|
62 |
|
63 | const { siteInfo, accounts, addons } = await getSiteInfo({ api, siteId, mode, testOpts })
|
64 |
|
65 | const { defaultConfig: defaultConfigA, baseRelDir: baseRelDirA } = parseDefaultConfig({
|
66 | defaultConfig,
|
67 | base,
|
68 | baseRelDir,
|
69 | siteInfo,
|
70 | logs,
|
71 | debug,
|
72 | })
|
73 | const inlineConfigA = getInlineConfig({ inlineConfig, configMutations, logs, debug })
|
74 |
|
75 | const { configPath, config, buildDir, redirectsPath, headersPath } = await loadConfig({
|
76 | configOpt,
|
77 | cwd,
|
78 | context,
|
79 | repositoryRoot,
|
80 | branch,
|
81 | defaultConfig: defaultConfigA,
|
82 | inlineConfig: inlineConfigA,
|
83 | baseRelDir: baseRelDirA,
|
84 | logs,
|
85 | })
|
86 |
|
87 | const env = await getEnv({
|
88 | mode,
|
89 | config,
|
90 | siteInfo,
|
91 | accounts,
|
92 | addons,
|
93 | buildDir,
|
94 | branch,
|
95 | deployId,
|
96 | buildId,
|
97 | context,
|
98 | })
|
99 |
|
100 |
|
101 | const configA = addLegacyFunctionsDirectory(config)
|
102 |
|
103 | const result = {
|
104 | siteInfo,
|
105 | accounts,
|
106 | addons,
|
107 | env,
|
108 | configPath,
|
109 | redirectsPath,
|
110 | headersPath,
|
111 | buildDir,
|
112 | repositoryRoot,
|
113 | config: configA,
|
114 | context,
|
115 | branch,
|
116 | token,
|
117 | api,
|
118 | logs,
|
119 | }
|
120 | logResult(result, { logs, debug })
|
121 | return result
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 | const addLegacyFunctionsDirectory = (config) => {
|
127 | if (!config.functionsDirectory) {
|
128 | return config
|
129 | }
|
130 |
|
131 | return {
|
132 | ...config,
|
133 | build: {
|
134 | ...config.build,
|
135 | functions: config.functionsDirectory,
|
136 | },
|
137 | }
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | const loadConfig = async function ({
|
144 | configOpt,
|
145 | cwd,
|
146 | context,
|
147 | repositoryRoot,
|
148 | branch,
|
149 | defaultConfig,
|
150 | inlineConfig,
|
151 | baseRelDir,
|
152 | logs,
|
153 | }) {
|
154 | const initialBase = getInitialBase({ repositoryRoot, defaultConfig, inlineConfig })
|
155 | const { configPath, config, buildDir, base, redirectsPath, headersPath } = await getFullConfig({
|
156 | configOpt,
|
157 | cwd,
|
158 | context,
|
159 | repositoryRoot,
|
160 | branch,
|
161 | defaultConfig,
|
162 | inlineConfig,
|
163 | baseRelDir,
|
164 | configBase: initialBase,
|
165 | logs,
|
166 | })
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | if (!baseRelDir || base === initialBase) {
|
176 | return { configPath, config, buildDir, redirectsPath, headersPath }
|
177 | }
|
178 |
|
179 | const {
|
180 | configPath: configPathA,
|
181 | config: configA,
|
182 | buildDir: buildDirA,
|
183 | redirectsPath: redirectsPathA,
|
184 | headersPath: headersPathA,
|
185 | } = await getFullConfig({
|
186 | cwd,
|
187 | context,
|
188 | repositoryRoot,
|
189 | branch,
|
190 | defaultConfig,
|
191 | inlineConfig,
|
192 | baseRelDir,
|
193 | configBase: base,
|
194 | base,
|
195 | logs,
|
196 | })
|
197 | return {
|
198 | configPath: configPathA,
|
199 | config: configA,
|
200 | buildDir: buildDirA,
|
201 | redirectsPath: redirectsPathA,
|
202 | headersPath: headersPathA,
|
203 | }
|
204 | }
|
205 |
|
206 |
|
207 | const getFullConfig = async function ({
|
208 | configOpt,
|
209 | cwd,
|
210 | context,
|
211 | repositoryRoot,
|
212 | branch,
|
213 | defaultConfig,
|
214 | inlineConfig,
|
215 | baseRelDir,
|
216 | configBase,
|
217 | base,
|
218 | logs,
|
219 | }) {
|
220 | const configPath = await getConfigPath({ configOpt, cwd, repositoryRoot, configBase })
|
221 |
|
222 | try {
|
223 | const config = await parseConfig(configPath)
|
224 | const configA = mergeAndNormalizeConfig({
|
225 | config,
|
226 | defaultConfig,
|
227 | inlineConfig,
|
228 | context,
|
229 | branch,
|
230 | logs,
|
231 | })
|
232 | const {
|
233 | config: configB,
|
234 | buildDir,
|
235 | base: baseA,
|
236 | } = await resolveFiles({ config: configA, repositoryRoot, base, baseRelDir })
|
237 | const headersPath = getHeadersPath(configB)
|
238 | const configC = await addHeaders(configB, headersPath, logs)
|
239 | const redirectsPath = getRedirectsPath(configC)
|
240 | const configD = await addRedirects(configC, redirectsPath, logs)
|
241 | return { configPath, config: configD, buildDir, base: baseA, redirectsPath, headersPath }
|
242 | } catch (error) {
|
243 | const configName = configPath === undefined ? '' : ` file ${configPath}`
|
244 | error.message = `When resolving config${configName}:\n${error.message}`
|
245 | throw error
|
246 | }
|
247 | }
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | const mergeAndNormalizeConfig = function ({ config, defaultConfig, inlineConfig, context, branch, logs }) {
|
257 | const configA = normalizeConfigAndContext(config, CONFIG_ORIGIN)
|
258 | const defaultConfigA = normalizeConfigAndContext(defaultConfig, UI_ORIGIN)
|
259 | const inlineConfigA = normalizeConfigAndContext(inlineConfig, INLINE_ORIGIN)
|
260 |
|
261 | const configB = mergeConfigs([defaultConfigA, configA])
|
262 | const configC = mergeContext({ config: configB, context, branch, logs })
|
263 | const configD = mergeConfigs([configC, inlineConfigA])
|
264 |
|
265 | const configE = normalizeAfterConfigMerge(configD)
|
266 | return configE
|
267 | }
|
268 |
|
269 | const normalizeConfigAndContext = function (config, origin) {
|
270 | const configA = normalizeBeforeConfigMerge(config, origin)
|
271 | const configB = normalizeContextProps({ config: configA, origin })
|
272 | return configB
|
273 | }
|
274 |
|
275 |
|
276 | const resolveFiles = async function ({ config, repositoryRoot, base, baseRelDir }) {
|
277 | const baseA = getBase(base, repositoryRoot, config)
|
278 | const buildDir = await getBuildDir(repositoryRoot, baseA)
|
279 | const configA = await resolveConfigPaths({ config, repositoryRoot, buildDir, baseRelDir })
|
280 | const configB = addBase(configA, baseA)
|
281 | return { config: configB, buildDir, base: baseA }
|
282 | }
|
283 |
|
284 | module.exports = resolveConfig
|
285 |
|
286 |
|
287 | module.exports.cleanupConfig = cleanupConfig
|
288 | module.exports.updateConfig = updateConfig
|
289 | module.exports.restoreConfig = restoreConfig
|
290 | module.exports.EVENTS = EVENTS
|
291 |
|