-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathutils.ts
268 lines (240 loc) · 8.34 KB
/
utils.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
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
import { writeFile } from 'fs/promises'
import path from 'path'
import type { Settings } from '@netlify/build-info'
import cleanDeep from 'clean-deep'
import inquirer from 'inquirer'
import type { NetlifyAPI } from 'netlify'
import type BaseCommand from '../../commands/base-command.js'
import { fileExistsAsync } from '../../lib/fs.js'
import { normalizeBackslash } from '../../lib/path.js'
import { detectBuildSettings } from '../build-info.js'
import { chalk, logAndThrowError, log, type NormalizedCachedConfigConfig, warn } from '../command-helpers.js'
import type { Plugin } from '../types.js'
import { getRecommendPlugins, getUIPlugins } from './plugins.js'
const formatTitle = (title: string) => chalk.cyan(title)
/**
* Retrieve a list of plugins to auto install
* @param pluginsToAlwaysInstall these plugins represent runtimes that are
* expected to be "automatically" installed. Even though
* they can be installed on package/toml, we always
* want them installed in the site settings. When installed
* there our build will automatically install the latest without
* user management of the versioning.
*/
export const getPluginsToAutoInstall = (
_command: BaseCommand,
pluginsInstalled: string[] = [],
pluginsRecommended: string[] = [],
) => {
const nextRuntime = '@netlify/plugin-nextjs'
const pluginsToAlwaysInstall = new Set([nextRuntime])
return pluginsRecommended.reduce<string[]>(
(acc, plugin) =>
pluginsInstalled.includes(plugin) && !pluginsToAlwaysInstall.has(plugin) ? acc : [...acc, plugin],
[],
)
}
const normalizeSettings = (settings: Partial<Settings>, config: NormalizedCachedConfigConfig, command: BaseCommand) => {
const plugins = getPluginsToAutoInstall(command, settings.plugins_from_config_file, settings.plugins_recommended)
const recommendedPlugins = getRecommendPlugins(plugins, config)
return {
defaultBaseDir: settings.baseDirectory ?? command.project.relativeBaseDirectory ?? '',
defaultBuildCmd: config.build.command || settings.buildCommand,
defaultBuildDir: settings.dist,
defaultFunctionsDir: config.build.functions || 'netlify/functions',
recommendedPlugins,
}
}
const getPromptInputs = ({
defaultBaseDir,
defaultBuildCmd,
defaultBuildDir,
}: {
defaultBaseDir: string
defaultBuildCmd?: string | undefined
defaultBuildDir?: string | undefined
}) => {
const inputs = [
defaultBaseDir !== '' && {
type: 'input',
name: 'baseDir',
message: 'Base directory `(blank for current dir):',
default: defaultBaseDir,
},
{
type: 'input',
name: 'buildCmd',
message: 'Your build command (hugo build/yarn run build/etc):',
filter: (val: string) => (val === '' ? '# no build command' : val),
default: defaultBuildCmd,
},
{
type: 'input',
name: 'buildDir',
message: 'Directory to deploy (blank for current dir):',
default: defaultBuildDir,
},
].filter(Boolean)
return inputs.filter(Boolean)
}
export const getBuildSettings = async ({
command,
config,
}: {
command: BaseCommand
config: NormalizedCachedConfigConfig
}) => {
const settings = await detectBuildSettings(command)
// TODO: add prompt for asking to choose the build command
const setting: Partial<Settings> = settings.length > 0 ? settings[0] : {}
const { defaultBaseDir, defaultBuildCmd, defaultBuildDir, defaultFunctionsDir, recommendedPlugins } =
normalizeSettings(setting, config, command)
if (recommendedPlugins.length !== 0 && setting.framework?.name) {
log(`Configuring ${formatTitle(setting.framework.name)} runtime...`)
log()
}
const { baseDir, buildCmd, buildDir } = await inquirer.prompt<{
baseDir?: string | undefined
buildCmd: string
buildDir: string
}>(
getPromptInputs({
defaultBaseDir,
defaultBuildCmd,
defaultBuildDir,
}),
)
const pluginsToInstall = recommendedPlugins.map((plugin) => ({ package: plugin }))
const normalizedBaseDir = baseDir ? normalizeBackslash(baseDir) : undefined
return { baseDir: normalizedBaseDir, buildCmd, buildDir, functionsDir: defaultFunctionsDir, pluginsToInstall }
}
const getNetlifyToml = ({
command = '# no build command',
functions = 'functions',
publish = '.',
}) => `# example netlify.toml
[build]
command = "${command}"
functions = "${functions}"
publish = "${publish}"
## Uncomment to use this redirect for Single Page Applications like create-react-app.
## Not needed for static site generators.
#[[redirects]]
# from = "/*"
# to = "/index.html"
# status = 200
## (optional) Settings for Netlify Dev
## https://github.com/netlify/cli/blob/main/docs/netlify-dev.md#project-detection
#[dev]
# command = "yarn start" # Command to start your dev server
# port = 3000 # Port that the dev server will be listening on
# publish = "dist" # Folder with the static content for _redirect file
## more info on configuring this file: https://ntl.fyi/file-based-build-config
`
export const saveNetlifyToml = async ({
baseDir,
buildCmd,
buildDir,
config,
configPath,
functionsDir,
repositoryRoot,
}: {
baseDir: string | undefined
buildCmd: string
buildDir: string
config: NormalizedCachedConfigConfig
configPath: string | undefined
functionsDir: string | undefined
repositoryRoot: string
}) => {
const tomlPathParts = [repositoryRoot, baseDir, 'netlify.toml'].filter(
(part): part is string => part != null && part.length > 0,
)
const tomlPath = path.join(...tomlPathParts)
if (await fileExistsAsync(tomlPath)) {
return
}
// We don't want to create a `netlify.toml` file that overrides existing configuration
// In a monorepo the configuration can come from a repo level netlify.toml
// so we make sure it doesn't by checking `configPath === undefined`
// @ts-expect-error TS(2349)
if (configPath === undefined && Object.keys(cleanDeep(config)).length !== 0) {
return
}
const { makeNetlifyTOML } = await inquirer.prompt([
{
type: 'confirm',
name: 'makeNetlifyTOML',
message: 'No netlify.toml detected. Would you like to create one with these build settings?',
default: true,
},
])
if (makeNetlifyTOML) {
try {
await writeFile(
tomlPath,
getNetlifyToml({ command: buildCmd, publish: buildDir, functions: functionsDir }),
'utf-8',
)
} catch (error) {
warn(`Failed saving Netlify toml file: ${error instanceof Error ? error.message : error?.toString()}`)
}
}
}
// @ts-expect-error TS(7031) FIXME: Binding element 'error' implicitly has an 'any' ty... Remove this comment to see the full error message
export const formatErrorMessage = ({ error, message }) => {
const errorMessage = error.json ? `${error.message} - ${JSON.stringify(error.json)}` : error.message
return `${message} with error: ${chalk.red(errorMessage)}`
}
export type DeployKey = Awaited<ReturnType<NetlifyAPI['createDeployKey']>>
export const createDeployKey = async ({ api }: { api: NetlifyAPI }): Promise<DeployKey> => {
try {
const deployKey = await api.createDeployKey()
return deployKey
} catch (error) {
const message = formatErrorMessage({ message: 'Failed creating deploy key', error })
return logAndThrowError(message)
}
}
// TODO(serhalp): Export convenient named types from `netlify` package to avoid needing bizarre type patterns.
type UpdateSiteRequestBody = Exclude<Parameters<NetlifyAPI['updateSite']>[0]['body'], () => unknown>
export const updateSite = async ({
api,
options,
siteId,
}: {
api: NetlifyAPI
options: UpdateSiteRequestBody
siteId: string
}) => {
try {
const updatedSite = await api.updateSite({ siteId, body: options })
return updatedSite
} catch (error) {
const message = formatErrorMessage({ message: 'Failed updating site with repo information', error })
return logAndThrowError(message)
}
}
export const setupSite = async ({
api,
configPlugins,
pluginsToInstall,
repo,
siteId,
}: {
api: NetlifyAPI
configPlugins: Plugin[]
pluginsToInstall: { package: string }[]
repo: NonNullable<UpdateSiteRequestBody>['repo']
siteId: string
}) => {
const updatedSite = await updateSite({
siteId,
api,
// merge existing plugins with new ones
// @ts-expect-error(serhalp) -- `plugins` is missing from `api.updateSite()` req body type
options: { repo, plugins: [...getUIPlugins(configPlugins), ...pluginsToInstall] },
})
return updatedSite
}