forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeJsCompatibility.ts
More file actions
117 lines (101 loc) · 3.92 KB
/
NodeJsCompatibility.ts
File metadata and controls
117 lines (101 loc) · 3.92 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as colors from 'colors';
import * as semver from 'semver';
import { RushConfiguration } from '../api/RushConfiguration';
/**
* This constant is the major version of the next LTS node Node.js release. This constant should be updated when
* a new LTS version is added to Rush's support matrix.
*/
const UPCOMING_NODE_LTS_VERSION: number = 12;
const nodeVersion: string = process.versions.node;
const nodeMajorVersion: number = semver.major(nodeVersion);
export interface IWarnAboutVersionTooNewOptions {
isRushLib: boolean;
alreadyReportedNodeTooNewError: boolean;
}
export interface IWarnAboutCompatibilityIssuesOptions extends IWarnAboutVersionTooNewOptions {
rushConfiguration: RushConfiguration | undefined;
}
/**
* This class provides useful functions for warning if the current Node.js runtime isn't supported.
*
* @internal
*/
export class NodeJsCompatibility {
public static warnAboutCompatibilityIssues(options: IWarnAboutCompatibilityIssuesOptions): boolean {
// Only show the first warning
return (
NodeJsCompatibility.warnAboutVersionTooOld() ||
NodeJsCompatibility.warnAboutVersionTooNew(options) ||
NodeJsCompatibility.warnAboutOddNumberedVersion() ||
NodeJsCompatibility.warnAboutNonLtsVersion(options.rushConfiguration)
);
}
public static warnAboutVersionTooOld(): boolean {
if (semver.satisfies(nodeVersion, '< 8.9.0')) {
// We are on an ancient version of Node.js that is known not to work with Rush
console.error(colors.red(
`Your version of Node.js (${nodeVersion}) is very old and incompatible with Rush. ` +
`Please upgrade to the latest Long-Term Support (LTS) version.`
));
return true;
} else {
return false;
}
}
public static warnAboutVersionTooNew(options: IWarnAboutVersionTooNewOptions): boolean {
if (!options.alreadyReportedNodeTooNewError && nodeMajorVersion >= (UPCOMING_NODE_LTS_VERSION + 1)) {
// We are on a much newer release than we have tested and support
if (options.isRushLib) {
console.warn(colors.yellow(
`Your version of Node.js (${nodeVersion}) has not been tested with this release ` +
`of the Rush engine. Please consider upgrading the "rushVersion" setting in rush.json, ` +
`or downgrading Node.js.`
));
} else {
console.warn(colors.yellow(
`Your version of Node.js (${nodeVersion}) has not been tested with this release ` +
`of Rush. Please consider installing a newer version of the "@microsoft/rush" ` +
`package, or downgrading Node.js.`
));
}
return true;
} else {
return false;
}
}
public static warnAboutNonLtsVersion(rushConfiguration: RushConfiguration | undefined): boolean {
if (
rushConfiguration &&
!rushConfiguration.suppressNodeLtsWarning &&
!NodeJsCompatibility.isLtsVersion
) {
console.warn(colors.yellow(
`Your version of Node.js (${nodeVersion}) is not a Long-Term Support (LTS) release. ` +
'These versions frequently have bugs. Please consider installing a stable release.'
));
return true;
} else {
return false;
}
}
public static warnAboutOddNumberedVersion(): boolean {
if (NodeJsCompatibility.isOddNumberedVersion) {
console.warn(colors.yellow(
`Your version of Node.js (${nodeVersion}) is an odd-numbered release. ` +
`These releases frequently have bugs. Please consider installing a Long Term Support (LTS) ` +
`version instead.`
));
return true;
} else {
return false;
}
}
public static get isLtsVersion(): boolean {
return !!process.release.lts;
}
public static get isOddNumberedVersion(): boolean {
return (nodeMajorVersion % 2) !== 0;
}
}