|
| 1 | +import { promisify } from "util"; |
| 2 | + |
| 3 | +import child_process from "child_process"; |
| 4 | + |
| 5 | +const exec = promisify(child_process.exec); |
| 6 | + |
| 7 | +// a function that queries the Node.js release website for new versions, |
| 8 | +// compare the available ones with the ones we use in this repo |
| 9 | +// and returns whether we should update or not |
| 10 | +const checkIfThereAreNewVersions = async (github) => { |
| 11 | + try { |
| 12 | + const { stdout: versionsOutput } = await exec(". ./functions.sh && get_versions", { shell: "bash" }); |
| 13 | + |
| 14 | + const supportedVersions = versionsOutput.trim().split(" "); |
| 15 | + |
| 16 | + let latestSupportedVersions = {}; |
| 17 | + |
| 18 | + for (let supportedVersion of supportedVersions) { |
| 19 | + const { stdout } = await exec(`ls ${supportedVersion}`); |
| 20 | + |
| 21 | + const { stdout: fullVersionOutput } = await exec(`. ./functions.sh && get_full_version ./${supportedVersion}/${stdout.trim().split("\n")[0]}`, { shell: "bash" }); |
| 22 | + |
| 23 | + console.log(fullVersionOutput); |
| 24 | + |
| 25 | + latestSupportedVersions[supportedVersion] = { fullVersion: fullVersionOutput.trim() }; |
| 26 | + } |
| 27 | + |
| 28 | + const { data: availableVersionsJson } = await github.request('https://nodejs.org/download/release/index.json'); |
| 29 | + |
| 30 | + // filter only more recent versions of availableVersionsJson for each major version in latestSupportedVersions' keys |
| 31 | + // e.g. if latestSupportedVersions = { "12": "12.22.10", "14": "14.19.0", "16": "16.14.0", "17": "17.5.0" } |
| 32 | + // and availableVersions = ["Node.js 12.22.10", "Node.js 12.24.0", "Node.js 14.19.0", "Node.js 14.22.0", "Node.js 16.14.0", "Node.js 16.16.0", "Node.js 17.5.0", "Node.js 17.8.0"] |
| 33 | + // return { "12": "12.24.0", "14": "14.22.0", "16": "16.16.0", "17": "17.8.0" } |
| 34 | + |
| 35 | + let filteredNewerVersions = {}; |
| 36 | + |
| 37 | + for (let availableVersion of availableVersionsJson) { |
| 38 | + const [availableMajor, availableMinor, availablePatch] = availableVersion.version.split("v")[1].split("."); |
| 39 | + if (latestSupportedVersions[availableMajor] == null) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + const [_latestMajor, latestMinor, latestPatch] = latestSupportedVersions[availableMajor].fullVersion.split("."); |
| 43 | + if (latestSupportedVersions[availableMajor] && (Number(availableMinor) > Number(latestMinor) || (availableMinor === latestMinor && Number(availablePatch) > Number(latestPatch)))) { |
| 44 | + filteredNewerVersions[availableMajor] = { fullVersion: `${availableMajor}.${availableMinor}.${availablePatch}` }; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + shouldUpdate: Object.keys(filteredNewerVersions).length > 0 && JSON.stringify(filteredNewerVersions) !== JSON.stringify(latestSupportedVersions), |
| 50 | + versions: filteredNewerVersions, |
| 51 | + } |
| 52 | + } catch (error) { |
| 53 | + console.error(error); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +// a function that queries the Node.js unofficial release website for new musl versions and security releases, |
| 59 | +// and returns relevant information |
| 60 | +const checkForMuslVersionsAndSecurityReleases = async (github, versions) => { |
| 61 | + try { |
| 62 | + const { data: unofficialBuildsIndexText } = await github.request('https://unofficial-builds.nodejs.org/download/release/index.json'); |
| 63 | + |
| 64 | + for (let version of Object.keys(versions)) { |
| 65 | + const { data: unofficialBuildsWebsiteText } = await github.request(`https://unofficial-builds.nodejs.org/download/release/v${versions[version].fullVersion}`); |
| 66 | + |
| 67 | + versions[version].muslBuildExists = unofficialBuildsWebsiteText.includes("musl"); |
| 68 | + versions[version].isSecurityRelease = unofficialBuildsIndexText.find(indexVersion => indexVersion.version === `v${versions[version].fullVersion}`)?.security; |
| 69 | + } |
| 70 | + return versions; |
| 71 | + } catch (error) { |
| 72 | + console.error(error); |
| 73 | + process.exit(1); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +export default async function(github) { |
| 78 | +// if there are no new versions, exit gracefully |
| 79 | +// if there are new versions, |
| 80 | +// check for musl builds |
| 81 | +// then run update.sh |
| 82 | + const { shouldUpdate, versions } = await checkIfThereAreNewVersions(github); |
| 83 | + |
| 84 | + if (!shouldUpdate) { |
| 85 | + console.log("No new versions found. No update required."); |
| 86 | + process.exit(0); |
| 87 | + } else { |
| 88 | + const newVersions = await checkForMuslVersionsAndSecurityReleases(github, versions); |
| 89 | + let updatedVersions = []; |
| 90 | + for (let version of Object.keys(newVersions)) { |
| 91 | + if (newVersions[version].muslBuildExists) { |
| 92 | + const { stdout } = await exec(`./update.sh ${newVersions[version].isSecurityRelease ? "-s " : ""}${version}`); |
| 93 | + console.log(stdout); |
| 94 | + updatedVersions.push(newVersions[version].fullVersion); |
| 95 | + } else { |
| 96 | + console.log(`There's no musl build for version ${newVersions[version].fullVersion} yet.`); |
| 97 | + process.exit(0); |
| 98 | + } |
| 99 | + } |
| 100 | + console.log(`::set-output name=updated-versions::${updatedVersions.join(',')}`); |
| 101 | + const { stdout } = (await exec(`git diff`)); |
| 102 | + console.log(stdout); |
| 103 | + } |
| 104 | +} |
0 commit comments