From f78ca6a6567632f396af28b9190c071b033981d7 Mon Sep 17 00:00:00 2001 From: Adishwar Rishi Date: Fri, 9 Feb 2024 22:07:27 +1100 Subject: [PATCH] (feat): Check for existing wrangler installation --- src/index.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/index.ts b/src/index.ts index 405aa3b..ff79412 100755 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,8 @@ import { setFailed, setOutput, } from "@actions/core"; +import { getExecOutput } from "@actions/exec"; +import semverGt from "semver/functions/gt"; import { exec, execShell } from "./exec"; import { checkWorkingDirectory, semverCompare } from "./utils"; import { getPackageManager } from "./packageManagers"; @@ -82,6 +84,44 @@ async function installWrangler() { ); } + startGroup("🔍 Checking for existing Wrangler installation"); + let installedVersion = ""; + try { + const { stdout } = await getExecOutput( + packageManager.exec, + ["wrangler", "--version"], + { + cwd: config["workingDirectory"], + silent: true, + }, + ); + const versionMatch = stdout.match(/wrangler (\d+\.\d+\.\d+)/); + if (!versionMatch) { + throw new Error( + `Unable to parse Wrangler version from the output: ${stdout}`, + ); + } + installedVersion = versionMatch[1]; + if (semverGt(config["WRANGLER_VERSION"], installedVersion)) { + info( + `Wrangler version ${installedVersion} is less than required. Installing...`, + true, + ); + } else { + info(`✅ Using Wrangler ${installedVersion}`, true); + endGroup(); + return; + } + } catch (error) { + debug(`Error checking Wrangler version: ${error}`); + info( + "Wrangler not found or version is not compatible. Installing...", + true, + ); + } finally { + endGroup(); + } + startGroup("📥 Installing Wrangler"); try { await exec(