@netlify/build 29.0.1-rc → 29.0.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 (266) hide show
  1. package/bin.js +5 -0
  2. package/lib/core/bin.js +66 -0
  3. package/lib/core/build.js +356 -0
  4. package/lib/core/config.js +121 -0
  5. package/lib/core/constants.js +116 -0
  6. package/lib/core/dev.js +27 -0
  7. package/lib/core/dry.js +21 -0
  8. package/lib/core/feature_flags.js +21 -0
  9. package/lib/core/flags.js +201 -0
  10. package/lib/core/lingering.js +68 -0
  11. package/lib/core/main.js +110 -0
  12. package/lib/core/missing_side_file.js +17 -0
  13. package/lib/core/normalize_flags.js +59 -0
  14. package/lib/core/severity.js +21 -0
  15. package/lib/core/types.js +8 -0
  16. package/lib/core/user_node_version.js +32 -0
  17. package/lib/env/changes.js +43 -0
  18. package/lib/env/main.js +14 -0
  19. package/lib/env/metadata.js +68 -0
  20. package/lib/error/api.js +37 -0
  21. package/lib/error/build.js +36 -0
  22. package/{src → lib}/error/cancel.js +5 -6
  23. package/lib/error/colors.js +9 -0
  24. package/lib/error/handle.js +46 -0
  25. package/lib/error/info.js +37 -0
  26. package/lib/error/monitor/location.js +16 -0
  27. package/lib/error/monitor/normalize.js +86 -0
  28. package/lib/error/monitor/print.js +20 -0
  29. package/lib/error/monitor/report.js +120 -0
  30. package/lib/error/monitor/start.js +61 -0
  31. package/lib/error/parse/clean_stack.js +70 -0
  32. package/lib/error/parse/location.js +50 -0
  33. package/lib/error/parse/normalize.js +24 -0
  34. package/lib/error/parse/parse.js +67 -0
  35. package/lib/error/parse/plugin.js +55 -0
  36. package/lib/error/parse/properties.js +16 -0
  37. package/lib/error/parse/serialize_log.js +34 -0
  38. package/lib/error/parse/serialize_status.js +18 -0
  39. package/lib/error/parse/stack.js +34 -0
  40. package/lib/error/report.js +27 -0
  41. package/lib/error/type.js +177 -0
  42. package/lib/install/functions.js +20 -0
  43. package/lib/install/local.js +45 -0
  44. package/lib/install/main.js +67 -0
  45. package/lib/install/missing.js +54 -0
  46. package/lib/log/colors.js +28 -0
  47. package/lib/log/description.js +21 -0
  48. package/lib/log/header.js +14 -0
  49. package/lib/log/header_func.js +13 -0
  50. package/lib/log/logger.js +140 -0
  51. package/lib/log/messages/compatibility.js +146 -0
  52. package/lib/log/messages/config.js +91 -0
  53. package/lib/log/messages/core.js +51 -0
  54. package/lib/log/messages/core_steps.js +75 -0
  55. package/lib/log/messages/dry.js +41 -0
  56. package/lib/log/messages/install.js +25 -0
  57. package/lib/log/messages/ipc.js +29 -0
  58. package/lib/log/messages/mutations.js +62 -0
  59. package/{src → lib}/log/messages/plugins.js +18 -32
  60. package/lib/log/messages/status.js +14 -0
  61. package/lib/log/messages/steps.js +18 -0
  62. package/lib/log/old_version.js +32 -0
  63. package/lib/log/serialize.js +10 -0
  64. package/lib/log/stream.js +68 -0
  65. package/lib/log/theme.js +27 -0
  66. package/lib/plugins/child/diff.js +46 -0
  67. package/lib/plugins/child/error.js +26 -0
  68. package/lib/plugins/child/lazy.js +15 -0
  69. package/lib/plugins/child/load.js +22 -0
  70. package/lib/plugins/child/logic.js +57 -0
  71. package/lib/plugins/child/main.js +37 -0
  72. package/lib/plugins/child/run.js +19 -0
  73. package/lib/plugins/child/status.js +63 -0
  74. package/lib/plugins/child/typescript.js +28 -0
  75. package/lib/plugins/child/utils.js +42 -0
  76. package/lib/plugins/child/validate.js +31 -0
  77. package/lib/plugins/compatibility.js +104 -0
  78. package/{src → lib}/plugins/error.js +31 -35
  79. package/{src → lib}/plugins/events.js +7 -12
  80. package/lib/plugins/expected_version.js +81 -0
  81. package/lib/plugins/internal.js +10 -0
  82. package/lib/plugins/ipc.js +120 -0
  83. package/lib/plugins/list.js +73 -0
  84. package/lib/plugins/load.js +50 -0
  85. package/lib/plugins/manifest/check.js +85 -0
  86. package/lib/plugins/manifest/load.js +38 -0
  87. package/lib/plugins/manifest/main.js +19 -0
  88. package/lib/plugins/manifest/path.js +24 -0
  89. package/lib/plugins/manifest/validate.js +91 -0
  90. package/lib/plugins/node_version.js +35 -0
  91. package/lib/plugins/options.js +70 -0
  92. package/lib/plugins/pinned_version.js +83 -0
  93. package/lib/plugins/resolve.js +110 -0
  94. package/lib/plugins/spawn.js +58 -0
  95. package/lib/plugins_core/add.js +35 -0
  96. package/lib/plugins_core/build_command.js +50 -0
  97. package/lib/plugins_core/deploy/buildbot_client.js +87 -0
  98. package/lib/plugins_core/deploy/index.js +49 -0
  99. package/{src → lib}/plugins_core/deploy/manifest.yml +0 -0
  100. package/lib/plugins_core/edge_functions/index.js +76 -0
  101. package/{src → lib}/plugins_core/edge_functions/lib/error.js +13 -17
  102. package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +21 -0
  103. package/lib/plugins_core/functions/error.js +123 -0
  104. package/lib/plugins_core/functions/feature_flags.js +6 -0
  105. package/lib/plugins_core/functions/index.js +114 -0
  106. package/lib/plugins_core/functions/utils.js +45 -0
  107. package/lib/plugins_core/functions/zisi.js +39 -0
  108. package/{src → lib}/plugins_core/functions_install/index.js +8 -11
  109. package/{src → lib}/plugins_core/functions_install/manifest.yml +0 -0
  110. package/lib/plugins_core/list.js +20 -0
  111. package/lib/report/statsd.js +31 -0
  112. package/lib/status/add.js +30 -0
  113. package/lib/status/colors.js +18 -0
  114. package/lib/status/load_error.js +10 -0
  115. package/lib/status/report.js +83 -0
  116. package/lib/status/success.js +14 -0
  117. package/lib/steps/core_step.js +59 -0
  118. package/lib/steps/error.js +65 -0
  119. package/lib/steps/get.js +43 -0
  120. package/lib/steps/plugin.js +55 -0
  121. package/lib/steps/return.js +25 -0
  122. package/lib/steps/run_core_steps.js +117 -0
  123. package/lib/steps/run_step.js +190 -0
  124. package/lib/steps/run_steps.js +96 -0
  125. package/lib/steps/update_config.js +66 -0
  126. package/lib/telemetry/main.js +97 -0
  127. package/lib/time/aggregate.js +109 -0
  128. package/lib/time/main.js +31 -0
  129. package/lib/time/measure.js +16 -0
  130. package/lib/time/report.js +24 -0
  131. package/lib/utils/errors.js +13 -0
  132. package/lib/utils/json.js +15 -0
  133. package/lib/utils/omit.js +3 -0
  134. package/lib/utils/package.js +24 -0
  135. package/lib/utils/remove_falsy.js +8 -0
  136. package/lib/utils/resolve.js +41 -0
  137. package/lib/utils/runtime.js +5 -0
  138. package/lib/utils/semver.js +28 -0
  139. package/package.json +41 -24
  140. package/types/config/netlify_config.d.ts +4 -4
  141. package/types/netlify_plugin_constants.d.ts +8 -8
  142. package/src/core/bin.js +0 -83
  143. package/src/core/build.js +0 -554
  144. package/src/core/config.js +0 -186
  145. package/src/core/constants.js +0 -156
  146. package/src/core/dev.js +0 -31
  147. package/src/core/dry.js +0 -39
  148. package/src/core/feature_flags.js +0 -22
  149. package/src/core/flags.js +0 -204
  150. package/src/core/lingering.js +0 -85
  151. package/src/core/main.js +0 -165
  152. package/src/core/missing_side_file.js +0 -29
  153. package/src/core/normalize_flags.js +0 -70
  154. package/src/core/severity.js +0 -22
  155. package/src/core/user_node_version.js +0 -41
  156. package/src/env/changes.js +0 -52
  157. package/src/env/main.js +0 -19
  158. package/src/env/metadata.js +0 -81
  159. package/src/error/api.js +0 -46
  160. package/src/error/build.js +0 -50
  161. package/src/error/colors.js +0 -11
  162. package/src/error/handle.js +0 -57
  163. package/src/error/info.js +0 -46
  164. package/src/error/monitor/location.js +0 -21
  165. package/src/error/monitor/normalize.js +0 -96
  166. package/src/error/monitor/print.js +0 -42
  167. package/src/error/monitor/report.js +0 -138
  168. package/src/error/monitor/start.js +0 -69
  169. package/src/error/parse/clean_stack.js +0 -87
  170. package/src/error/parse/location.js +0 -62
  171. package/src/error/parse/normalize.js +0 -29
  172. package/src/error/parse/parse.js +0 -97
  173. package/src/error/parse/plugin.js +0 -70
  174. package/src/error/parse/properties.js +0 -23
  175. package/src/error/parse/serialize_log.js +0 -42
  176. package/src/error/parse/serialize_status.js +0 -23
  177. package/src/error/parse/stack.js +0 -43
  178. package/src/error/type.js +0 -189
  179. package/src/install/functions.js +0 -28
  180. package/src/install/local.js +0 -62
  181. package/src/install/main.js +0 -81
  182. package/src/install/missing.js +0 -67
  183. package/src/log/colors.js +0 -34
  184. package/src/log/description.js +0 -26
  185. package/src/log/header.js +0 -16
  186. package/src/log/header_func.js +0 -17
  187. package/src/log/logger.js +0 -161
  188. package/src/log/messages/compatibility.js +0 -164
  189. package/src/log/messages/config.js +0 -107
  190. package/src/log/messages/core.js +0 -70
  191. package/src/log/messages/core_steps.js +0 -104
  192. package/src/log/messages/dry.js +0 -63
  193. package/src/log/messages/install.js +0 -20
  194. package/src/log/messages/ipc.js +0 -38
  195. package/src/log/messages/mutations.js +0 -82
  196. package/src/log/messages/status.js +0 -16
  197. package/src/log/messages/steps.js +0 -22
  198. package/src/log/old_version.js +0 -41
  199. package/src/log/serialize.js +0 -13
  200. package/src/log/stream.js +0 -85
  201. package/src/log/theme.js +0 -26
  202. package/src/plugins/child/diff.js +0 -55
  203. package/src/plugins/child/error.js +0 -32
  204. package/src/plugins/child/lazy.js +0 -18
  205. package/src/plugins/child/load.js +0 -29
  206. package/src/plugins/child/logic.js +0 -57
  207. package/src/plugins/child/main.js +0 -51
  208. package/src/plugins/child/run.js +0 -28
  209. package/src/plugins/child/status.js +0 -74
  210. package/src/plugins/child/typescript.js +0 -45
  211. package/src/plugins/child/utils.js +0 -56
  212. package/src/plugins/child/validate.js +0 -34
  213. package/src/plugins/compatibility.js +0 -128
  214. package/src/plugins/expected_version.js +0 -119
  215. package/src/plugins/ipc.js +0 -145
  216. package/src/plugins/list.js +0 -86
  217. package/src/plugins/load.js +0 -70
  218. package/src/plugins/manifest/check.js +0 -106
  219. package/src/plugins/manifest/load.js +0 -41
  220. package/src/plugins/manifest/main.js +0 -22
  221. package/src/plugins/manifest/path.js +0 -31
  222. package/src/plugins/manifest/validate.js +0 -108
  223. package/src/plugins/node_version.js +0 -50
  224. package/src/plugins/options.js +0 -88
  225. package/src/plugins/pinned_version.js +0 -131
  226. package/src/plugins/resolve.js +0 -152
  227. package/src/plugins/spawn.js +0 -66
  228. package/src/plugins_core/add.js +0 -49
  229. package/src/plugins_core/build_command.js +0 -75
  230. package/src/plugins_core/deploy/buildbot_client.js +0 -102
  231. package/src/plugins_core/deploy/index.js +0 -73
  232. package/src/plugins_core/edge_functions/index.js +0 -123
  233. package/src/plugins_core/edge_functions/lib/internal_manifest.js +0 -54
  234. package/src/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +0 -89
  235. package/src/plugins_core/functions/error.js +0 -163
  236. package/src/plugins_core/functions/feature_flags.js +0 -6
  237. package/src/plugins_core/functions/index.js +0 -160
  238. package/src/plugins_core/functions/utils.js +0 -66
  239. package/src/plugins_core/functions/zisi.js +0 -53
  240. package/src/plugins_core/list.js +0 -27
  241. package/src/status/add.js +0 -36
  242. package/src/status/colors.js +0 -23
  243. package/src/status/load_error.js +0 -11
  244. package/src/status/report.js +0 -137
  245. package/src/status/success.js +0 -18
  246. package/src/steps/core_step.js +0 -92
  247. package/src/steps/error.js +0 -102
  248. package/src/steps/get.js +0 -51
  249. package/src/steps/plugin.js +0 -85
  250. package/src/steps/return.js +0 -52
  251. package/src/steps/run_core_steps.js +0 -200
  252. package/src/steps/run_step.js +0 -304
  253. package/src/steps/run_steps.js +0 -179
  254. package/src/steps/update_config.js +0 -93
  255. package/src/telemetry/main.js +0 -136
  256. package/src/time/aggregate.js +0 -146
  257. package/src/time/main.js +0 -48
  258. package/src/time/measure.js +0 -22
  259. package/src/time/report.js +0 -59
  260. package/src/utils/errors.js +0 -12
  261. package/src/utils/json.js +0 -19
  262. package/src/utils/omit.js +0 -6
  263. package/src/utils/package.js +0 -23
  264. package/src/utils/remove_falsy.js +0 -10
  265. package/src/utils/resolve.js +0 -46
  266. package/src/utils/semver.js +0 -34
package/bin.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ // This is a workaround for npm issue: https://github.com/npm/cli/issues/2632
4
+
5
+ import './lib/core/bin.js'
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env node
2
+ import process from 'process';
3
+ import filterObj from 'filter-obj';
4
+ import yargs from 'yargs';
5
+ import { hideBin } from 'yargs/helpers';
6
+ import { normalizeCliFeatureFlags } from './feature_flags.js';
7
+ import { FLAGS } from './flags.js';
8
+ import build from './main.js';
9
+ import { FALLBACK_SEVERITY_ENTRY } from './severity.js';
10
+ // CLI entry point.
11
+ // Before adding logic to this file, please consider adding it to the main
12
+ // programmatic command instead, so that the new logic is available when run
13
+ // programmatically as well. This file should only contain logic that makes
14
+ // sense only in CLI, such as CLI flags parsing and exit code.
15
+ const runCli = async function () {
16
+ const flags = parseFlags();
17
+ const flagsA = filterObj(flags, isUserFlag);
18
+ const state = { done: false };
19
+ process.on('exit', onExit.bind(undefined, state));
20
+ const { severityCode, logs } = await build(flagsA);
21
+ printLogs(logs);
22
+ process.exitCode = severityCode;
23
+ state.done = true;
24
+ };
25
+ const parseFlags = function () {
26
+ const { featureFlags: cliFeatureFlags = '', ...flags } = yargs(hideBin(process.argv))
27
+ .options(FLAGS)
28
+ .usage(USAGE)
29
+ .parse();
30
+ const featureFlags = normalizeCliFeatureFlags(cliFeatureFlags);
31
+ return { ...flags, featureFlags };
32
+ };
33
+ const USAGE = `netlify-build [OPTIONS...]
34
+
35
+ Run Netlify Build system locally.
36
+
37
+ Options can also be specified as environment variables prefixed with
38
+ NETLIFY_BUILD_. For example the environment variable NETLIFY_BUILD_DRY=true can
39
+ be used instead of the CLI flag --dry.`;
40
+ // Remove `yargs`-specific options, shortcuts, dash-cased and aliases
41
+ const isUserFlag = function (key, value) {
42
+ return value !== undefined && !INTERNAL_KEYS.has(key) && key.length !== 1 && !key.includes('-');
43
+ };
44
+ const INTERNAL_KEYS = new Set(['help', 'version', '_', '$0', 'dryRun']);
45
+ // Used mostly for testing
46
+ const printLogs = function (logs) {
47
+ if (logs === undefined) {
48
+ return;
49
+ }
50
+ const allLogs = [logs.stdout.join('\n'), logs.stderr.join('\n')].filter(Boolean).join('\n\n');
51
+ console.log(allLogs);
52
+ };
53
+ // In theory, the main process should not exit until the main `build()` function
54
+ // has completed. In practice, this might happen due to bugs.
55
+ // Making the exit code not 0 in that case ensures the caller knows that the
56
+ // build has completed when the exit code is 0. This `exit` event handlers
57
+ // guarantees this.
58
+ const onExit = function ({ done }, exitCode) {
59
+ if (done || exitCode !== 0) {
60
+ return;
61
+ }
62
+ const [, processName] = process.argv;
63
+ console.log(`${processName} exited with exit code ${exitCode} without finishing the build.`);
64
+ process.exitCode = FALLBACK_SEVERITY_ENTRY.severityCode;
65
+ };
66
+ runCli();
@@ -0,0 +1,356 @@
1
+ import { getErrorInfo } from '../error/info.js';
2
+ import { startErrorMonitor } from '../error/monitor/start.js';
3
+ import { getBufferLogs, getSystemLogger } from '../log/logger.js';
4
+ import { logBuildStart } from '../log/messages/core.js';
5
+ import { loadPlugins } from '../plugins/load.js';
6
+ import { getPluginsOptions } from '../plugins/options.js';
7
+ import { pinPlugins } from '../plugins/pinned_version.js';
8
+ import { startPlugins, stopPlugins } from '../plugins/spawn.js';
9
+ import { addCorePlugins } from '../plugins_core/add.js';
10
+ import { reportStatuses } from '../status/report.js';
11
+ import { getDevSteps, getSteps } from '../steps/get.js';
12
+ import { runSteps } from '../steps/run_steps.js';
13
+ import { initTimers, measureDuration } from '../time/main.js';
14
+ import { getConfigOpts, loadConfig } from './config.js';
15
+ import { getConstants } from './constants.js';
16
+ import { doDryRun } from './dry.js';
17
+ import { warnOnLingeringProcesses } from './lingering.js';
18
+ import { warnOnMissingSideFiles } from './missing_side_file.js';
19
+ import { normalizeFlags } from './normalize_flags.js';
20
+ // Performed on build start. Must be kept small and unlikely to fail since it
21
+ // does not have proper error handling. Error handling relies on `errorMonitor`
22
+ // being built, which relies itself on flags being normalized.
23
+ export const startBuild = function (flags) {
24
+ const timers = initTimers();
25
+ const logs = getBufferLogs(flags);
26
+ logBuildStart(logs);
27
+ const { bugsnagKey, ...flagsA } = normalizeFlags(flags, logs);
28
+ const errorMonitor = startErrorMonitor({ flags: flagsA, logs, bugsnagKey });
29
+ return { ...flagsA, errorMonitor, logs, timers };
30
+ };
31
+ const tExecBuild = async function ({ config, defaultConfig, cachedConfig, cachedConfigPath, cwd, repositoryRoot, apiHost, token, siteId, context, branch, baseRelDir, env: envOpt, debug, systemLogFile, verbose, nodePath, functionsDistDir, edgeFunctionsDistDir, cacheDir, dry, mode, offline, deployId, buildId, testOpts, errorMonitor, errorParams, logs, timers, buildbotServerSocket, sendStatus, saveConfig, featureFlags, timeline, devCommand, }) {
32
+ const configOpts = getConfigOpts({
33
+ config,
34
+ defaultConfig,
35
+ cwd,
36
+ repositoryRoot,
37
+ apiHost,
38
+ token,
39
+ siteId,
40
+ context,
41
+ branch,
42
+ baseRelDir,
43
+ envOpt,
44
+ mode,
45
+ offline,
46
+ deployId,
47
+ buildId,
48
+ testOpts,
49
+ featureFlags,
50
+ });
51
+ const { netlifyConfig, configPath, headersPath, redirectsPath, buildDir, repositoryRoot: repositoryRootA, packageJson, userNodeVersion, childEnv, context: contextA, branch: branchA, token: tokenA, api, siteInfo, timers: timersA, } = await loadConfig({
52
+ configOpts,
53
+ cachedConfig,
54
+ cachedConfigPath,
55
+ envOpt,
56
+ debug,
57
+ logs,
58
+ nodePath,
59
+ timers,
60
+ });
61
+ const constants = await getConstants({
62
+ configPath,
63
+ buildDir,
64
+ functionsDistDir,
65
+ edgeFunctionsDistDir,
66
+ cacheDir,
67
+ netlifyConfig,
68
+ siteInfo,
69
+ apiHost,
70
+ token: tokenA,
71
+ mode,
72
+ testOpts,
73
+ });
74
+ const systemLog = getSystemLogger(logs, debug, systemLogFile);
75
+ const pluginsOptions = addCorePlugins({ netlifyConfig, constants });
76
+ // `errorParams` is purposely stateful
77
+ Object.assign(errorParams, { netlifyConfig, pluginsOptions, siteInfo, childEnv, userNodeVersion });
78
+ const { pluginsOptions: pluginsOptionsA, netlifyConfig: netlifyConfigA, stepsCount, timers: timersB, configMutations, } = await runAndReportBuild({
79
+ pluginsOptions,
80
+ netlifyConfig,
81
+ configOpts,
82
+ siteInfo,
83
+ configPath,
84
+ headersPath,
85
+ redirectsPath,
86
+ buildDir,
87
+ repositoryRoot: repositoryRootA,
88
+ nodePath,
89
+ packageJson,
90
+ userNodeVersion,
91
+ childEnv,
92
+ context: contextA,
93
+ branch: branchA,
94
+ dry,
95
+ mode,
96
+ api,
97
+ errorMonitor,
98
+ deployId,
99
+ errorParams,
100
+ logs,
101
+ debug,
102
+ systemLog,
103
+ verbose,
104
+ timers: timersA,
105
+ sendStatus,
106
+ saveConfig,
107
+ testOpts,
108
+ buildbotServerSocket,
109
+ constants,
110
+ featureFlags,
111
+ timeline,
112
+ devCommand,
113
+ });
114
+ return {
115
+ pluginsOptions: pluginsOptionsA,
116
+ netlifyConfig: netlifyConfigA,
117
+ siteInfo,
118
+ userNodeVersion,
119
+ stepsCount,
120
+ timers: timersB,
121
+ configMutations,
122
+ };
123
+ };
124
+ export const execBuild = measureDuration(tExecBuild, 'total', { parentTag: 'build_site' });
125
+ // Runs a build then report any plugin statuses
126
+ export const runAndReportBuild = async function ({ pluginsOptions, netlifyConfig, configOpts, siteInfo, configPath, headersPath, redirectsPath, buildDir, repositoryRoot, nodePath, packageJson, userNodeVersion, childEnv, context, branch, buildbotServerSocket, constants, dry, mode, api, errorMonitor, deployId, errorParams, logs, debug, systemLog, verbose, timers, sendStatus, saveConfig, testOpts, featureFlags, timeline, devCommand, }) {
127
+ try {
128
+ const { stepsCount, netlifyConfig: netlifyConfigA, statuses, pluginsOptions: pluginsOptionsA, failedPlugins, timers: timersA, configMutations, } = await initAndRunBuild({
129
+ pluginsOptions,
130
+ netlifyConfig,
131
+ configOpts,
132
+ siteInfo,
133
+ configPath,
134
+ headersPath,
135
+ redirectsPath,
136
+ buildDir,
137
+ repositoryRoot,
138
+ nodePath,
139
+ packageJson,
140
+ userNodeVersion,
141
+ childEnv,
142
+ context,
143
+ branch,
144
+ dry,
145
+ mode,
146
+ api,
147
+ errorMonitor,
148
+ deployId,
149
+ errorParams,
150
+ logs,
151
+ debug,
152
+ systemLog,
153
+ verbose,
154
+ timers,
155
+ sendStatus,
156
+ saveConfig,
157
+ testOpts,
158
+ buildbotServerSocket,
159
+ constants,
160
+ featureFlags,
161
+ timeline,
162
+ devCommand,
163
+ });
164
+ await Promise.all([
165
+ reportStatuses({
166
+ statuses,
167
+ childEnv,
168
+ api,
169
+ mode,
170
+ pluginsOptions: pluginsOptionsA,
171
+ netlifyConfig: netlifyConfigA,
172
+ errorMonitor,
173
+ deployId,
174
+ logs,
175
+ debug,
176
+ sendStatus,
177
+ testOpts,
178
+ }),
179
+ pinPlugins({
180
+ pluginsOptions: pluginsOptionsA,
181
+ failedPlugins,
182
+ api,
183
+ siteInfo,
184
+ childEnv,
185
+ mode,
186
+ netlifyConfig: netlifyConfigA,
187
+ errorMonitor,
188
+ logs,
189
+ debug,
190
+ testOpts,
191
+ sendStatus,
192
+ }),
193
+ ]);
194
+ return {
195
+ pluginsOptions: pluginsOptionsA,
196
+ netlifyConfig: netlifyConfigA,
197
+ stepsCount,
198
+ timers: timersA,
199
+ configMutations,
200
+ };
201
+ }
202
+ catch (error) {
203
+ const [{ statuses }] = getErrorInfo(error);
204
+ await reportStatuses({
205
+ statuses,
206
+ childEnv,
207
+ api,
208
+ mode,
209
+ pluginsOptions,
210
+ netlifyConfig,
211
+ errorMonitor,
212
+ deployId,
213
+ logs,
214
+ debug,
215
+ sendStatus,
216
+ testOpts,
217
+ });
218
+ throw error;
219
+ }
220
+ };
221
+ // Initialize plugin processes then runs a build
222
+ const initAndRunBuild = async function ({ pluginsOptions, netlifyConfig, configOpts, siteInfo, configPath, headersPath, redirectsPath, buildDir, repositoryRoot, nodePath, packageJson, userNodeVersion, childEnv, context, branch, dry, mode, api, errorMonitor, deployId, errorParams, logs, debug, systemLog, verbose, sendStatus, saveConfig, timers, testOpts, buildbotServerSocket, constants, featureFlags, timeline, devCommand, }) {
223
+ const { pluginsOptions: pluginsOptionsA, timers: timersA } = await getPluginsOptions({
224
+ pluginsOptions,
225
+ netlifyConfig,
226
+ siteInfo,
227
+ buildDir,
228
+ nodePath,
229
+ packageJson,
230
+ userNodeVersion,
231
+ mode,
232
+ api,
233
+ logs,
234
+ debug,
235
+ sendStatus,
236
+ timers,
237
+ testOpts,
238
+ featureFlags,
239
+ });
240
+ errorParams.pluginsOptions = pluginsOptionsA;
241
+ const { childProcesses, timers: timersB } = await startPlugins({
242
+ pluginsOptions: pluginsOptionsA,
243
+ buildDir,
244
+ childEnv,
245
+ logs,
246
+ debug,
247
+ timers: timersA,
248
+ featureFlags,
249
+ });
250
+ try {
251
+ const { stepsCount, netlifyConfig: netlifyConfigA, statuses, failedPlugins, timers: timersC, configMutations, } = await runBuild({
252
+ childProcesses,
253
+ pluginsOptions: pluginsOptionsA,
254
+ netlifyConfig,
255
+ configOpts,
256
+ packageJson,
257
+ configPath,
258
+ headersPath,
259
+ redirectsPath,
260
+ buildDir,
261
+ repositoryRoot,
262
+ nodePath,
263
+ childEnv,
264
+ context,
265
+ branch,
266
+ dry,
267
+ buildbotServerSocket,
268
+ constants,
269
+ mode,
270
+ api,
271
+ errorMonitor,
272
+ deployId,
273
+ errorParams,
274
+ logs,
275
+ debug,
276
+ systemLog,
277
+ verbose,
278
+ saveConfig,
279
+ timers: timersB,
280
+ testOpts,
281
+ featureFlags,
282
+ timeline,
283
+ devCommand,
284
+ });
285
+ await Promise.all([
286
+ warnOnMissingSideFiles({ buildDir, netlifyConfig: netlifyConfigA, logs }),
287
+ warnOnLingeringProcesses({ mode, logs, testOpts }),
288
+ ]);
289
+ return {
290
+ stepsCount,
291
+ netlifyConfig: netlifyConfigA,
292
+ statuses,
293
+ pluginsOptions: pluginsOptionsA,
294
+ failedPlugins,
295
+ timers: timersC,
296
+ configMutations,
297
+ };
298
+ }
299
+ finally {
300
+ // Terminate the child processes of plugins so that they don't linger after
301
+ // the build is finished. The exception is when running in the dev timeline
302
+ // since those are long-running events by nature.
303
+ if (timeline !== 'dev') {
304
+ stopPlugins(childProcesses);
305
+ }
306
+ }
307
+ };
308
+ // Load plugin main files, retrieve their event handlers then runs them,
309
+ // together with the build command
310
+ const runBuild = async function ({ childProcesses, pluginsOptions, netlifyConfig, configOpts, packageJson, configPath, headersPath, redirectsPath, buildDir, repositoryRoot, nodePath, childEnv, context, branch, dry, buildbotServerSocket, constants, mode, api, errorMonitor, deployId, errorParams, logs, debug, systemLog, verbose, saveConfig, timers, testOpts, featureFlags, timeline, devCommand, }) {
311
+ const { pluginsSteps, timers: timersA } = await loadPlugins({
312
+ pluginsOptions,
313
+ childProcesses,
314
+ packageJson,
315
+ timers,
316
+ logs,
317
+ debug,
318
+ verbose,
319
+ });
320
+ const { steps, events } = timeline === 'dev' ? getDevSteps(devCommand, pluginsSteps) : getSteps(pluginsSteps);
321
+ if (dry) {
322
+ await doDryRun({ buildDir, steps, netlifyConfig, constants, buildbotServerSocket, logs });
323
+ return { netlifyConfig };
324
+ }
325
+ const { stepsCount, netlifyConfig: netlifyConfigA, statuses, failedPlugins, timers: timersB, configMutations, } = await runSteps({
326
+ steps,
327
+ buildbotServerSocket,
328
+ events,
329
+ configPath,
330
+ headersPath,
331
+ redirectsPath,
332
+ buildDir,
333
+ repositoryRoot,
334
+ nodePath,
335
+ childEnv,
336
+ context,
337
+ branch,
338
+ constants,
339
+ mode,
340
+ api,
341
+ errorMonitor,
342
+ deployId,
343
+ errorParams,
344
+ netlifyConfig,
345
+ configOpts,
346
+ logs,
347
+ debug,
348
+ systemLog,
349
+ verbose,
350
+ saveConfig,
351
+ timers: timersA,
352
+ testOpts,
353
+ featureFlags,
354
+ });
355
+ return { stepsCount, netlifyConfig: netlifyConfigA, statuses, failedPlugins, timers: timersB, configMutations };
356
+ };
@@ -0,0 +1,121 @@
1
+ import { resolveConfig, updateConfig, restoreConfig } from '@netlify/config';
2
+ import mapObj from 'map-obj';
3
+ import { getChildEnv } from '../env/main.js';
4
+ import { addApiErrorHandlers } from '../error/api.js';
5
+ import { changeErrorType } from '../error/info.js';
6
+ import { logBuildDir, logConfigPath, logConfig, logContext } from '../log/messages/config.js';
7
+ import { logConfigOnUpload, logHeadersOnUpload, logRedirectsOnUpload } from '../log/messages/mutations.js';
8
+ import { measureDuration } from '../time/main.js';
9
+ import { getPackageJson } from '../utils/package.js';
10
+ import { getUserNodeVersion } from './user_node_version.js';
11
+ // Retrieve immutable options passed to `@netlify/config`.
12
+ // This does not include options which might change during the course of the
13
+ // build:
14
+ // - `cachedConfig` and `cachedConfigPath` are only used at the beginning of
15
+ // the build
16
+ // - If plugins change the configuration, `configMutations` is used instead
17
+ // In both cases, almost all options should remain the same.
18
+ export const getConfigOpts = function ({ config, defaultConfig, cwd, repositoryRoot, apiHost, token, siteId, context, branch, baseRelDir, envOpt, mode, offline, deployId, buildId, testOpts, featureFlags, }) {
19
+ return {
20
+ config,
21
+ defaultConfig,
22
+ cwd,
23
+ repositoryRoot,
24
+ context,
25
+ branch,
26
+ baseRelDir,
27
+ host: apiHost,
28
+ token,
29
+ siteId,
30
+ deployId,
31
+ buildId,
32
+ mode,
33
+ offline,
34
+ env: envOpt,
35
+ testOpts,
36
+ featureFlags,
37
+ };
38
+ };
39
+ // Retrieve configuration object
40
+ const tLoadConfig = async function ({ configOpts, cachedConfig, cachedConfigPath, envOpt, debug, logs, nodePath }) {
41
+ const { configPath, headersPath, redirectsPath, buildDir, repositoryRoot, config: netlifyConfig, context: contextA, branch: branchA, token: tokenA, api, siteInfo, env, } = await resolveInitialConfig(configOpts, cachedConfig, cachedConfigPath);
42
+ logConfigInfo({ logs, configPath, buildDir, netlifyConfig, context: contextA, debug });
43
+ const apiA = addApiErrorHandlers(api);
44
+ const envValues = mapObj(env, (key, { value }) => [key, value]);
45
+ const childEnv = getChildEnv({ envOpt, env: envValues });
46
+ const [{ packageJson }, userNodeVersion] = await Promise.all([getPackageJson(buildDir), getUserNodeVersion(nodePath)]);
47
+ return {
48
+ netlifyConfig,
49
+ configPath,
50
+ headersPath,
51
+ redirectsPath,
52
+ buildDir,
53
+ repositoryRoot,
54
+ packageJson,
55
+ userNodeVersion,
56
+ childEnv,
57
+ context: contextA,
58
+ branch: branchA,
59
+ token: tokenA,
60
+ api: apiA,
61
+ siteInfo,
62
+ };
63
+ };
64
+ export const loadConfig = measureDuration(tLoadConfig, 'resolve_config');
65
+ // Retrieve initial configuration.
66
+ // In the buildbot and CLI, we re-use the already parsed `@netlify/config`
67
+ // return value which is passed as `cachedConfig`/`cachedConfigPath`.
68
+ const resolveInitialConfig = async function (configOpts, cachedConfig, cachedConfigPath) {
69
+ return await resolveConfig({ ...configOpts, cachedConfig, cachedConfigPath });
70
+ };
71
+ const logConfigInfo = function ({ logs, configPath, buildDir, netlifyConfig, context, debug }) {
72
+ logBuildDir(logs, buildDir);
73
+ logConfigPath(logs, configPath);
74
+ logConfig({ logs, netlifyConfig, debug });
75
+ logContext(logs, context);
76
+ };
77
+ // Retrieve the configuration after it's been changed.
78
+ // This ensures any configuration changes done by plugins is validated and
79
+ // normalized.
80
+ // We use `debug: false` to avoid any debug logs. Otherwise every configuration
81
+ // change would create debug logs which would be too verbose.
82
+ // Errors are propagated and assigned to the specific plugin or core step
83
+ // which changed the configuration.
84
+ export const resolveUpdatedConfig = async function (configOpts, configMutations) {
85
+ try {
86
+ return await resolveConfig({ ...configOpts, configMutations, debug: false });
87
+ }
88
+ catch (error) {
89
+ changeErrorType(error, 'resolveConfig', 'pluginValidation');
90
+ throw error;
91
+ }
92
+ };
93
+ // If the configuration was changed, persist it to `netlify.toml`.
94
+ // If `netlify.toml` does not exist, create it inside repository root.
95
+ // This is only done when `saveConfig` is `true`. This allows performing this
96
+ // in the buildbot but not in local builds, since only the latter run in a
97
+ // container and we want to avoid saving files on local machines.
98
+ export const saveUpdatedConfig = async function ({ configMutations, buildDir, repositoryRoot, configPath = `${repositoryRoot}/netlify.toml`, headersPath, redirectsPath, logs, featureFlags, context, branch, debug, saveConfig, }) {
99
+ if (!saveConfig) {
100
+ return;
101
+ }
102
+ await updateConfig(configMutations, {
103
+ buildDir,
104
+ configPath,
105
+ headersPath,
106
+ redirectsPath,
107
+ context,
108
+ branch,
109
+ logs,
110
+ featureFlags,
111
+ });
112
+ await logConfigOnUpload({ logs, configPath, debug });
113
+ await logHeadersOnUpload({ logs, headersPath, debug });
114
+ await logRedirectsOnUpload({ logs, redirectsPath, debug });
115
+ };
116
+ export const restoreUpdatedConfig = async function ({ configMutations, buildDir, repositoryRoot, configPath = `${repositoryRoot}/netlify.toml`, headersPath, redirectsPath, saveConfig, }) {
117
+ if (!saveConfig) {
118
+ return;
119
+ }
120
+ await restoreConfig(configMutations, { buildDir, configPath, headersPath, redirectsPath });
121
+ };
@@ -0,0 +1,116 @@
1
+ import { relative, normalize } from 'path';
2
+ import { getCacheDir } from '@netlify/cache-utils';
3
+ import mapObj from 'map-obj';
4
+ import { pathExists } from 'path-exists';
5
+ import { ROOT_PACKAGE_JSON } from '../utils/json.js';
6
+ // Retrieve constants passed to plugins
7
+ export const getConstants = async function ({ configPath, buildDir, functionsDistDir, edgeFunctionsDistDir, cacheDir, netlifyConfig, siteInfo: { id: siteId }, apiHost, token, mode, }) {
8
+ const isLocal = mode !== 'buildbot';
9
+ const normalizedCacheDir = getCacheDir({ cacheDir, cwd: buildDir });
10
+ const constants = {
11
+ // Path to the Netlify configuration file
12
+ CONFIG_PATH: configPath,
13
+ // The directory where built serverless functions are placed before deployment
14
+ FUNCTIONS_DIST: functionsDistDir,
15
+ // The directory where built Edge Functions are placed before deployment
16
+ EDGE_FUNCTIONS_DIST: edgeFunctionsDistDir,
17
+ // Path to the Netlify build cache folder
18
+ CACHE_DIR: normalizedCacheDir,
19
+ // Boolean indicating whether the build was run locally (Netlify CLI) or in the production CI
20
+ IS_LOCAL: isLocal,
21
+ // The version of Netlify Build
22
+ NETLIFY_BUILD_VERSION: ROOT_PACKAGE_JSON.version,
23
+ // The Netlify Site ID
24
+ SITE_ID: siteId,
25
+ // The Netlify API access token
26
+ NETLIFY_API_TOKEN: token,
27
+ // The Netlify API host
28
+ NETLIFY_API_HOST: apiHost,
29
+ // The directory where internal functions (i.e. generated programmatically
30
+ // via plugins or others) live
31
+ INTERNAL_FUNCTIONS_SRC: `${buildDir}/${INTERNAL_FUNCTIONS_SRC}`,
32
+ // The directory where internal Edge Functions (i.e. generated programmatically
33
+ // via plugins or others) live
34
+ INTERNAL_EDGE_FUNCTIONS_SRC: `${buildDir}/${INTERNAL_EDGE_FUNCTIONS_SRC}`,
35
+ };
36
+ const constantsA = await addMutableConstants({ constants, buildDir, netlifyConfig });
37
+ return constantsA;
38
+ };
39
+ const INTERNAL_EDGE_FUNCTIONS_SRC = '.netlify/edge-functions';
40
+ const INTERNAL_FUNCTIONS_SRC = '.netlify/functions-internal';
41
+ // Retrieve constants which might change during the build if a plugin modifies
42
+ // `netlifyConfig` or creates some default directories.
43
+ // Unlike readonly constants, this is called again before each build step.
44
+ export const addMutableConstants = async function ({ constants, buildDir, netlifyConfig: { build: { publish, edge_functions: edgeFunctions }, functionsDirectory, }, }) {
45
+ const constantsA = {
46
+ ...constants,
47
+ // Directory that contains the deploy-ready HTML files and assets generated by the build
48
+ PUBLISH_DIR: publish,
49
+ // The directory where function source code lives
50
+ FUNCTIONS_SRC: functionsDirectory,
51
+ // The directory where Edge Functions source code lives
52
+ EDGE_FUNCTIONS_SRC: edgeFunctions,
53
+ };
54
+ const constantsB = await addDefaultConstants(constantsA, buildDir);
55
+ const constantsC = normalizeConstantsPaths(constantsB, buildDir);
56
+ return constantsC;
57
+ };
58
+ // Some `constants` have a default value when a specific file exists.
59
+ // Those default values are assigned by `@netlify/config`. However, the build
60
+ // command or plugins might create those specific files, in which case, the
61
+ // related `constant` should be updated, unless the user has explicitly
62
+ // configured it.
63
+ const addDefaultConstants = async function (constants, buildDir) {
64
+ const newConstants = await Promise.all(DEFAULT_PATHS.map(({ constantName, defaultPath }) => addDefaultConstant({ constants, constantName, defaultPath, buildDir })));
65
+ return Object.assign({}, constants, ...newConstants);
66
+ };
67
+ // The current directory is the build directory, which is correct, so we don't
68
+ // need to resolve paths
69
+ const DEFAULT_PATHS = [
70
+ // @todo Remove once we drop support for the legacy default functions directory.
71
+ { constantName: 'FUNCTIONS_SRC', defaultPath: 'netlify-automatic-functions' },
72
+ { constantName: 'FUNCTIONS_SRC', defaultPath: 'netlify/functions' },
73
+ { constantName: 'EDGE_FUNCTIONS_SRC', defaultPath: 'netlify/edge-functions' },
74
+ ];
75
+ const addDefaultConstant = async function ({ constants, constantName, defaultPath, buildDir }) {
76
+ // Configuration paths are relative to the build directory.
77
+ if (!isEmptyValue(constants[constantName]) || !(await pathExists(`${buildDir}/${defaultPath}`))) {
78
+ return {};
79
+ }
80
+ // However, the plugin child process' current directory is the build directory,
81
+ // so we can pass the relative path instead of the resolved absolute path.
82
+ return { [constantName]: defaultPath };
83
+ };
84
+ const normalizeConstantsPaths = function (constants, buildDir) {
85
+ return mapObj(constants, (key, path) => [key, normalizePath(path, buildDir, key)]);
86
+ };
87
+ // The current directory is `buildDir`. Most constants are inside this `buildDir`.
88
+ // Instead of passing absolute paths, we pass paths relative to `buildDir`, so
89
+ // that logs are less verbose.
90
+ const normalizePath = function (path, buildDir, key) {
91
+ if (isEmptyValue(path) || !CONSTANT_PATHS.has(key)) {
92
+ return path;
93
+ }
94
+ const pathA = normalize(path);
95
+ if (pathA === buildDir) {
96
+ return '.';
97
+ }
98
+ if (pathA.startsWith(buildDir)) {
99
+ return relative(buildDir, pathA);
100
+ }
101
+ return pathA;
102
+ };
103
+ const isEmptyValue = function (path) {
104
+ return path === undefined || path === '';
105
+ };
106
+ const CONSTANT_PATHS = new Set([
107
+ 'CONFIG_PATH',
108
+ 'PUBLISH_DIR',
109
+ 'FUNCTIONS_SRC',
110
+ 'FUNCTIONS_DIST',
111
+ 'INTERNAL_EDGE_FUNCTIONS_SRC',
112
+ 'INTERNAL_FUNCTIONS_SRC',
113
+ 'EDGE_FUNCTIONS_DIST',
114
+ 'EDGE_FUNCTIONS_SRC',
115
+ 'CACHE_DIR',
116
+ ]);
@@ -0,0 +1,27 @@
1
+ import { handleBuildError } from '../error/handle.js';
2
+ import { execBuild, startBuild } from './build.js';
3
+ import { getSeverity } from './severity.js';
4
+ export const startDev = async (devCommand, flags = {}) => {
5
+ const { errorMonitor, mode, logs, debug, testOpts, ...normalizedFlags } = startBuild(flags);
6
+ const errorParams = { errorMonitor, mode, logs, debug, testOpts };
7
+ try {
8
+ const { netlifyConfig: netlifyConfigA, configMutations } = await execBuild({
9
+ ...normalizedFlags,
10
+ errorMonitor,
11
+ errorParams,
12
+ mode,
13
+ logs,
14
+ debug,
15
+ testOpts,
16
+ timeline: 'dev',
17
+ devCommand,
18
+ });
19
+ const { success, severityCode } = getSeverity('success');
20
+ return { success, severityCode, netlifyConfig: netlifyConfigA, logs, configMutations };
21
+ }
22
+ catch (error) {
23
+ const { severity, message, stack } = await handleBuildError(error, errorParams);
24
+ const { success, severityCode } = getSeverity(severity);
25
+ return { success, severityCode, logs, error: { message, stack } };
26
+ }
27
+ };