-
Notifications
You must be signed in to change notification settings - Fork 53
/
node_version.ts
143 lines (125 loc) · 4.69 KB
/
node_version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { dirname } from 'path'
import { execPath, version as currentVersion } from 'process'
import semver from 'semver'
import link from 'terminal-link'
import { logWarning, logWarningSubHeader } from '../log/logger.js'
import { getPackageJson } from '../utils/package.js'
export type PluginsLoadedFrom = 'auto_install' | 'local' | 'package.json'
export type PluginsOptions = {
packageName: string
pluginPath?: any
pinnedVersion?: any
loadedFrom: PluginsLoadedFrom
origin: 'config' | string
inputs: Record<string, any>
pluginPackageJson?: Record<string, any>
}
/**
* This node version is minimum required to run the plugins code.
* If the users preferred Node.js version is below that we have to fall back to the system node version
*/
const MINIMUM_REQUIRED_NODE_VERSION = '^14.14.0 || >=16.0.0'
const UPCOMING_MINIMUM_REQUIRED_NODE_VERSION = '>=18.14.0'
/**
* Local plugins and `package.json`-installed plugins use user's preferred Node.js version if higher than our minimum
* supported version. Else default to the system Node version.
* Local and programmatic builds use `@netlify/build` Node.js version, which is
* usually the system's Node.js version.
* If the user Node version does not satisfy our supported engine range use our own system Node version
*/
export const addPluginsNodeVersion = function ({
featureFlags,
pluginsOptions,
nodePath,
userNodeVersion,
logs,
systemLog,
}) {
const currentNodeVersion = semver.clean(currentVersion)
return Promise.all(
pluginsOptions.map((pluginOptions) =>
addPluginNodeVersion({
featureFlags,
pluginOptions,
currentNodeVersion,
userNodeVersion,
nodePath,
logs,
systemLog,
}),
),
)
}
const addPluginNodeVersion = async function ({
featureFlags,
pluginOptions,
pluginOptions: { loadedFrom, packageName, pluginPath },
currentNodeVersion,
userNodeVersion,
nodePath,
logs,
systemLog,
}: {
pluginOptions: PluginsOptions
[key: string]: any
}) {
const systemNode = { ...pluginOptions, nodePath: execPath, nodeVersion: currentNodeVersion }
const userNode = { ...pluginOptions, nodePath, nodeVersion: userNodeVersion }
const isLocalPlugin = loadedFrom === 'local' || loadedFrom === 'package.json'
const isUIOrAutoInstalledPlugin = !isLocalPlugin
if (isUIOrAutoInstalledPlugin) {
return systemNode
}
if (
featureFlags.build_warn_upcoming_system_version_change &&
!semver.satisfies(userNodeVersion, UPCOMING_MINIMUM_REQUIRED_NODE_VERSION)
) {
logWarningSubHeader(
logs,
`Warning: Starting January 30, 2024 plugin "${packageName}" will be executed with Node.js version 20.`,
)
logWarning(
logs,
` We're upgrading our system node version on that day, which means the plugin cannot be executed with your defined Node.js version ${userNodeVersion}.
Please make sure your plugin supports being run on Node.js 20.
Read more about our minimum required version in our ${link(
'forums announcement',
'https://answers.netlify.com/t/build-plugin-update-system-node-js-version-upgrade-to-20/108633',
)}`,
)
if (pluginPath) {
const pluginDir = dirname(pluginPath)
const { packageJson: pluginPackageJson } = await getPackageJson(pluginDir)
// Ensure Node.js version is compatible with plugin's `engines.node`
const pluginNodeVersionRange = pluginPackageJson?.engines?.node
if (!pluginNodeVersionRange) {
systemLog(`plugin "${packageName}" might be affected by node.js 20 change`)
} else if (semver.satisfies('20.0.0', pluginNodeVersionRange)) {
systemLog(`plugin "${packageName}" probably not affected by node.js 20 change`)
} else {
logWarning(
logs,
`In its package.json, the plugin says it's incompatible with Node.js 20 (version range: "${pluginNodeVersionRange}"). Please upgrade the plugin, so it can be used with Node.js 20.`,
)
systemLog(`plugin "${packageName}" will be affected by node.js 20 change`)
}
} else {
systemLog(
`plugin "${packageName}" might be affected by node.js 20 change, pluginPath not available to determine its compatibility`,
)
}
}
if (semver.satisfies(userNodeVersion, MINIMUM_REQUIRED_NODE_VERSION)) {
return userNode
}
logWarningSubHeader(logs, `Warning: ${packageName} will be executed with Node.js version ${currentNodeVersion}`)
logWarning(
logs,
` The plugin cannot be executed with your defined Node.js version ${userNodeVersion}
Read more about our minimum required version in our ${link(
'forums announcement',
'https://answers.netlify.com/t/build-plugins-dropping-support-for-node-js-12/79421',
)}`,
)
return systemNode
}