|
1 | 1 | import semver from "semver";
|
2 | 2 |
|
| 3 | +import { assertHardhatInvariant } from "../core/errors"; |
3 | 4 | import { SUPPORTED_NODE_VERSIONS } from "./constants";
|
4 | 5 |
|
5 | 6 | /**
|
6 |
| - * Determine if the node version is unsupported by Hardhat |
7 |
| - * and so a warning message should be shown. |
| 7 | + * Determine if the node version should trigger an unsupported |
| 8 | + * warning. |
8 | 9 | *
|
9 |
| - * The current rule is that Hardhat supports all `Current`, |
10 |
| - * `Active LTS`, and `Maintenance LTS` versions of Node.js |
11 |
| - * as defined in the Node.js release schedule and encoded |
12 |
| - * in the `SUPPORTED_NODE_VERSIONS` constant. |
| 10 | + * The current rule is that a unsupported warning will be shown if |
| 11 | + * |
| 12 | + * 1. An odd numbered version of Node.js is used - as this will never go to LTS |
| 13 | + * 2. The version is less than the minimum supported version |
| 14 | + * |
| 15 | + * We intentionally do not warn on newer **even** versions of Node.js. |
13 | 16 | */
|
14 | 17 | export function isNodeVersionToWarnOn(nodeVersion: string): boolean {
|
15 |
| - return !semver.satisfies(nodeVersion, SUPPORTED_NODE_VERSIONS.join(" || ")); |
| 18 | + const supportedVersions = SUPPORTED_NODE_VERSIONS.join(" || "); |
| 19 | + |
| 20 | + // If the version is supported, no need to warn and short circuit |
| 21 | + if (semver.satisfies(nodeVersion, supportedVersions)) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + if (_onOddNumberedVersion(nodeVersion)) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + if (_lessThanMinimumSupportedVersion(nodeVersion, supportedVersions)) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + // A newer version of Node.js that will go to LTS |
| 34 | + // we have opted not to warn. |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +function _onOddNumberedVersion(nodeVersion: string) { |
| 39 | + return semver.major(nodeVersion) % 2 === 1; |
| 40 | +} |
| 41 | + |
| 42 | +function _lessThanMinimumSupportedVersion( |
| 43 | + nodeVersion: string, |
| 44 | + supportedVersions: string |
| 45 | +) { |
| 46 | + const minSupportedVersion = semver.minVersion(supportedVersions); |
| 47 | + |
| 48 | + assertHardhatInvariant( |
| 49 | + minSupportedVersion !== null, |
| 50 | + "Unexpectedly failed to parse the minimum supported version of Node.js" |
| 51 | + ); |
| 52 | + |
| 53 | + return semver.lt(nodeVersion, minSupportedVersion); |
16 | 54 | }
|
0 commit comments