2021-06-08 10:44:33 +00:00
|
|
|
import { readFileSync, writeFileSync } from 'fs';
|
|
|
|
import https from 'https';
|
2019-05-12 17:08:37 +00:00
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
const srcRoot = new URL('../../', import.meta.url);
|
2019-11-02 07:48:47 +00:00
|
|
|
|
|
|
|
const isRelease = () => {
|
|
|
|
const re = /#define NODE_VERSION_IS_RELEASE 0/;
|
2021-06-08 10:44:33 +00:00
|
|
|
const file = new URL('./src/node_version.h', srcRoot);
|
2019-11-02 07:48:47 +00:00
|
|
|
return !re.test(readFileSync(file, { encoding: 'utf8' }));
|
|
|
|
};
|
|
|
|
|
2019-05-12 17:08:37 +00:00
|
|
|
const getUrl = (url) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-03-27 14:04:40 +00:00
|
|
|
const request = https.get(url, { timeout: 30000 }, (response) => {
|
2019-05-12 17:08:37 +00:00
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
reject(new Error(
|
|
|
|
`Failed to get ${url}, status code ${response.statusCode}`));
|
|
|
|
}
|
|
|
|
response.setEncoding('utf8');
|
|
|
|
let body = '';
|
2019-11-02 07:48:47 +00:00
|
|
|
response.on('aborted', () => reject());
|
2019-05-12 17:08:37 +00:00
|
|
|
response.on('data', (data) => body += data);
|
|
|
|
response.on('end', () => resolve(body));
|
|
|
|
});
|
|
|
|
request.on('error', (err) => reject(err));
|
2019-11-02 07:48:47 +00:00
|
|
|
request.on('timeout', () => request.abort());
|
2019-05-12 17:08:37 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:01:21 +00:00
|
|
|
const kNoInternet = !!process.env.NODE_TEST_NO_INTERNET;
|
2020-03-27 14:04:40 +00:00
|
|
|
const outFile = (process.argv.length > 2 ? process.argv[2] : undefined);
|
2020-02-18 15:01:21 +00:00
|
|
|
|
2020-03-27 14:04:40 +00:00
|
|
|
async function versions() {
|
|
|
|
// The CHANGELOG.md on release branches may not reference newer semver
|
|
|
|
// majors of Node.js so fetch and parse the version from the master branch.
|
|
|
|
const url =
|
2021-05-03 09:15:35 +00:00
|
|
|
'https://raw.githubusercontent.com/nodejs/node/HEAD/CHANGELOG.md';
|
2020-03-27 14:04:40 +00:00
|
|
|
let changelog;
|
2021-06-08 10:44:33 +00:00
|
|
|
const file = new URL('./CHANGELOG.md', srcRoot);
|
2020-03-27 14:04:40 +00:00
|
|
|
if (kNoInternet) {
|
|
|
|
changelog = readFileSync(file, { encoding: 'utf8' });
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
changelog = await getUrl(url);
|
|
|
|
} catch (e) {
|
|
|
|
// Fail if this is a release build, otherwise fallback to local files.
|
|
|
|
if (isRelease()) {
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
console.warn(`Unable to retrieve ${url}. Falling back to ${file}.`);
|
|
|
|
changelog = readFileSync(file, { encoding: 'utf8' });
|
2019-11-02 07:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 14:04:40 +00:00
|
|
|
}
|
|
|
|
const ltsRE = /Long Term Support/i;
|
|
|
|
const versionRE = /\* \[Node\.js ([0-9.]+)\]\S+ (.*)\r?\n/g;
|
|
|
|
const _versions = [];
|
|
|
|
let match;
|
|
|
|
while ((match = versionRE.exec(changelog)) != null) {
|
|
|
|
const entry = { num: `${match[1]}.x` };
|
|
|
|
if (ltsRE.test(match[2])) {
|
|
|
|
entry.lts = true;
|
2019-05-12 17:08:37 +00:00
|
|
|
}
|
2020-03-27 14:04:40 +00:00
|
|
|
_versions.push(entry);
|
2019-05-12 17:08:37 +00:00
|
|
|
}
|
2020-03-27 14:04:40 +00:00
|
|
|
return _versions;
|
|
|
|
}
|
|
|
|
|
|
|
|
versions().then((v) => {
|
|
|
|
if (outFile) {
|
|
|
|
writeFileSync(outFile, JSON.stringify(v));
|
|
|
|
} else {
|
|
|
|
console.log(JSON.stringify(v));
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|