-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathdev.ts
More file actions
300 lines (264 loc) · 8.83 KB
/
dev.ts
File metadata and controls
300 lines (264 loc) · 8.83 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import process from 'process'
import type { NetlifyAPI } from '@netlify/api'
import getPort from 'get-port'
import isEmpty from 'lodash/isEmpty.js'
import { supportsBackgroundFunctions } from '../lib/account.js'
import { NETLIFYDEVLOG, chalk, logAndThrowError, log, warn, APIError } from './command-helpers.js'
import { loadDotEnvFiles } from './dot-env.js'
import type { EnvironmentVariables, SiteInfo } from './types.js'
// Possible sources of environment variables. For the purpose of printing log messages only. Order does not matter.
const ENV_VAR_SOURCES = {
account: {
name: 'shared',
printFn: chalk.magenta,
},
addons: {
name: 'addon',
printFn: chalk.yellow,
},
configFile: {
name: 'netlify.toml file',
printFn: chalk.green,
},
general: {
name: 'general context',
printFn: chalk.italic,
},
process: {
name: 'process',
printFn: chalk.red,
},
ui: {
name: 'project settings',
printFn: chalk.blue,
},
}
const ERROR_CALL_TO_ACTION =
"Double-check your login status with 'netlify status' or contact support with details of your error."
const validateSiteInfo = ({ site, siteInfo }: { site: { id?: string }; siteInfo: SiteInfo }) => {
if (isEmpty(siteInfo)) {
return logAndThrowError(
`Failed to retrieve project information for project ${chalk.yellow(site.id)}. ${ERROR_CALL_TO_ACTION}`,
)
}
}
type ApiAccount = Awaited<ReturnType<NetlifyAPI['listAccountsForUser']>>[number]
type Capabilities = NonNullable<ApiAccount['capabilities']> & {
// FIXME(serhalp): `background_functions` is missing from Netlify API account capabilities type
background_functions?:
| {
included?: boolean | undefined
}
| undefined
ai_gateway_disabled?:
| {
included?: boolean | undefined
}
| undefined
}
export type Capability = keyof Capabilities
export type Account = ApiAccount & {
capabilities?: Capabilities
}
const getAccounts = async ({ api }: { api: NetlifyAPI }) => {
try {
const accounts = await api.listAccountsForUser()
return accounts
} catch (error_) {
return logAndThrowError(`Failed retrieving user account: ${(error_ as APIError).message}. ${ERROR_CALL_TO_ACTION}`)
}
}
const getAddons = async ({ api, site }: { api: NetlifyAPI; site: { id?: string } }) => {
const { id } = site
if (!id) {
return []
}
try {
const addons = await api.listServiceInstancesForSite({ siteId: id })
return addons
} catch (error_) {
return logAndThrowError(
`Failed retrieving addons for site ${chalk.yellow(site.id)}: ${
(error_ as APIError).message
}. ${ERROR_CALL_TO_ACTION}`,
)
}
}
type Addon = Awaited<ReturnType<NetlifyAPI['listServiceInstancesForSite']>>[number]
const getAddonsInformation = ({ addons, siteInfo }: { addons: Addon[]; siteInfo: SiteInfo }) => {
const urls = Object.fromEntries(
addons.map((addon) => [addon.service_slug, `${siteInfo.ssl_url}${addon.service_path}`]),
)
const env = Object.assign({}, ...addons.map((addon) => addon.env))
return { urls, env }
}
const getSiteAccount = ({ accounts, siteInfo }: { accounts: Account[]; siteInfo: SiteInfo }): Account | undefined => {
const { account_id: accountId, account_slug: accountSlug } = siteInfo
const siteAccount = accounts.find((account) => account.slug === accountSlug || account.id === accountId)
if (!siteAccount) {
warn(`Could not find account for project '${siteInfo.name}' with account slug '${accountSlug}'`)
return undefined
}
return siteAccount
}
// default 10 seconds for synchronous functions
const SYNCHRONOUS_FUNCTION_TIMEOUT = 30
// default 15 minutes for background functions
const BACKGROUND_FUNCTION_TIMEOUT = 900
interface GetSiteInformationOptions {
api: NetlifyAPI
offline: boolean
site: { id?: string }
siteInfo: SiteInfo
}
export interface SiteInformationResult {
addonsUrls: Record<string, string>
siteUrl: string
accountId?: string
capabilities: {
backgroundFunctions?: boolean
aiGatewayDisabled: boolean
}
timeouts: {
syncFunctions: number
backgroundFunctions: number
}
}
export const getSiteInformation = async ({
api,
offline,
site,
siteInfo,
}: GetSiteInformationOptions): Promise<SiteInformationResult> => {
if (site.id && !offline) {
validateSiteInfo({ site, siteInfo })
const [accounts, addons] = await Promise.all([getAccounts({ api }), getAddons({ api, site })])
const { urls: addonsUrls } = getAddonsInformation({ siteInfo, addons })
const account = getSiteAccount({ siteInfo, accounts })
return {
addonsUrls,
siteUrl: siteInfo.ssl_url,
accountId: account?.id ?? siteInfo.account_id,
capabilities: {
backgroundFunctions: supportsBackgroundFunctions(account),
aiGatewayDisabled: siteInfo.capabilities?.ai_gateway_disabled ?? false,
},
timeouts: {
syncFunctions: siteInfo.functions_timeout ?? siteInfo.functions_config?.timeout ?? SYNCHRONOUS_FUNCTION_TIMEOUT,
backgroundFunctions: BACKGROUND_FUNCTION_TIMEOUT,
},
}
}
// best defaults we can have without retrieving site information
return {
addonsUrls: {},
siteUrl: '',
capabilities: {
aiGatewayDisabled: false,
},
timeouts: {
syncFunctions: SYNCHRONOUS_FUNCTION_TIMEOUT,
backgroundFunctions: BACKGROUND_FUNCTION_TIMEOUT,
},
}
}
const getEnvSourceName = (source: string) => {
const sourceConfig = (
ENV_VAR_SOURCES as Record<string, { name: string; printFn: (s: string) => string } | undefined>
)[source]
const { name = source, printFn = chalk.green } = sourceConfig || {}
return printFn(name)
}
/**
* @param {{devConfig: any, env: Record<string, { sources: string[], value: string}>, site: any}} param0
*/
export const getDotEnvVariables = async ({
devConfig,
env,
site,
}: {
devConfig: { envFiles?: string[]; env_files?: string[]; [key: string]: unknown }
env: EnvironmentVariables
site: { root?: string; [key: string]: unknown }
}): Promise<EnvironmentVariables> => {
const envFiles = devConfig.envFiles || devConfig.env_files
// eslint-disable-next-line no-restricted-properties
const dotEnvFiles = await loadDotEnvFiles({ envFiles, projectDir: site.root || process.cwd() })
dotEnvFiles.forEach(({ env: fileEnv, file }) => {
const newSourceName = `${file} file`
Object.keys(fileEnv).forEach((key) => {
const sources = key in env ? [newSourceName, ...env[key].sources] : [newSourceName]
if (sources.includes('internal')) {
return
}
env[key] = {
sources,
value: fileEnv[key],
}
})
})
return env
}
/**
* Takes a set of environment variables in the format provided by @netlify/config and injects them into `process.env`
*/
export const injectEnvVariables = (env: EnvironmentVariables): void => {
const envVarsToLogByUsedSource: Record<string, string[]> = {}
for (const [key, variable] of Object.entries(env)) {
const existsInProcess = process.env[key] !== undefined
const [usedSource, ...overriddenSources] = existsInProcess ? ['process', ...variable.sources] : variable.sources
const usedSourceName = getEnvSourceName(usedSource)
const isInternal = variable.sources.includes('internal')
overriddenSources.forEach((source) => {
const sourceName = getEnvSourceName(source)
log(
chalk.dim(
`${NETLIFYDEVLOG} Ignored ${chalk.bold(sourceName)} env var: ${chalk.yellow(
key,
)} (defined in ${usedSourceName})`,
),
)
})
if (!existsInProcess || isInternal) {
// Omitting `general` and `internal` env vars to reduce noise in the logs.
if (usedSource !== 'general' && !isInternal) {
envVarsToLogByUsedSource[usedSource] ??= []
envVarsToLogByUsedSource[usedSource].push(key)
}
process.env[key] = variable.value
}
}
for (const [source, keys] of Object.entries(envVarsToLogByUsedSource)) {
const sourceName = getEnvSourceName(source)
log(`${NETLIFYDEVLOG} Injected ${sourceName} env vars: ${keys.map((key) => chalk.yellow(key)).join(', ')}`)
}
}
export const acquirePort = async ({
configuredPort,
defaultPort,
errorMessage,
}: {
configuredPort?: number
defaultPort: number
errorMessage: string
}) => {
const acquiredPort = await getPort({ port: configuredPort || defaultPort })
if (configuredPort && acquiredPort !== configuredPort) {
throw new Error(`${errorMessage}: '${configuredPort}'`)
}
return acquiredPort
}
export const processOnExit = (fn: (codeOrSignal: string | number) => void | Promise<void>) => {
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit']
signals.forEach((signal) => {
process.on(signal, (codeOrSignal) => {
const result = fn(codeOrSignal)
if (result instanceof Promise) {
result.catch(() => {
// ignore
})
}
})
})
}
export const UNLINKED_SITE_MOCK_ID = 'unlinked'