From 80a6116bef8a909246dd358ad15023bdbfc47b16 Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Mon, 4 Nov 2024 15:35:18 +0100 Subject: [PATCH 1/3] Fix update-node-version script for non-existent @types/node version --- .../ql-vscode/scripts/update-node-version.ts | 78 +++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/extensions/ql-vscode/scripts/update-node-version.ts b/extensions/ql-vscode/scripts/update-node-version.ts index b07cfb88a3d..f0da292681c 100644 --- a/extensions/ql-vscode/scripts/update-node-version.ts +++ b/extensions/ql-vscode/scripts/update-node-version.ts @@ -3,6 +3,7 @@ import { execSync } from "child_process"; import { outputFile, readFile, readJSON } from "fs-extra"; import { getVersionInformation } from "./util/vscode-versions"; import { fetchJson } from "./util/fetch"; +import { SemVer } from "semver"; const extensionDirectory = resolve(__dirname, ".."); @@ -10,6 +11,29 @@ interface Release { tag_name: string; } +interface NpmViewError { + error: { + code: string; + summary: string; + detail: string; + }; +} + +interface ExecError extends Error { + status: number; + stdout: string; +} + +function isExecError(e: unknown): e is ExecError { + return ( + e instanceof Error && + "status" in e && + typeof e.status === "number" && + "stdout" in e && + typeof e.stdout === "string" + ); +} + async function updateNodeVersion() { const latestVsCodeRelease = await fetchJson( "https://api.github.com/repos/microsoft/vscode/releases/latest", @@ -49,6 +73,8 @@ async function updateNodeVersion() { "utf8", ); + const nodeVersion = new SemVer(versionInformation.nodeVersion); + // The @types/node version needs to match the first two parts of the Node // version, e.g. if the Node version is 18.17.3, the @types/node version // should be 18.17.*. This corresponds with the documentation at @@ -56,13 +82,55 @@ async function updateNodeVersion() { // "The patch version of the type declaration package is unrelated to the library patch version. This allows // Definitely Typed to safely update type declarations for the same major/minor version of a library." // 18.17.* is equivalent to >=18.17.0 <18.18.0 - const typesNodeVersion = versionInformation.nodeVersion - .split(".") - .slice(0, 2) - .join("."); + // In some cases, the @types/node version matching the exact Node version may not exist, in which case we'll try + // the next lower minor version, and so on, until we find a version that exists. + const typesNodeSemver = new SemVer(nodeVersion); + typesNodeSemver.patch = 0; + + // eslint-disable-next-line no-constant-condition + while (true) { + const typesNodeVersion = `${typesNodeSemver.major}.${typesNodeSemver.minor}.*`; + + try { + // Check that this version actually exists + console.log(`Checking if @types/node@${typesNodeVersion} exists`); + + execSync(`npm view --json "@types/node@${typesNodeVersion}"`, { + encoding: "utf-8", + stdio: "pipe", + }); + + console.log(`@types/node@${typesNodeVersion} exists`); + + // If it exists, we can break out of this loop + break; + } catch (e: unknown) { + if (!isExecError(e)) { + throw e; + } + + const error = JSON.parse(e.stdout) as NpmViewError; + if (error.error.code !== "E404") { + throw new Error(error.error.detail); + } + + console.log( + `@types/node package doesn't exist for ${typesNodeVersion}, trying a lower version (${error.error.summary})`, + ); + + // This means the version doesn't exist, so we'll try decrementing the minor version + typesNodeSemver.minor -= 1; + if (typesNodeSemver.minor < 0) { + throw new Error( + `Could not find a suitable @types/node version for Node ${nodeVersion.format()}`, + ); + } + } + } packageJson.engines.node = `^${versionInformation.nodeVersion}`; - packageJson.devDependencies["@types/node"] = `${typesNodeVersion}.*`; + packageJson.devDependencies["@types/node"] = + `${typesNodeSemver.major}.${typesNodeSemver.minor}.*`; await outputFile( join(extensionDirectory, "package.json"), From e7d76f760576e7920e09e37e9223d027426cc4da Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Wed, 6 Nov 2024 13:46:55 +0100 Subject: [PATCH 2/3] Always try updating Node version --- .../ql-vscode/scripts/update-node-version.ts | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/extensions/ql-vscode/scripts/update-node-version.ts b/extensions/ql-vscode/scripts/update-node-version.ts index f0da292681c..b3bb4d9346e 100644 --- a/extensions/ql-vscode/scripts/update-node-version.ts +++ b/extensions/ql-vscode/scripts/update-node-version.ts @@ -1,6 +1,6 @@ import { join, resolve } from "path"; import { execSync } from "child_process"; -import { outputFile, readFile, readJSON } from "fs-extra"; +import { outputFile, readJSON } from "fs-extra"; import { getVersionInformation } from "./util/vscode-versions"; import { fetchJson } from "./util/fetch"; import { SemVer } from "semver"; @@ -47,19 +47,7 @@ async function updateNodeVersion() { `VS Code ${versionInformation.vscodeVersion} uses Electron ${versionInformation.electronVersion} and Node ${versionInformation.nodeVersion}`, ); - let currentNodeVersion = ( - await readFile(join(extensionDirectory, ".nvmrc"), "utf8") - ).trim(); - if (currentNodeVersion.startsWith("v")) { - currentNodeVersion = currentNodeVersion.slice(1); - } - - if (currentNodeVersion === versionInformation.nodeVersion) { - console.log("Node version is already up to date"); - return; - } - - console.log("Node version needs to be updated, updating now"); + console.log("Updating files related to the Node version"); await outputFile( join(extensionDirectory, ".nvmrc"), From 20a8976d8fbb15e33ba7436522b8150643d2c6db Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Wed, 6 Nov 2024 13:47:22 +0100 Subject: [PATCH 3/3] Use vite-node instead of ts-node --- .github/workflows/update-node-version.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update-node-version.yml b/.github/workflows/update-node-version.yml index 800a34f38aa..27fba7e604a 100644 --- a/.github/workflows/update-node-version.yml +++ b/.github/workflows/update-node-version.yml @@ -34,7 +34,7 @@ jobs: - name: Update Node version working-directory: extensions/ql-vscode run: | - npx ts-node scripts/update-node-version.ts + npx vite-node scripts/update-node-version.ts shell: bash - name: Get current Node version working-directory: extensions/ql-vscode