UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.getSupportedNodeVersion = exports.getDiscontinuedNodeVersions = exports.getLatestNodeVersion = void 0;
7const semver_1 = require("semver");
8const boxen_1 = __importDefault(require("boxen"));
9const errors_1 = require("../errors");
10const debug_1 = __importDefault(require("../debug"));
11const allOptions = [
12 { major: 12, range: '12.x', runtime: 'nodejs12.x' },
13 { major: 10, range: '10.x', runtime: 'nodejs10.x' },
14 {
15 major: 8,
16 range: '8.10.x',
17 runtime: 'nodejs8.10',
18 discontinueDate: new Date('2020-01-06'),
19 },
20];
21const pleaseSet = 'Please set "engines": { "node": "' +
22 getLatestNodeVersion().range +
23 '" } in your `package.json` file to upgrade to Node.js ' +
24 getLatestNodeVersion().major +
25 '.';
26const upstreamProvider = 'This change is the result of a decision made by an upstream infrastructure provider (AWS).' +
27 '\nRead more: https://docs.aws.amazon.com/lambda/latest/dg/runtime-support-policy.html';
28function getLatestNodeVersion() {
29 return allOptions[0];
30}
31exports.getLatestNodeVersion = getLatestNodeVersion;
32function getDiscontinuedNodeVersions() {
33 return allOptions.filter(isDiscontinued);
34}
35exports.getDiscontinuedNodeVersions = getDiscontinuedNodeVersions;
36async function getSupportedNodeVersion(engineRange, isAuto) {
37 let selection = getLatestNodeVersion();
38 if (engineRange) {
39 const found = semver_1.validRange(engineRange) &&
40 allOptions.some(o => {
41 // the array is already in order so return the first
42 // match which will be the newest version of node
43 selection = o;
44 return semver_1.intersects(o.range, engineRange);
45 });
46 if (!found) {
47 const intro = isAuto || !engineRange
48 ? 'This project is using an invalid version of Node.js and must be changed.'
49 : 'Found `engines` in `package.json` with an invalid Node.js version range: "' +
50 engineRange +
51 '".';
52 throw new errors_1.NowBuildError({
53 code: 'BUILD_UTILS_NODE_VERSION_INVALID',
54 link: 'https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version',
55 message: intro + '\n' + pleaseSet,
56 });
57 }
58 }
59 if (isDiscontinued(selection)) {
60 const intro = isAuto || !engineRange
61 ? 'This project is using a discontinued version of Node.js (' +
62 selection.range +
63 ') and must be upgraded.'
64 : 'Found `engines` in `package.json` with a discontinued Node.js version range: "' +
65 engineRange +
66 '".';
67 throw new errors_1.NowBuildError({
68 code: 'BUILD_UTILS_NODE_VERSION_DISCONTINUED',
69 link: 'https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version',
70 message: intro + '\n' + pleaseSet + '\n' + upstreamProvider,
71 });
72 }
73 debug_1.default(isAuto || !engineRange
74 ? 'Using default Node.js range: "' + selection.range + '".'
75 : 'Found `engines` in `package.json`, selecting range: "' +
76 selection.range +
77 '".');
78 if (selection.discontinueDate) {
79 const d = selection.discontinueDate.toISOString().split('T')[0];
80 console.warn(boxen_1.default('NOTICE' +
81 '\n' +
82 `\nNode.js version ${selection.range} has reached end-of-life.` +
83 `\nAs a result, deployments created on or after ${d} will fail to build.` +
84 '\n' +
85 pleaseSet +
86 '\n' +
87 upstreamProvider, { padding: 1 }));
88 }
89 return selection;
90}
91exports.getSupportedNodeVersion = getSupportedNodeVersion;
92function isDiscontinued({ discontinueDate }) {
93 const today = Date.now();
94 return discontinueDate !== undefined && discontinueDate.getTime() <= today;
95}