(feat): Check for existing wrangler installation

This commit is contained in:
Adishwar Rishi 2024-02-09 22:07:27 +11:00 committed by Peter Bacon Darwin
parent ea5754c9ad
commit f78ca6a656

View file

@ -10,6 +10,8 @@ import {
setFailed, setFailed,
setOutput, setOutput,
} from "@actions/core"; } from "@actions/core";
import { getExecOutput } from "@actions/exec";
import semverGt from "semver/functions/gt";
import { exec, execShell } from "./exec"; import { exec, execShell } from "./exec";
import { checkWorkingDirectory, semverCompare } from "./utils"; import { checkWorkingDirectory, semverCompare } from "./utils";
import { getPackageManager } from "./packageManagers"; 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"); startGroup("📥 Installing Wrangler");
try { try {
await exec( await exec(