|
1 | 1 | const { log } = require('proc-log')
|
2 | 2 |
|
3 | 3 | class BaseCommand {
|
| 4 | + // these defaults can be overridden by individual commands |
4 | 5 | static workspaces = false
|
5 | 6 | static ignoreImplicitWorkspace = true
|
| 7 | + static checkDevEngines = false |
6 | 8 |
|
7 |
| - // these are all overridden by individual commands |
| 9 | + // these should always be overridden by individual commands |
8 | 10 | static name = null
|
9 | 11 | static description = null
|
10 | 12 | static params = null
|
@@ -129,6 +131,63 @@ class BaseCommand {
|
129 | 131 | }
|
130 | 132 | }
|
131 | 133 |
|
| 134 | + // Checks the devEngines entry in the package.json at this.localPrefix |
| 135 | + async checkDevEngines () { |
| 136 | + const force = this.npm.flatOptions.force |
| 137 | + |
| 138 | + const { devEngines } = await require('@npmcli/package-json') |
| 139 | + .normalize(this.npm.config.localPrefix) |
| 140 | + .then(p => p.content) |
| 141 | + .catch(() => ({})) |
| 142 | + |
| 143 | + if (typeof devEngines === 'undefined') { |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + const { checkDevEngines, currentEnv } = require('npm-install-checks') |
| 148 | + const current = currentEnv.devEngines({ |
| 149 | + nodeVersion: this.npm.nodeVersion, |
| 150 | + npmVersion: this.npm.version, |
| 151 | + }) |
| 152 | + |
| 153 | + const failures = checkDevEngines(devEngines, current) |
| 154 | + const warnings = failures.filter(f => f.isWarn) |
| 155 | + const errors = failures.filter(f => f.isError) |
| 156 | + |
| 157 | + const genMsg = (failure, i = 0) => { |
| 158 | + return [...new Set([ |
| 159 | + // eslint-disable-next-line |
| 160 | + i === 0 ? 'The developer of this package has specified the following through devEngines' : '', |
| 161 | + `${failure.message}`, |
| 162 | + `${failure.errors.map(e => e.message).join('\n')}`, |
| 163 | + ])].filter(v => v).join('\n') |
| 164 | + } |
| 165 | + |
| 166 | + [...warnings, ...(force ? errors : [])].forEach((failure, i) => { |
| 167 | + const message = genMsg(failure, i) |
| 168 | + log.warn('EBADDEVENGINES', message) |
| 169 | + log.warn('EBADDEVENGINES', { |
| 170 | + current: failure.current, |
| 171 | + required: failure.required, |
| 172 | + }) |
| 173 | + }) |
| 174 | + |
| 175 | + if (force) { |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + if (errors.length) { |
| 180 | + const failure = errors[0] |
| 181 | + const message = genMsg(failure) |
| 182 | + throw Object.assign(new Error(message), { |
| 183 | + engine: failure.engine, |
| 184 | + code: 'EBADDEVENGINES', |
| 185 | + current: failure.current, |
| 186 | + required: failure.required, |
| 187 | + }) |
| 188 | + } |
| 189 | + } |
| 190 | + |
132 | 191 | async setWorkspaces () {
|
133 | 192 | const { relative } = require('node:path')
|
134 | 193 |
|
|
0 commit comments