netlify-cli 15.10.0-rc.1 → 15.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/bin/run.mjs +5 -6
  2. package/npm-shrinkwrap.json +3 -6
  3. package/package.json +2 -1
  4. package/src/commands/base-command.mjs +116 -295
  5. package/src/commands/build/build.mjs +1 -9
  6. package/src/commands/deploy/deploy.mjs +9 -23
  7. package/src/commands/dev/dev.mjs +17 -22
  8. package/src/commands/functions/functions-create.mjs +89 -118
  9. package/src/commands/functions/functions-invoke.mjs +7 -10
  10. package/src/commands/functions/functions-list.mjs +2 -2
  11. package/src/commands/init/init.mjs +1 -1
  12. package/src/commands/link/link.mjs +5 -5
  13. package/src/commands/serve/serve.mjs +6 -10
  14. package/src/commands/sites/sites-create-template.mjs +1 -1
  15. package/src/commands/sites/sites-create.mjs +1 -1
  16. package/src/lib/edge-functions/internal.mjs +3 -5
  17. package/src/lib/edge-functions/proxy.mjs +3 -27
  18. package/src/lib/functions/netlify-function.mjs +26 -1
  19. package/src/lib/functions/registry.mjs +14 -26
  20. package/src/lib/functions/runtimes/js/worker.mjs +1 -1
  21. package/src/lib/spinner.mjs +1 -1
  22. package/src/recipes/vscode/index.mjs +6 -24
  23. package/src/utils/command-helpers.mjs +7 -16
  24. package/src/utils/detect-server-settings.mjs +245 -133
  25. package/src/utils/framework-server.mjs +5 -6
  26. package/src/utils/functions/functions.mjs +5 -8
  27. package/src/utils/get-repo-data.mjs +6 -5
  28. package/src/utils/init/config-github.mjs +2 -2
  29. package/src/utils/init/config-manual.mjs +7 -24
  30. package/src/utils/init/frameworks.mjs +23 -0
  31. package/src/utils/init/utils.mjs +63 -62
  32. package/src/utils/proxy-server.mjs +4 -7
  33. package/src/utils/proxy.mjs +3 -4
  34. package/src/utils/read-repo-url.mjs +0 -4
  35. package/src/utils/run-build.mjs +32 -58
  36. package/src/utils/shell.mjs +7 -24
  37. package/src/utils/state-config.mjs +1 -5
  38. package/src/utils/static-server.mjs +0 -4
  39. package/src/utils/build-info.mjs +0 -100
@@ -16,7 +16,7 @@ const normalizeFunction = function (deployedFunctions, { name, urlPath: url }) {
16
16
  * @param {import('../base-command.mjs').default} command
17
17
  */
18
18
  const functionsList = async (options, command) => {
19
- const { config, relConfigFilePath, siteInfo } = command.netlify
19
+ const { config, siteInfo } = command.netlify
20
20
 
21
21
  const deploy = siteInfo.published_deploy || {}
22
22
  const deployedFunctions = deploy.available_functions || []
@@ -25,7 +25,7 @@ const functionsList = async (options, command) => {
25
25
 
26
26
  if (typeof functionsDir === 'undefined') {
27
27
  log('Functions directory is undefined')
28
- log(`Please verify functions.directory is set in your Netlify configuration file ${relConfigFilePath}`)
28
+ log('Please verify functions.directory is set in your Netlify configuration file (netlify.toml/yml/json)')
29
29
  log('See https://docs.netlify.com/configure-builds/file-based-configuration/ for more information')
30
30
  exit(1)
31
31
  }
@@ -196,7 +196,7 @@ export const init = async (options, command) => {
196
196
  }
197
197
 
198
198
  // Look for local repo
199
- const repoData = await getRepoData({ workingDir: command.workingDir, remoteName: options.gitRemoteName })
199
+ const repoData = await getRepoData({ remoteName: options.gitRemoteName })
200
200
  if (repoData.error) {
201
201
  await handleNoGitRemoteAndExit({ command, error: repoData.error, state })
202
202
  }
@@ -11,11 +11,11 @@ import { track } from '../../utils/telemetry/index.mjs'
11
11
 
12
12
  /**
13
13
  *
14
- * @param {import('../base-command.mjs').default} command
14
+ * @param {import('../base-command.mjs').NetlifyOptions} netlify
15
15
  * @param {import('commander').OptionValues} options
16
16
  */
17
- const linkPrompt = async (command, options) => {
18
- const { api, state } = command.netlify
17
+ const linkPrompt = async (netlify, options) => {
18
+ const { api, state } = netlify
19
19
 
20
20
  const SITE_NAME_PROMPT = 'Search by full or partial site name'
21
21
  const SITE_LIST_PROMPT = 'Choose from a list of your recently updated sites'
@@ -24,7 +24,7 @@ const linkPrompt = async (command, options) => {
24
24
  let GIT_REMOTE_PROMPT = 'Use the current git remote origin URL'
25
25
  let site
26
26
  // Get git remote data if exists
27
- const repoData = await getRepoData({ workingDir: command.workingDir, remoteName: options.gitRemoteName })
27
+ const repoData = await getRepoData({ remoteName: options.gitRemoteName })
28
28
 
29
29
  let linkChoices = [SITE_NAME_PROMPT, SITE_LIST_PROMPT, SITE_ID_PROMPT]
30
30
 
@@ -326,7 +326,7 @@ export const link = async (options, command) => {
326
326
  kind: 'byName',
327
327
  })
328
328
  } else {
329
- siteData = await linkPrompt(command, options)
329
+ siteData = await linkPrompt(command.netlify, options)
330
330
  }
331
331
  return siteData
332
332
  }
@@ -38,7 +38,6 @@ const serve = async (options, command) => {
38
38
  const devConfig = {
39
39
  ...(config.functionsDirectory && { functions: config.functionsDirectory }),
40
40
  ...(config.build.publish && { publish: config.build.publish }),
41
-
42
41
  ...config.dev,
43
42
  ...options,
44
43
  // Override the `framework` value so that we start a static server and not
@@ -70,9 +69,10 @@ const serve = async (options, command) => {
70
69
  // Netlify Build are loaded.
71
70
  await getInternalFunctionsDir({ base: site.root, ensureExists: true })
72
71
 
73
- let settings = /** @type {import('../../utils/types.js').ServerSettings} */ ({})
72
+ /** @type {Partial<import('../../utils/types').ServerSettings>} */
73
+ let settings = {}
74
74
  try {
75
- settings = await detectServerSettings(devConfig, options, command)
75
+ settings = await detectServerSettings(devConfig, options, site.root)
76
76
 
77
77
  cachedConfig.config = getConfigWithPlugins(cachedConfig.config, settings)
78
78
  } catch (error_) {
@@ -87,11 +87,7 @@ const serve = async (options, command) => {
87
87
  `${NETLIFYDEVWARN} Changes will not be hot-reloaded, so if you need to rebuild your site you must exit and run 'netlify serve' again`,
88
88
  )
89
89
 
90
- const { configPath: configPathOverride } = await runBuildTimeline({
91
- command,
92
- settings,
93
- options,
94
- })
90
+ const { configPath: configPathOverride } = await runBuildTimeline({ cachedConfig, options, settings, site })
95
91
 
96
92
  await startFunctionsServer({
97
93
  api,
@@ -121,7 +117,8 @@ const serve = async (options, command) => {
121
117
 
122
118
  // TODO: We should consolidate this with the existing config watcher.
123
119
  const getUpdatedConfig = async () => {
124
- const { config: newConfig } = await command.getConfig({ cwd: command.workingDir, offline: true, state })
120
+ const cwd = options.cwd || process.cwd()
121
+ const { config: newConfig } = await command.getConfig({ cwd, offline: true, state })
125
122
  const normalizedNewConfig = normalizeConfig(newConfig)
126
123
 
127
124
  return normalizedNewConfig
@@ -138,7 +135,6 @@ const serve = async (options, command) => {
138
135
  getUpdatedConfig,
139
136
  inspectSettings,
140
137
  offline: options.offline,
141
- projectDir: command.workingDir,
142
138
  settings,
143
139
  site,
144
140
  siteInfo,
@@ -197,7 +197,7 @@ const sitesCreateTemplate = async (repository, options, command) => {
197
197
 
198
198
  if (options.withCi) {
199
199
  log('Configuring CI')
200
- const repoData = await getRepoData({ workingDir: command.workingDir })
200
+ const repoData = await getRepoData()
201
201
  await configureRepo({ command, siteId: site.id, repoData, manual: options.manual })
202
202
  }
203
203
 
@@ -102,7 +102,7 @@ export const sitesCreate = async (options, command) => {
102
102
 
103
103
  if (options.withCi) {
104
104
  log('Configuring CI')
105
- const repoData = await getRepoData({ workingDir: command.workingDir })
105
+ const repoData = await getRepoData()
106
106
  await configureRepo({ command, siteId: site.id, repoData, manual: options.manual })
107
107
  }
108
108
 
@@ -1,16 +1,14 @@
1
1
  // @ts-check
2
2
  import { readFile, stat } from 'fs/promises'
3
3
  import { dirname, join, resolve } from 'path'
4
+ import { cwd } from 'process'
4
5
 
5
6
  import { getPathInProject } from '../settings.mjs'
6
7
 
7
8
  import { INTERNAL_EDGE_FUNCTIONS_FOLDER } from './consts.mjs'
8
9
 
9
- /**
10
- * @param {string} workingDir
11
- */
12
- export const getInternalFunctions = async (workingDir) => {
13
- const path = join(workingDir, getPathInProject([INTERNAL_EDGE_FUNCTIONS_FOLDER]))
10
+ export const getInternalFunctions = async () => {
11
+ const path = join(cwd(), getPathInProject([INTERNAL_EDGE_FUNCTIONS_FOLDER]))
14
12
 
15
13
  try {
16
14
  const stats = await stat(path)
@@ -1,7 +1,7 @@
1
1
  // @ts-check
2
2
  import { Buffer } from 'buffer'
3
3
  import { relative } from 'path'
4
- import { env } from 'process'
4
+ import { cwd, env } from 'process'
5
5
 
6
6
  // eslint-disable-next-line import/no-namespace
7
7
  import * as bundler from '@netlify/edge-bundler'
@@ -62,26 +62,6 @@ export const createAccountInfoHeader = (accountInfo = {}) => {
62
62
  return Buffer.from(accountString).toString('base64')
63
63
  }
64
64
 
65
- /**
66
- *
67
- * @param {object} config
68
- * @param {*} config.accountId
69
- * @param {*} config.config
70
- * @param {*} config.configPath
71
- * @param {*} config.debug
72
- * @param {*} config.env
73
- * @param {*} config.geoCountry
74
- * @param {*} config.geolocationMode
75
- * @param {*} config.getUpdatedConfig
76
- * @param {*} config.inspectSettings
77
- * @param {*} config.mainPort
78
- * @param {boolean=} config.offline
79
- * @param {*} config.passthroughPort
80
- * @param {*} config.projectDir
81
- * @param {*} config.siteInfo
82
- * @param {*} config.state
83
- * @returns
84
- */
85
65
  export const initializeProxy = async ({
86
66
  accountId,
87
67
  config,
@@ -99,11 +79,7 @@ export const initializeProxy = async ({
99
79
  siteInfo,
100
80
  state,
101
81
  }) => {
102
- const {
103
- functions: internalFunctions,
104
- importMap,
105
- path: internalFunctionsPath,
106
- } = await getInternalFunctions(projectDir)
82
+ const { functions: internalFunctions, importMap, path: internalFunctionsPath } = await getInternalFunctions()
107
83
  const userFunctionsPath = config.build.edge_functions
108
84
  const isolatePort = await getAvailablePort()
109
85
 
@@ -157,7 +133,7 @@ export const initializeProxy = async ({
157
133
  )} matches declaration for edge function ${chalk.yellow(
158
134
  functionName,
159
135
  )}, but there's no matching function file in ${chalk.yellow(
160
- relative(projectDir, userFunctionsPath),
136
+ relative(cwd(), userFunctionsPath),
161
137
  )}. Please visit ${chalk.blue('https://ntl.fyi/edge-create')} for more information.`,
162
138
  )
163
139
  })
@@ -1,9 +1,14 @@
1
1
  // @ts-check
2
+ import { version as nodeVersion } from 'process'
3
+
2
4
  import CronParser from 'cron-parser'
5
+ import semver from 'semver'
3
6
 
4
7
  import { error as errorExit } from '../../utils/command-helpers.mjs'
5
8
  import { BACKGROUND } from '../../utils/functions/get-functions.mjs'
6
9
 
10
+ const V2_MIN_NODE_VERSION = '18.0.0'
11
+
7
12
  // Returns a new set with all elements of `setA` that don't exist in `setB`.
8
13
  const difference = (setA, setB) => new Set([...setA].filter((item) => !setB.has(item)))
9
14
 
@@ -27,12 +32,13 @@ export default class NetlifyFunction {
27
32
  timeoutBackground,
28
33
  timeoutSynchronous,
29
34
  }) {
35
+ this.buildError = null
30
36
  this.config = config
31
37
  this.directory = directory
32
38
  this.errorExit = errorExit
33
39
  this.mainFile = mainFile
34
40
  this.name = name
35
- this.displayName = displayName
41
+ this.displayName = displayName ?? name
36
42
  this.projectRoot = projectRoot
37
43
  this.runtime = runtime
38
44
  this.timeoutBackground = timeoutBackground
@@ -63,6 +69,10 @@ export default class NetlifyFunction {
63
69
  return Boolean(this.schedule)
64
70
  }
65
71
 
72
+ isSupported() {
73
+ return !(this.buildData?.runtimeAPIVersion === 2 && semver.lt(nodeVersion, V2_MIN_NODE_VERSION))
74
+ }
75
+
66
76
  async getNextRun() {
67
77
  if (!(await this.isScheduled())) {
68
78
  return null
@@ -93,11 +103,22 @@ export default class NetlifyFunction {
93
103
  const srcFilesDiff = this.getSrcFilesDiff(srcFilesSet)
94
104
 
95
105
  this.buildData = buildData
106
+ this.buildError = null
96
107
  this.srcFiles = srcFilesSet
97
108
  this.schedule = schedule || this.schedule
98
109
 
110
+ if (!this.isSupported()) {
111
+ throw new Error(
112
+ `Function requires Node.js version ${V2_MIN_NODE_VERSION} or above, but ${nodeVersion.slice(
113
+ 1,
114
+ )} is installed. Refer to https://ntl.fyi/functions-node18 for information on how to update.`,
115
+ )
116
+ }
117
+
99
118
  return { includedFiles, srcFilesDiff }
100
119
  } catch (error) {
120
+ this.buildError = error
121
+
101
122
  return { error }
102
123
  }
103
124
  }
@@ -118,6 +139,10 @@ export default class NetlifyFunction {
118
139
  async invoke(event, context) {
119
140
  await this.buildQueue
120
141
 
142
+ if (this.buildError) {
143
+ return { result: null, error: { errorMessage: this.buildError.message } }
144
+ }
145
+
121
146
  const timeout = this.isBackground ? this.timeoutBackground : this.timeoutSynchronous
122
147
 
123
148
  try {
@@ -6,15 +6,7 @@ import { env } from 'process'
6
6
  import { listFunctions } from '@netlify/zip-it-and-ship-it'
7
7
  import extractZip from 'extract-zip'
8
8
 
9
- import {
10
- chalk,
11
- getTerminalLink,
12
- log,
13
- NETLIFYDEVERR,
14
- NETLIFYDEVLOG,
15
- warn,
16
- watchDebounced,
17
- } from '../../utils/command-helpers.mjs'
9
+ import { chalk, log, NETLIFYDEVERR, NETLIFYDEVLOG, warn, watchDebounced } from '../../utils/command-helpers.mjs'
18
10
  import { INTERNAL_FUNCTIONS_FOLDER, SERVE_FUNCTIONS_FOLDER } from '../../utils/functions/functions.mjs'
19
11
  import { BACKGROUND_FUNCTIONS_WARNING } from '../log.mjs'
20
12
  import { getPathInProject } from '../settings.mjs'
@@ -70,21 +62,23 @@ export class FunctionsRegistry {
70
62
  )
71
63
  }
72
64
 
73
- async buildFunctionAndWatchFiles(func, { verbose = false } = {}) {
74
- if (verbose) {
75
- log(`${NETLIFYDEVLOG} ${chalk.magenta('Reloading')} function ${chalk.yellow(func.name)}...`)
65
+ async buildFunctionAndWatchFiles(func, firstLoad = false) {
66
+ if (!firstLoad) {
67
+ log(`${NETLIFYDEVLOG} ${chalk.magenta('Reloading')} function ${chalk.yellow(func.displayName)}...`)
76
68
  }
77
69
 
78
- const { error_, includedFiles, srcFilesDiff } = await func.build({ cache: this.buildCache })
70
+ const { error: buildError, includedFiles, srcFilesDiff } = await func.build({ cache: this.buildCache })
79
71
 
80
- if (error_) {
72
+ if (buildError) {
81
73
  log(
82
- `${NETLIFYDEVERR} ${chalk.red('Failed')} reloading function ${chalk.yellow(func.name)} with error:\n${
83
- error_.message
74
+ `${NETLIFYDEVERR} ${chalk.red('Failed to load')} function ${chalk.yellow(func.displayName)}: ${
75
+ buildError.message
84
76
  }`,
85
77
  )
86
- } else if (verbose) {
87
- log(`${NETLIFYDEVLOG} ${chalk.green('Reloaded')} function ${chalk.yellow(func.name)}`)
78
+ } else {
79
+ const verb = firstLoad ? 'Loaded' : 'Reloaded'
80
+
81
+ log(`${NETLIFYDEVLOG} ${chalk.green(verb)} function ${chalk.yellow(func.displayName)}`)
88
82
  }
89
83
 
90
84
  // If the build hasn't resulted in any files being added or removed, there
@@ -116,7 +110,7 @@ export class FunctionsRegistry {
116
110
 
117
111
  const newWatcher = await watchDebounced(filesToWatch, {
118
112
  onChange: () => {
119
- this.buildFunctionAndWatchFiles(func, { verbose: true })
113
+ this.buildFunctionAndWatchFiles(func, false)
120
114
  },
121
115
  })
122
116
 
@@ -163,14 +157,8 @@ export class FunctionsRegistry {
163
157
  }
164
158
 
165
159
  this.functions.set(name, func)
166
- this.buildFunctionAndWatchFiles(func)
167
160
 
168
- log(
169
- `${NETLIFYDEVLOG} ${chalk.green('Loaded')} function ${getTerminalLink(
170
- chalk.yellow(func.displayName || name),
171
- func.url,
172
- )}.`,
173
- )
161
+ this.buildFunctionAndWatchFiles(func, true)
174
162
  }
175
163
 
176
164
  // This function is here so we can mock it in tests
@@ -11,7 +11,7 @@ if (isMainThread) {
11
11
 
12
12
  sourceMapSupport.install()
13
13
 
14
- lambdaLocal.getLogger().level = 'warn'
14
+ lambdaLocal.getLogger().level = 'alert'
15
15
 
16
16
  const { clientContext, entryFilePath, event, timeoutMs } = workerData
17
17
 
@@ -17,7 +17,7 @@ export const startSpinner = ({ text }) =>
17
17
  * Stops the spinner with the following text
18
18
  * @param {object} config
19
19
  * @param {ora.Ora} config.spinner
20
- * @param {boolean} [config.error]
20
+ * @param {object} [config.error]
21
21
  * @param {string} [config.text]
22
22
  * @returns {void}
23
23
  */
@@ -1,4 +1,3 @@
1
- // @ts-check
2
1
  import { join } from 'path'
3
2
 
4
3
  import { DenoBridge } from '@netlify/edge-bundler'
@@ -28,24 +27,15 @@ const getPrompt = ({ fileExists, path }) => {
28
27
  const getEdgeFunctionsPath = ({ config, repositoryRoot }) =>
29
28
  config.build.edge_functions || join(repositoryRoot, 'netlify', 'edge-functions')
30
29
 
31
- /**
32
- * @param {string} repositoryRoot
33
- */
34
30
  const getSettingsPath = (repositoryRoot) => join(repositoryRoot, '.vscode', 'settings.json')
35
31
 
36
- /**
37
- * @param {string} repositoryRoot
38
- */
39
- const hasDenoVSCodeExt = async (repositoryRoot) => {
40
- const { stdout: extensions } = await execa('code', ['--list-extensions'], { stderr: 'inherit', cwd: repositoryRoot })
32
+ const hasDenoVSCodeExt = async () => {
33
+ const { stdout: extensions } = await execa('code', ['--list-extensions'], { stderr: 'inherit' })
41
34
  return extensions.split('\n').includes('denoland.vscode-deno')
42
35
  }
43
36
 
44
- /**
45
- * @param {string} repositoryRoot
46
- */
47
- const getDenoVSCodeExt = async (repositoryRoot) => {
48
- await execa('code', ['--install-extension', 'denoland.vscode-deno'], { stdio: 'inherit', cwd: repositoryRoot })
37
+ const getDenoVSCodeExt = async () => {
38
+ await execa('code', ['--install-extension', 'denoland.vscode-deno'], { stdio: 'inherit' })
49
39
  }
50
40
 
51
41
  const getDenoExtPrompt = () => {
@@ -59,12 +49,6 @@ const getDenoExtPrompt = () => {
59
49
  })
60
50
  }
61
51
 
62
- /**
63
- * @param {object} params
64
- * @param {*} params.config
65
- * @param {string} params.repositoryRoot
66
- * @returns
67
- */
68
52
  export const run = async ({ config, repositoryRoot }) => {
69
53
  const deno = new DenoBridge({
70
54
  onBeforeDownload: () =>
@@ -82,11 +66,9 @@ export const run = async ({ config, repositoryRoot }) => {
82
66
  }
83
67
 
84
68
  try {
85
- if (!(await hasDenoVSCodeExt(repositoryRoot))) {
69
+ if (!(await hasDenoVSCodeExt())) {
86
70
  const { confirm: denoExtConfirm } = await getDenoExtPrompt()
87
- if (denoExtConfirm) {
88
- getDenoVSCodeExt(repositoryRoot)
89
- }
71
+ if (denoExtConfirm) getDenoVSCodeExt()
90
72
  }
91
73
  } catch {
92
74
  log(
@@ -24,7 +24,7 @@ const argv = process.argv.slice(2)
24
24
  * Chalk instance for CLI that can be initialized with no colors mode
25
25
  * needed for json outputs where we don't want to have colors
26
26
  * @param {boolean} noColors - disable chalk colors
27
- * @return {import('chalk').ChalkInstance} - default or custom chalk instance
27
+ * @return {object} - default or custom chalk instance
28
28
  */
29
29
  const safeChalk = function (noColors) {
30
30
  if (noColors) {
@@ -174,18 +174,12 @@ export const warn = (message = '') => {
174
174
 
175
175
  /**
176
176
  * throws an error or log it
177
- * @param {unknown} message
177
+ * @param {string|Error} message
178
178
  * @param {object} [options]
179
179
  * @param {boolean} [options.exit]
180
180
  */
181
181
  export const error = (message = '', options = {}) => {
182
- const err =
183
- message instanceof Error
184
- ? message
185
- : // eslint-disable-next-line unicorn/no-nested-ternary
186
- typeof message === 'string'
187
- ? new Error(message)
188
- : /** @type {Error} */ ({ message, stack: undefined, name: 'Error' })
182
+ const err = message instanceof Error ? message : new Error(message)
189
183
 
190
184
  if (options.exit === false) {
191
185
  const bang = chalk.red(BANG)
@@ -204,13 +198,10 @@ export const exit = (code = 0) => {
204
198
  process.exit(code)
205
199
  }
206
200
 
207
- /**
208
- * When `build.publish` is not set by the user, the CLI behavior differs in
209
- * several ways. It detects it by checking if `build.publish` is `undefined`.
210
- * However, `@netlify/config` adds a default value to `build.publish`.
211
- * This removes 'publish' and 'publishOrigin' in this case.
212
- * @param {*} config
213
- */
201
+ // When `build.publish` is not set by the user, the CLI behavior differs in
202
+ // several ways. It detects it by checking if `build.publish` is `undefined`.
203
+ // However, `@netlify/config` adds a default value to `build.publish`.
204
+ // This removes 'publish' and 'publishOrigin' in this case.
214
205
  export const normalizeConfig = (config) => {
215
206
  // Unused var here is in order to omit 'publish' from build config
216
207
  // eslint-disable-next-line no-unused-vars