UNPKG

5.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.emitSupportPolicyInformation = void 0;
4const node_fs_1 = require("node:fs");
5const node_https_1 = require("node:https");
6const node_path_1 = require("node:path");
7const chalk = require("chalk");
8const version_1 = require("./version");
9const SILENCE_ENV_VAR = 'JSII_SILENCE_SUPPORT_WARNING';
10const THIRTY_DAYS_IN_MILLIS = 2592000000;
11/**
12 * Checks whether the current release line is close to End-of-Support (within
13 * 30 days), or already in End-of-Support, and if that is the case, emits a
14 * warning to call the user to action.
15 *
16 * It is possible for users to opt out of these notifications by setting the
17 * `JSII_SILENCE_SUPPORT_WARNING` environment variable to any truthy value (that
18 * is, any non-empty value).
19 */
20async function emitSupportPolicyInformation() {
21 if (process.env[SILENCE_ENV_VAR]) {
22 return;
23 }
24 const data = await getReleasesDocument();
25 if (data.current == version_1.RELEASE_LINE) {
26 // Current release is not close to deprecation
27 return;
28 }
29 const endOfSupportDate = data.endOfSupport?.includes(version_1.RELEASE_LINE) ? new Date(0) : data.maintenance[version_1.RELEASE_LINE];
30 if (endOfSupportDate == null) {
31 // Don't know the status, so don't say anything...
32 return;
33 }
34 const now = new Date();
35 const inThirtyDays = new Date(now.getTime() + THIRTY_DAYS_IN_MILLIS);
36 const alternatives = Object.entries(data.maintenance)
37 .flatMap(([release, dateStr]) => {
38 const date = new Date(dateStr);
39 if (date <= inThirtyDays) {
40 return [];
41 }
42 return [{ release, date }];
43 })
44 .reduce((acc, { release, date }) => {
45 if (acc.length === 0) {
46 acc.push('', 'Other actively supported release lines include:');
47 }
48 acc.push(`- ${release} (planned End-of-Support date: ${date.toISOString()})`);
49 return acc;
50 }, new Array());
51 if (endOfSupportDate <= now) {
52 // End-of-Support already!
53 veryVisibleMessage(chalk.bgRed.white.bold, `The ${version_1.RELEASE_LINE} release line of jsii has reached End-of-Support.`, `We strongly recommend you upgrade to the current release line (${data.current}) at your earliest convenience.`, ...alternatives);
54 }
55 else if (endOfSupportDate <= inThirtyDays) {
56 // End-of-Support within 30 days!
57 veryVisibleMessage(chalk.bgYellow.black, `The ${version_1.RELEASE_LINE} release line of jsii will reach End-of-Support soon, on ${endOfSupportDate.toISOString()}.`, `We strongly recommend you upgrade to the current release line (${data.current}) at your earliest convenience.`, ...alternatives);
58 }
59}
60exports.emitSupportPolicyInformation = emitSupportPolicyInformation;
61/**
62 * Downloads the latest `releases.json` document from 'https://raw.githubusercontent.com/aws/jsii-compiler/main/releases.json'
63 * if possible, or falls back to the built-in version of that file if that fails in any way.
64 */
65async function getReleasesDocument() {
66 const downloaded = await new Promise((ok, ko) => {
67 const request = (0, node_https_1.get)(new URL('https://raw.githubusercontent.com/aws/jsii-compiler/main/releases.json'), (response) => {
68 if (response.statusCode === 404) {
69 return ok(undefined);
70 }
71 if (response.statusCode !== 200) {
72 return ko(`received error response: HTTP ${response.statusCode} - ${response.statusMessage}`);
73 }
74 response.once('error', ko);
75 const chunks = new Array();
76 response.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
77 response.once('end', () => ok(Buffer.concat(chunks).toString('utf-8')));
78 });
79 request.once('abort', () => ko('request aborted'));
80 request.once('timeout', () => ko('request timed out'));
81 request.once('error', ko);
82 request.end();
83 }).catch((cause) => {
84 if (process.env.JSII_DEBUG) {
85 console.error(`Could not download releases.json from GitHub: ${cause}`);
86 }
87 undefined;
88 });
89 return JSON.parse(downloaded ?? (0, node_fs_1.readFileSync)((0, node_path_1.join)(__dirname, '..', 'releases.json'), 'utf-8'), (key, value) => {
90 if (key !== 'maintenance') {
91 return value;
92 }
93 return Object.fromEntries(Object.entries(value).map(([release, date]) => [release, new Date(date)]));
94 });
95}
96function veryVisibleMessage(formatter, ...lines) {
97 if (lines.length === 0) {
98 throw new Error(`At least one line of message must be provided!`);
99 }
100 const len = Math.max(...lines.map((line) => line.length));
101 const border = formatter('!'.repeat(len + 8));
102 const spacer = formatter(`!! ${' '.repeat(len)} !!`);
103 console.error(border);
104 console.error(spacer);
105 for (const line of lines) {
106 console.error(formatter(`!! ${line.padEnd(len, ' ')} !!`));
107 }
108 console.error(spacer);
109 console.error(border);
110}
111//# sourceMappingURL=support.js.map
\No newline at end of file