netlify-cli 8.0.13 → 8.1.0-rc.2

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 (184) hide show
  1. package/README.md +12 -13
  2. package/bin/run +38 -2
  3. package/npm-shrinkwrap.json +260 -6244
  4. package/package.json +13 -35
  5. package/src/commands/addons/addons-auth.js +50 -0
  6. package/src/commands/addons/addons-config.js +180 -0
  7. package/src/commands/addons/addons-create.js +131 -0
  8. package/src/commands/addons/addons-delete.js +60 -0
  9. package/src/commands/addons/addons-list.js +62 -0
  10. package/src/commands/addons/addons.js +44 -0
  11. package/src/commands/addons/index.js +3 -24
  12. package/src/commands/api/api.js +75 -0
  13. package/src/commands/api/index.js +5 -0
  14. package/src/commands/base-command.js +509 -0
  15. package/src/commands/build/build.js +58 -0
  16. package/src/commands/build/index.js +3 -61
  17. package/src/commands/completion/completion.js +36 -0
  18. package/src/commands/completion/index.js +5 -0
  19. package/src/commands/{deploy.js → deploy/deploy.js} +295 -275
  20. package/src/commands/deploy/index.js +5 -0
  21. package/src/commands/dev/dev-exec.js +35 -0
  22. package/src/commands/dev/dev-trace.js +47 -0
  23. package/src/commands/dev/dev.js +340 -0
  24. package/src/commands/dev/index.js +3 -335
  25. package/src/commands/env/env-get.js +51 -0
  26. package/src/commands/env/env-import.js +93 -0
  27. package/src/commands/env/env-list.js +63 -0
  28. package/src/commands/env/env-set.js +67 -0
  29. package/src/commands/env/env-unset.js +66 -0
  30. package/src/commands/env/env.js +42 -0
  31. package/src/commands/env/index.js +3 -23
  32. package/src/commands/functions/functions-build.js +59 -0
  33. package/src/commands/functions/{create.js → functions-create.js} +130 -94
  34. package/src/commands/functions/functions-invoke.js +273 -0
  35. package/src/commands/functions/functions-list.js +106 -0
  36. package/src/commands/functions/functions-serve.js +63 -0
  37. package/src/commands/functions/functions.js +47 -0
  38. package/src/commands/functions/index.js +3 -45
  39. package/src/commands/index.js +7 -0
  40. package/src/commands/init/index.js +6 -0
  41. package/src/commands/{init.js → init/init.js} +79 -68
  42. package/src/commands/link/index.js +6 -0
  43. package/src/{utils/link/link-by-prompt.js → commands/link/link.js} +141 -14
  44. package/src/commands/lm/index.js +3 -19
  45. package/src/commands/lm/lm-info.js +42 -0
  46. package/src/commands/lm/lm-install.js +33 -0
  47. package/src/commands/lm/lm-setup.js +106 -0
  48. package/src/commands/lm/lm-uninstall.js +25 -0
  49. package/src/commands/lm/lm.js +34 -0
  50. package/src/commands/login/index.js +6 -0
  51. package/src/commands/login/login.js +55 -0
  52. package/src/commands/logout/index.js +5 -0
  53. package/src/commands/logout/logout.js +43 -0
  54. package/src/commands/main.js +188 -0
  55. package/src/commands/open/index.js +3 -39
  56. package/src/commands/open/open-admin.js +60 -0
  57. package/src/commands/open/open-site.js +53 -0
  58. package/src/commands/open/open.js +38 -0
  59. package/src/commands/sites/index.js +5 -20
  60. package/src/commands/sites/sites-create.js +187 -0
  61. package/src/commands/sites/sites-delete.js +104 -0
  62. package/src/commands/sites/sites-list.js +89 -0
  63. package/src/commands/sites/sites.js +32 -0
  64. package/src/commands/status/index.js +3 -118
  65. package/src/commands/status/status-hooks.js +69 -0
  66. package/src/commands/status/status.js +124 -0
  67. package/src/commands/switch/index.js +5 -0
  68. package/src/commands/switch/switch.js +50 -0
  69. package/src/commands/unlink/index.js +5 -0
  70. package/src/commands/unlink/unlink.js +48 -0
  71. package/src/commands/watch/index.js +5 -0
  72. package/src/commands/watch/watch.js +121 -0
  73. package/src/lib/build.js +21 -7
  74. package/src/lib/exec-fetcher.js +5 -3
  75. package/src/lib/fs.js +54 -36
  76. package/src/lib/functions/background.js +1 -1
  77. package/src/lib/functions/form-submissions-handler.js +2 -1
  78. package/src/lib/functions/local-proxy.js +2 -1
  79. package/src/lib/functions/netlify-function.js +4 -1
  80. package/src/lib/functions/registry.js +4 -6
  81. package/src/lib/functions/runtimes/go/index.js +2 -1
  82. package/src/lib/functions/runtimes/js/builders/netlify-lambda.js +6 -4
  83. package/src/lib/functions/runtimes/js/builders/zisi.js +3 -3
  84. package/src/lib/functions/runtimes/rust/index.js +4 -3
  85. package/src/lib/functions/server.js +2 -3
  86. package/src/lib/functions/synchronous.js +2 -1
  87. package/src/lib/functions/utils.js +2 -3
  88. package/src/lib/functions/watcher.js +1 -0
  89. package/src/lib/http-agent.js +5 -5
  90. package/src/lib/log.js +2 -1
  91. package/src/lib/spinner.js +22 -0
  92. package/src/utils/addons/diffs/index.js +1 -0
  93. package/src/utils/addons/diffs/options.js +3 -1
  94. package/src/utils/addons/prepare.js +13 -6
  95. package/src/utils/addons/prompts.js +2 -1
  96. package/src/utils/addons/render.js +3 -1
  97. package/src/utils/command-helpers.js +116 -43
  98. package/src/utils/create-stream-promise.js +5 -5
  99. package/src/utils/deferred.js +1 -0
  100. package/src/utils/deploy/deploy-site.js +1 -1
  101. package/src/utils/deploy/index.js +4 -0
  102. package/src/utils/detect-server-settings.js +10 -12
  103. package/src/utils/dev.js +18 -10
  104. package/src/utils/dot-env.js +4 -2
  105. package/src/utils/{edge-handlers.js → functions/edge-handlers.js} +8 -7
  106. package/src/utils/functions/functions.js +36 -0
  107. package/src/utils/{get-functions.js → functions/get-functions.js} +2 -1
  108. package/src/utils/functions/index.js +8 -26
  109. package/src/utils/get-global-config.js +3 -2
  110. package/src/utils/get-repo-data.js +1 -0
  111. package/src/utils/gh-auth.js +1 -0
  112. package/src/utils/gitignore.js +7 -5
  113. package/src/utils/headers.js +1 -2
  114. package/src/utils/index.js +42 -0
  115. package/src/utils/init/config-github.js +12 -5
  116. package/src/utils/init/config-manual.js +9 -2
  117. package/src/utils/init/config.js +13 -7
  118. package/src/utils/init/frameworks.js +1 -0
  119. package/src/utils/init/node-version.js +4 -2
  120. package/src/utils/init/plugins.js +2 -4
  121. package/src/utils/init/utils.js +10 -6
  122. package/src/utils/live-tunnel.js +3 -4
  123. package/src/utils/lm/install.js +10 -15
  124. package/src/utils/lm/requirements.js +3 -1
  125. package/src/utils/lm/steps.js +1 -1
  126. package/src/utils/lm/ui.js +7 -3
  127. package/src/utils/open-browser.js +8 -2
  128. package/src/utils/parse-raw-flags.js +4 -4
  129. package/src/utils/proxy.js +6 -5
  130. package/src/utils/read-repo-url.js +1 -0
  131. package/src/utils/redirects.js +3 -5
  132. package/src/utils/rules-proxy.js +2 -1
  133. package/src/utils/state-config.js +1 -1
  134. package/src/utils/telemetry/index.js +2 -113
  135. package/src/utils/telemetry/request.js +3 -1
  136. package/src/utils/telemetry/telemetry.js +117 -0
  137. package/src/utils/telemetry/validation.js +13 -12
  138. package/src/utils/traffic-mesh.js +3 -3
  139. package/oclif.manifest.json +0 -1
  140. package/src/commands/addons/auth.js +0 -42
  141. package/src/commands/addons/config.js +0 -177
  142. package/src/commands/addons/create.js +0 -127
  143. package/src/commands/addons/delete.js +0 -69
  144. package/src/commands/addons/list.js +0 -54
  145. package/src/commands/api.js +0 -84
  146. package/src/commands/dev/exec.js +0 -32
  147. package/src/commands/dev/trace.js +0 -61
  148. package/src/commands/env/get.js +0 -44
  149. package/src/commands/env/import.js +0 -90
  150. package/src/commands/env/list.js +0 -49
  151. package/src/commands/env/set.js +0 -64
  152. package/src/commands/env/unset.js +0 -58
  153. package/src/commands/functions/build.js +0 -60
  154. package/src/commands/functions/invoke.js +0 -277
  155. package/src/commands/functions/list.js +0 -102
  156. package/src/commands/functions/serve.js +0 -70
  157. package/src/commands/link.js +0 -133
  158. package/src/commands/lm/info.js +0 -36
  159. package/src/commands/lm/install.js +0 -30
  160. package/src/commands/lm/setup.js +0 -107
  161. package/src/commands/lm/uninstall.js +0 -17
  162. package/src/commands/login.js +0 -54
  163. package/src/commands/logout.js +0 -37
  164. package/src/commands/open/admin.js +0 -51
  165. package/src/commands/open/site.js +0 -43
  166. package/src/commands/sites/create.js +0 -191
  167. package/src/commands/sites/delete.js +0 -116
  168. package/src/commands/sites/list.js +0 -84
  169. package/src/commands/status/hooks.js +0 -60
  170. package/src/commands/switch.js +0 -44
  171. package/src/commands/unlink.js +0 -38
  172. package/src/commands/watch.js +0 -115
  173. package/src/hooks/init.js +0 -46
  174. package/src/index.js +0 -25
  175. package/src/lib/help.js +0 -26
  176. package/src/utils/chalk.js +0 -16
  177. package/src/utils/check-command-inputs.js +0 -21
  178. package/src/utils/command.js +0 -261
  179. package/src/utils/detect-functions-builder.js +0 -25
  180. package/src/utils/difference.js +0 -4
  181. package/src/utils/header.js +0 -18
  182. package/src/utils/logo.js +0 -11
  183. package/src/utils/show-help.js +0 -5
  184. package/src/utils/telemetry/tracked-command.js +0 -51
@@ -1,3 +1,5 @@
1
+ // @ts-check
2
+ const { readFile } = require('fs').promises
1
3
  const { dirname, extname, join, resolve } = require('path')
2
4
  const { platform } = require('process')
3
5
 
@@ -6,8 +8,7 @@ const toml = require('toml')
6
8
 
7
9
  const isWindows = platform === 'win32'
8
10
 
9
- const execa = require('../../../../utils/execa')
10
- const { readFileAsync } = require('../../../fs')
11
+ const { execa } = require('../../../../utils')
11
12
  const { getPathInProject } = require('../../../settings')
12
13
  const { runFunctionsProxy } = require('../../local-proxy')
13
14
 
@@ -36,7 +37,7 @@ const getBuildFunction =
36
37
 
37
38
  const getCrateName = async (cwd) => {
38
39
  const manifestPath = await findUp('Cargo.toml', { cwd, type: 'file' })
39
- const manifest = await readFileAsync(manifestPath)
40
+ const manifest = await readFile(manifestPath, 'utf-8')
40
41
  const { package } = toml.parse(manifest)
41
42
 
42
43
  return package.name
@@ -1,8 +1,7 @@
1
+ // @ts-check
1
2
  const jwtDecode = require('jwt-decode')
2
3
 
3
- const { error: errorExit, log } = require('../../utils/command-helpers')
4
- const { getInternalFunctionsDir } = require('../../utils/functions')
5
- const { NETLIFYDEVERR, NETLIFYDEVLOG } = require('../../utils/logo')
4
+ const { NETLIFYDEVERR, NETLIFYDEVLOG, error: errorExit, getInternalFunctionsDir, log } = require('../../utils')
6
5
 
7
6
  const { handleBackgroundFunction, handleBackgroundFunctionResult } = require('./background')
8
7
  const { createFormSubmissionHandler } = require('./form-submissions-handler')
@@ -1,6 +1,7 @@
1
+ // @ts-check
1
2
  const { Buffer } = require('buffer')
2
3
 
3
- const { NETLIFYDEVERR } = require('../../utils/logo')
4
+ const { NETLIFYDEVERR } = require('../../utils')
4
5
 
5
6
  const { detectAwsSdkError } = require('./utils')
6
7
 
@@ -1,6 +1,5 @@
1
- const chalk = require('chalk')
2
-
3
- const { warn } = require('../../utils/command-helpers')
1
+ // @ts-check
2
+ const { chalk, warn } = require('../../utils')
4
3
  const { getLogMessage } = require('../log')
5
4
 
6
5
  const DEFAULT_LAMBDA_OPTIONS = {
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  const chokidar = require('chokidar')
2
3
  const decache = require('decache')
3
4
  const debounce = require('lodash/debounce')
@@ -1,10 +1,10 @@
1
+ // @ts-check
2
+ const { readFile } = require('fs').promises
3
+
1
4
  const { HttpsProxyAgent } = require('https-proxy-agent')
2
5
  const waitPort = require('wait-port')
3
6
 
4
- const { exit, log } = require('../utils/command-helpers')
5
- const { NETLIFYDEVERR, NETLIFYDEVWARN } = require('../utils/logo')
6
-
7
- const fs = require('./fs')
7
+ const { NETLIFYDEVERR, NETLIFYDEVWARN, exit, log } = require('../utils')
8
8
 
9
9
  // https://github.com/TooTallNate/node-https-proxy-agent/issues/89
10
10
  class HttpsProxyAgentWithCA extends HttpsProxyAgent {
@@ -66,7 +66,7 @@ const tryGetAgent = async ({ certificateFile, httpProxy }) => {
66
66
  let certificate
67
67
  if (certificateFile) {
68
68
  try {
69
- certificate = await fs.readFileAsync(certificateFile)
69
+ certificate = await readFile(certificateFile)
70
70
  } catch (error) {
71
71
  response = { warning: `Could not read certificate file '${certificateFile}'.`, message: error.message }
72
72
  }
package/src/lib/log.js CHANGED
@@ -1,6 +1,7 @@
1
- const chalk = require('chalk')
2
1
  const dotProp = require('dot-prop')
3
2
 
3
+ const { chalk } = require('../utils')
4
+
4
5
  const RED_BACKGROUND = chalk.red('-background')
5
6
  const [PRO, BUSINESS, ENTERPRISE] = ['Pro', 'Business', 'Enterprise'].map((plan) => chalk.magenta(plan))
6
7
  const BACKGROUND_FUNCTIONS_WARNING = `A serverless function ending in \`${RED_BACKGROUND}\` was detected.
@@ -1,15 +1,31 @@
1
+ // @ts-check
1
2
  const logSymbols = require('log-symbols')
2
3
  const ora = require('ora')
3
4
 
5
+ /**
6
+ * Creates a spinner with the following text
7
+ * @param {object} config
8
+ * @param {string} config.text
9
+ * @returns {ora.Ora}
10
+ */
4
11
  const startSpinner = ({ text }) =>
5
12
  ora({
6
13
  text,
7
14
  }).start()
8
15
 
16
+ /**
17
+ * Stops the spinner with the following text
18
+ * @param {object} config
19
+ * @param {object} [config.error]
20
+ * @param {ora.Ora} config.spinner
21
+ * @param {string} config.text
22
+ * @returns {void}
23
+ */
9
24
  const stopSpinner = ({ error, spinner, text }) => {
10
25
  if (!spinner) {
11
26
  return
12
27
  }
28
+ // TODO: refactor no package needed `log-symbols` for that
13
29
  const symbol = error ? logSymbols.error : logSymbols.success
14
30
  spinner.stopAndPersist({
15
31
  text,
@@ -17,6 +33,12 @@ const stopSpinner = ({ error, spinner, text }) => {
17
33
  })
18
34
  }
19
35
 
36
+ /**
37
+ * Clears the spinner
38
+ * @param {object} config
39
+ * @param {ora.Ora} config.spinner
40
+ * @returns {void}
41
+ */
20
42
  const clearSpinner = ({ spinner }) => {
21
43
  if (spinner) {
22
44
  spinner.stop()
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  const concordance = require('concordance')
2
3
 
3
4
  const { concordanceDiffOptions, concordanceOptions } = require('./options')
@@ -1,5 +1,7 @@
1
+ // @ts-check
1
2
  const ansiStyles = require('ansi-styles')
2
- const chalk = require('chalk')
3
+
4
+ const { chalk } = require('../../command-helpers')
3
5
 
4
6
  const forceColor = new chalk.Instance({ level: 1 })
5
7
 
@@ -1,6 +1,5 @@
1
- const chalk = require('chalk')
2
-
3
- const { error, exit, log, warn } = require('../command-helpers')
1
+ // @ts-check
2
+ const { chalk, error, exit, log, warn } = require('../command-helpers')
4
3
 
5
4
  const ADDON_VALIDATION = {
6
5
  EXISTS: 'EXISTS',
@@ -81,17 +80,25 @@ const getAddons = async ({ api, siteId }) => {
81
80
  return addons
82
81
  }
83
82
 
84
- const prepareAddonCommand = async ({ addonName, context, validation }) => {
85
- const { netlify } = context
83
+ /**
84
+ *
85
+ * @param {object} config
86
+ * @param {import('../../commands/base-command').BaseCommand} config.command
87
+ * @param {string} [config.addonName]
88
+ * @param {keyof ADDON_VALIDATION} [config.validation]
89
+ */
90
+ const prepareAddonCommand = async ({ addonName, command, validation }) => {
91
+ const { netlify } = command
86
92
  const { api, site } = netlify
87
93
  const siteId = site.id
88
94
  if (!siteId) {
89
95
  error('No site id found, please run inside a site folder or `netlify link`')
90
96
  }
91
97
 
92
- await context.authenticate()
98
+ await command.authenticate()
93
99
 
94
100
  const [manifest, siteData, addons] = await Promise.all([
101
+ // TODO: check as `getAddonManifest` did not accept a parameter error
95
102
  addonName ? getAddonManifest({ api, addonName, error }) : Promise.resolve(),
96
103
  getSiteData({ api, siteId }),
97
104
  getAddons({ api, siteId }),
@@ -1,4 +1,5 @@
1
- const chalk = require('chalk')
1
+ // @ts-check
2
+ const { chalk } = require('../command-helpers')
2
3
 
3
4
  /* programmatically generate CLI prompts */
4
5
  module.exports = function generatePrompts(settings) {
@@ -1,5 +1,7 @@
1
+ // @ts-check
1
2
  const AsciiTable = require('ascii-table')
2
- const chalk = require('chalk')
3
+
4
+ const { chalk } = require('../command-helpers')
3
5
 
4
6
  const missingValues = function (values, manifest) {
5
7
  const display = values
@@ -1,33 +1,83 @@
1
+ // @ts-check
2
+ const os = require('os')
1
3
  const process = require('process')
2
4
  const { format, inspect } = require('util')
3
5
 
4
- const Errors = require('@oclif/errors')
5
- const argv = require('minimist')(process.argv.slice(2))
6
- const omit = require('omit.js').default
6
+ // eslint-disable-next-line local-rules/no-direct-chalk-import
7
+ const { Instance: ChalkInstance } = require('chalk')
8
+ const WSL = require('is-wsl')
9
+ const { default: omit } = require('omit.js')
7
10
 
11
+ const { name, version } = require('../../package.json')
8
12
  const { clearSpinner, startSpinner } = require('../lib/spinner')
9
13
 
10
- const chalkInstance = require('./chalk')
11
14
  const getGlobalConfig = require('./get-global-config')
12
15
 
16
+ /** The parsed process argv without the binary only arguments and flags */
17
+ const argv = process.argv.slice(2)
18
+ /**
19
+ * Chalk instance for CLI that can be initialized with no colors mode
20
+ * needed for json outputs where we don't want to have colors
21
+ * @param {boolean} noColors - disable chalk colors
22
+ * @return {object} - default or custom chalk instance
23
+ */
24
+ const safeChalk = function (noColors) {
25
+ if (noColors) {
26
+ const colorlessChalk = new ChalkInstance({ level: 0 })
27
+ return colorlessChalk
28
+ }
29
+ return new ChalkInstance()
30
+ }
31
+
32
+ const chalk = safeChalk(argv.includes('--json'))
33
+
34
+ /**
35
+ * Adds the filler to the start of the string
36
+ * @param {string} str
37
+ * @param {number} count
38
+ * @param {string} [filler]
39
+ * @returns {string}
40
+ */
41
+ const padLeft = (str, count, filler = ' ') => str.padStart(str.length + count, filler)
42
+
43
+ const platform = WSL ? 'wsl' : os.platform()
44
+ const arch = os.arch() === 'ia32' ? 'x86' : os.arch()
45
+
46
+ const USER_AGENT = `${name}/${version} ${platform}-${arch} node-${process.version}`
47
+
13
48
  const { NETLIFY_AUTH_TOKEN } = process.env
14
49
 
15
- // 5 Minutes
16
- const TOKEN_TIMEOUT = 3e5
50
+ // eslint-disable-next-line no-magic-numbers
51
+ const NETLIFY_CYAN = chalk.rgb(40, 180, 170)
17
52
 
18
- const chalk = chalkInstance(argv.json)
53
+ const NETLIFYDEV = `${chalk.greenBright('◈')} ${NETLIFY_CYAN('Netlify Dev')} ${chalk.greenBright('◈')}`
54
+ const NETLIFYDEVLOG = `${chalk.greenBright('◈')}`
55
+ const NETLIFYDEVWARN = `${chalk.yellowBright('◈')}`
56
+ const NETLIFYDEVERR = `${chalk.redBright('◈')}`
19
57
 
20
- const pollForToken = async ({ api, exitWithError, ticket }) => {
58
+ const BANG = process.platform === 'win32' ? '»' : '›'
59
+
60
+ // Poll Token timeout 5 Minutes
61
+ const TOKEN_TIMEOUT = 3e5
62
+
63
+ /**
64
+ *
65
+ * @param {object} config
66
+ * @param {import('netlify').NetlifyAPI} config.api
67
+ * @param {object} config.ticket
68
+ * @returns
69
+ */
70
+ const pollForToken = async ({ api, ticket }) => {
21
71
  const spinner = startSpinner({ text: 'Waiting for authorization...' })
22
72
  try {
23
73
  const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
24
74
  if (!accessToken) {
25
- exitWithError('Could not retrieve access token')
75
+ error('Could not retrieve access token')
26
76
  }
27
77
  return accessToken
28
- } catch (error) {
29
- if (error.name === 'TimeoutError') {
30
- exitWithError(
78
+ } catch (error_) {
79
+ if (error_.name === 'TimeoutError') {
80
+ error(
31
81
  `Timed out waiting for authorization. If you do not have a ${chalk.bold.greenBright(
32
82
  'Netlify',
33
83
  )} account, please create one at ${chalk.magenta(
@@ -35,29 +85,22 @@ const pollForToken = async ({ api, exitWithError, ticket }) => {
35
85
  )}, then run ${chalk.cyanBright('netlify login')} again.`,
36
86
  )
37
87
  } else {
38
- exitWithError(error)
88
+ error(error_)
39
89
  }
40
90
  } finally {
41
91
  clearSpinner({ spinner })
42
92
  }
43
93
  }
44
94
 
45
- const getCwd = () => argv.cwd || process.cwd()
46
-
47
- const getAuthArg = () => {
48
- // If deploy command. Support shorthand 'a' flag
49
- if (argv && argv._ && argv._[0] === 'deploy') {
50
- return argv.auth || argv.a
51
- }
52
- return argv.auth
53
- }
54
-
55
- const getToken = async (authFromFlag) => {
56
- const tokenFromFlag = authFromFlag || getAuthArg(argv)
57
-
95
+ /**
96
+ * Get a netlify token
97
+ * @param {string} [tokenFromOptions] optional token from the provided --auth options
98
+ * @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
99
+ */
100
+ const getToken = async (tokenFromOptions) => {
58
101
  // 1. First honor command flag --auth
59
- if (tokenFromFlag) {
60
- return [tokenFromFlag, 'flag']
102
+ if (tokenFromOptions) {
103
+ return [tokenFromOptions, 'flag']
61
104
  }
62
105
  // 2. then Check ENV var
63
106
  if (NETLIFY_AUTH_TOKEN && NETLIFY_AUTH_TOKEN !== 'null') {
@@ -75,33 +118,58 @@ const getToken = async (authFromFlag) => {
75
118
 
76
119
  // 'api' command uses JSON output by default
77
120
  // 'functions:invoke' need to return the data from the function as is
78
- const isDefaultJson = () => argv._[0] === 'functions:invoke' || (argv._[0] === 'api' && argv.list !== true)
121
+ const isDefaultJson = () => argv[0] === 'functions:invoke' || (argv[0] === 'api' && !argv.includes('--list'))
79
122
 
123
+ /**
124
+ * logs a json message
125
+ * @param {string|object} message
126
+ */
80
127
  const logJson = (message = '') => {
81
- if (argv.json || isDefaultJson()) {
128
+ if (argv.includes('--json') || isDefaultJson()) {
82
129
  process.stdout.write(JSON.stringify(message, null, 2))
83
130
  }
84
131
  }
85
132
 
86
133
  const log = (message = '', ...args) => {
87
- /* If --silent or --json flag passed disable logger */
88
- if (argv.silent || argv.json || isDefaultJson()) {
134
+ // If --silent or --json flag passed disable logger
135
+ if (argv.includes('--json') || argv.includes('--silent') || isDefaultJson()) {
89
136
  return
90
137
  }
91
138
  message = typeof message === 'string' ? message : inspect(message)
92
139
  process.stdout.write(`${format(message, ...args)}\n`)
93
140
  }
94
141
 
142
+ /**
143
+ * logs a warning message
144
+ * @param {string} message
145
+ */
95
146
  const warn = (message = '') => {
96
- Errors.warn(message)
147
+ const bang = chalk.yellow(BANG)
148
+ log(` ${bang} Warning: ${message}`)
97
149
  }
98
150
 
151
+ /**
152
+ * throws an error or log it
153
+ * @param {string|Error} message
154
+ * @param {object} [options]
155
+ * @param {boolean} [options.exit]
156
+ */
99
157
  const error = (message = '', options = {}) => {
100
- Errors.error(message, options)
158
+ const err = message instanceof Error ? message : new Error(message)
159
+ if (options.exit === false) {
160
+ const bang = chalk.red(BANG)
161
+ if (process.env.DEBUG) {
162
+ process.stderr.write(` ${bang} Warning: ${err.stack.split('\n').join(`\n ${bang} `)}`)
163
+ } else {
164
+ process.stderr.write(` ${bang} ${err.name}: ${err.message}\n`)
165
+ }
166
+ } else {
167
+ throw err
168
+ }
101
169
  }
102
170
 
103
171
  const exit = (code = 0) => {
104
- Errors.exit(code)
172
+ process.exit(code)
105
173
  }
106
174
 
107
175
  // When `build.publish` is not set by the user, the CLI behavior differs in
@@ -114,15 +182,20 @@ const normalizeConfig = (config) =>
114
182
  : config
115
183
 
116
184
  module.exports = {
117
- argv,
118
- getCwd,
119
- pollForToken,
120
- log,
121
- logJson,
122
185
  getToken,
123
- normalizeConfig,
124
- chalk,
186
+ exit,
187
+ padLeft,
188
+ logJson,
189
+ log,
125
190
  warn,
126
191
  error,
127
- exit,
192
+ chalk,
193
+ pollForToken,
194
+ normalizeConfig,
195
+ USER_AGENT,
196
+ NETLIFY_CYAN,
197
+ NETLIFYDEV,
198
+ NETLIFYDEVLOG,
199
+ NETLIFYDEVWARN,
200
+ NETLIFYDEVERR,
128
201
  }
@@ -1,5 +1,10 @@
1
1
  const { Buffer } = require('buffer')
2
2
 
3
+ const SEC_TO_MILLISEC = 1e3
4
+
5
+ // 6 MiB
6
+ const DEFAULT_BYTES_LIMIT = 6e6
7
+
3
8
  const createStreamPromise = function (stream, timeoutSeconds, bytesLimit = DEFAULT_BYTES_LIMIT) {
4
9
  return new Promise(function streamPromiseFunc(resolve, reject) {
5
10
  let data = []
@@ -41,9 +46,4 @@ const createStreamPromise = function (stream, timeoutSeconds, bytesLimit = DEFAU
41
46
  })
42
47
  }
43
48
 
44
- const SEC_TO_MILLISEC = 1e3
45
-
46
- // 6 MiB
47
- const DEFAULT_BYTES_LIMIT = 6e6
48
-
49
49
  module.exports = { createStreamPromise }
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  const createDeferred = () => {
2
3
  let resolveDeferred
3
4
  let rejectDeferred
@@ -83,7 +83,7 @@ const deploySite = async (
83
83
 
84
84
  if (functionsWithNativeModules.length !== 0) {
85
85
  warn(`Modules with native dependencies\n
86
- ${functionsWithNativeModules.map(({ name }) => `- ${name}`).join('\n')}
86
+ ${functionsWithNativeModules.map(({ name }) => `- ${name}`).join('\n')}
87
87
 
88
88
  The serverless functions above use Node.js modules with native dependencies, which
89
89
  must be installed on a system with the same architecture as the function runtime. A
@@ -0,0 +1,4 @@
1
+ // @ts-check
2
+ const { deploySite } = require('./deploy-site')
3
+
4
+ module.exports = { deploySite }
@@ -4,17 +4,15 @@ const path = require('path')
4
4
  const process = require('process')
5
5
 
6
6
  const { getFramework, listFrameworks } = require('@netlify/framework-info')
7
- const chalk = require('chalk')
8
7
  const fuzzy = require('fuzzy')
9
8
  const getPort = require('get-port')
10
9
  const isPlainObject = require('is-plain-obj')
11
10
 
12
11
  const { readFileAsyncCatchError } = require('../lib/fs')
13
12
 
14
- const { log } = require('./command-helpers')
13
+ const { NETLIFYDEVWARN, chalk, log } = require('./command-helpers')
15
14
  const { acquirePort } = require('./dev')
16
15
  const { getInternalFunctionsDir } = require('./functions')
17
- const { NETLIFYDEVWARN } = require('./logo')
18
16
 
19
17
  const formatProperty = (str) => chalk.magenta(`'${str}'`)
20
18
  const formatValue = (str) => chalk.green(`'${str}'`)
@@ -106,14 +104,14 @@ const getDefaultDist = () => {
106
104
  *
107
105
  * @param {object} param0
108
106
  * @param {import('../commands/dev/types').DevConfig} param0.devConfig
109
- * @param {Record<string, string>} param0.flags
107
+ * @param {import('commander').OptionValues} param0.options
110
108
  * @param {string} param0.projectDir
111
109
  * @returns {Promise<import('./types').BaseServerSettings>}
112
110
  */
113
- const handleStaticServer = async ({ devConfig, flags, projectDir }) => {
111
+ const handleStaticServer = async ({ devConfig, options, projectDir }) => {
114
112
  validateNumberProperty({ devConfig, property: 'staticServerPort' })
115
113
 
116
- if (flags.dir) {
114
+ if (options.dir) {
117
115
  log(`${NETLIFYDEVWARN} Using simple static server because ${formatProperty('--dir')} flag was specified`)
118
116
  } else if (devConfig.framework === '#static') {
119
117
  log(
@@ -131,7 +129,7 @@ const handleStaticServer = async ({ devConfig, flags, projectDir }) => {
131
129
  )
132
130
  }
133
131
 
134
- const dist = flags.dir || devConfig.publish || getDefaultDist()
132
+ const dist = options.dir || devConfig.publish || getDefaultDist()
135
133
  log(`${NETLIFYDEVWARN} Running static server from "${path.relative(path.dirname(projectDir), dist)}"`)
136
134
 
137
135
  const frameworkPort = await acquirePort({
@@ -263,19 +261,19 @@ const handleForcedFramework = async ({ devConfig, projectDir }) => {
263
261
  /**
264
262
  * Get the server settings based on the flags and the devConfig
265
263
  * @param {import('../commands/dev/types').DevConfig} devConfig
266
- * @param {Record<string, string>} flags
264
+ * @param {import('commander').OptionValues} options
267
265
  * @param {string} projectDir
268
266
  * @returns {Promise<import('./types').ServerSettings>}
269
267
  */
270
- const detectServerSettings = async (devConfig, flags, projectDir) => {
268
+ const detectServerSettings = async (devConfig, options, projectDir) => {
271
269
  validateStringProperty({ devConfig, property: 'framework' })
272
270
 
273
271
  /** @type {Partial<import('./types').BaseServerSettings>} */
274
272
  let settings = {}
275
273
 
276
- if (flags.dir || devConfig.framework === '#static') {
274
+ if (options.dir || devConfig.framework === '#static') {
277
275
  // serving files statically without a framework server
278
- settings = await handleStaticServer({ flags, devConfig, projectDir })
276
+ settings = await handleStaticServer({ options, devConfig, projectDir })
279
277
  } else if (devConfig.framework === '#auto') {
280
278
  // this is the default CLI behavior
281
279
 
@@ -284,7 +282,7 @@ const detectServerSettings = async (devConfig, flags, projectDir) => {
284
282
 
285
283
  if (frameworkSettings === undefined && runDetection) {
286
284
  log(`${NETLIFYDEVWARN} No app server detected. Using simple static server`)
287
- settings = await handleStaticServer({ flags, devConfig, projectDir })
285
+ settings = await handleStaticServer({ options, devConfig, projectDir })
288
286
  } else {
289
287
  validateFrameworkConfig({ devConfig })
290
288
  const { command, frameworkPort, dist, framework, env, pollingStrategies = [] } = frameworkSettings || {}
package/src/utils/dev.js CHANGED
@@ -1,15 +1,14 @@
1
+ // @ts-check
1
2
  const process = require('process')
2
3
 
3
- const chalk = require('chalk')
4
4
  const { get } = require('dot-prop')
5
5
  const getPort = require('get-port')
6
6
  const isEmpty = require('lodash/isEmpty')
7
7
 
8
8
  const { supportsBackgroundFunctions } = require('../lib/account')
9
9
 
10
- const { error: failAndExit, log, warn } = require('./command-helpers')
10
+ const { NETLIFYDEVLOG, chalk, error, log, warn } = require('./command-helpers')
11
11
  const { loadDotEnvFiles } = require('./dot-env')
12
- const { NETLIFYDEVLOG } = require('./logo')
13
12
 
14
13
  // Possible sources of environment variables. For the purpose of printing log messages only. Order does not matter.
15
14
  const ENV_VAR_SOURCES = {
@@ -44,7 +43,7 @@ const ERROR_CALL_TO_ACTION =
44
43
 
45
44
  const validateSiteInfo = ({ site, siteInfo }) => {
46
45
  if (isEmpty(siteInfo)) {
47
- failAndExit(`Failed retrieving site information for site ${chalk.yellow(site.id)}. ${ERROR_CALL_TO_ACTION}`)
46
+ error(`Failed retrieving site information for site ${chalk.yellow(site.id)}. ${ERROR_CALL_TO_ACTION}`)
48
47
  }
49
48
  }
50
49
 
@@ -52,8 +51,8 @@ const getAccounts = async ({ api }) => {
52
51
  try {
53
52
  const accounts = await api.listAccountsForUser()
54
53
  return accounts
55
- } catch (error) {
56
- failAndExit(`Failed retrieving user account: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
54
+ } catch (error_) {
55
+ error(`Failed retrieving user account: ${error_.message}. ${ERROR_CALL_TO_ACTION}`)
57
56
  }
58
57
  }
59
58
 
@@ -61,8 +60,8 @@ const getAddons = async ({ api, site }) => {
61
60
  try {
62
61
  const addons = await api.listServiceInstancesForSite({ siteId: site.id })
63
62
  return addons
64
- } catch (error) {
65
- failAndExit(`Failed retrieving addons for site ${chalk.yellow(site.id)}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
63
+ } catch (error_) {
64
+ error(`Failed retrieving addons for site ${chalk.yellow(site.id)}: ${error_.message}. ${ERROR_CALL_TO_ACTION}`)
66
65
  }
67
66
  }
68
67
 
@@ -89,8 +88,17 @@ const SYNCHRONOUS_FUNCTION_TIMEOUT = 10
89
88
  // default 15 minutes for background functions
90
89
  const BACKGROUND_FUNCTION_TIMEOUT = 900
91
90
 
92
- const getSiteInformation = async ({ flags = {}, api, site, siteInfo }) => {
93
- if (site.id && !flags.offline) {
91
+ /**
92
+ *
93
+ * @param {object} config
94
+ * @param {boolean} config.offline
95
+ * @param {*} config.api
96
+ * @param {*} config.site
97
+ * @param {*} config.siteInfo
98
+ * @returns
99
+ */
100
+ const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
101
+ if (site.id && !offline) {
94
102
  validateSiteInfo({ site, siteInfo })
95
103
  const [accounts, addons] = await Promise.all([getAccounts({ api }), getAddons({ api, site })])
96
104
 
@@ -1,8 +1,10 @@
1
+ // @ts-check
2
+ const { readFile } = require('fs').promises
1
3
  const path = require('path')
2
4
 
3
5
  const dotenv = require('dotenv')
4
6
 
5
- const { isFileAsync, readFileAsync } = require('../lib/fs')
7
+ const { isFileAsync } = require('../lib/fs')
6
8
 
7
9
  const { warn } = require('./command-helpers')
8
10
 
@@ -32,7 +34,7 @@ const tryLoadDotEnvFiles = async ({ projectDir }) => {
32
34
  warning: `Failed reading env variables from file: ${filepath}: ${error.message}`,
33
35
  }
34
36
  }
35
- const content = await readFileAsync(filepath)
37
+ const content = await readFile(filepath, 'utf-8')
36
38
  const env = dotenv.parse(content)
37
39
  return { file, env }
38
40
  }),