From 355fe3a07024485c250675653a9a0710bc222ef7 Mon Sep 17 00:00:00 2001 From: Jesse Dijkstra Date: Thu, 11 Apr 2024 14:15:36 +0200 Subject: [PATCH] Add .tool-versions and .dvmrc support --- README.md | 14 ++++++++++++++ action.yml | 2 ++ main.js | 18 +++++++++++++----- src/version.js | 28 +++++++++++++++++++++++++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4d9a604..915c375 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,20 @@ Set up your GitHub Actions workflow with a specific version of Deno. ## Usage +### Version from file + +```yaml +- uses: denoland/setup-deno@v1 + with: + deno-version-file: .dvmrc +``` + +```yaml +- uses: denoland/setup-deno@v1 + with: + deno-version-file: .tool-versions +``` + ### Latest stable for a major ```yaml diff --git a/action.yml b/action.yml index dd4bef0..c89bd9b 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,8 @@ inputs: deno-version: description: The Deno version to install. Can be a semver version of a stable release, "canary" for the latest canary, or the Git hash of a specific canary release. default: "1.x" + deno-version-file: + description: File containing the Deno version to install such as .dvmrc or .tool-versions. Overridden by deno-version. outputs: deno-version: description: "The Deno version that was installed." diff --git a/main.js b/main.js index 9242d7d..08ceb39 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,11 @@ const process = require("process"); const core = require("@actions/core"); -const { parseVersionRange, resolveVersion } = require("./src/version.js"); +const { + parseVersionRange, + getDenoVersionFromFile, + resolveVersion, +} = require("./src/version.js"); const { install } = require("./src/install.js"); /** @@ -15,7 +19,11 @@ function exit(message) { async function main() { try { - const range = parseVersionRange(core.getInput("deno-version")); + const denoVersionFile = core.getInput("deno-version-file"); + const range = parseVersionRange( + core.getInput("deno-version") || getDenoVersionFromFile(denoVersionFile) + ); + if (range === null) { exit("The passed version range is not valid."); } @@ -26,9 +34,9 @@ async function main() { } core.info( - `Going to install ${ - version.isCanary ? "canary" : "stable" - } version ${version.version}.`, + `Going to install ${version.isCanary ? "canary" : "stable"} version ${ + version.version + }.` ); await install(version); diff --git a/src/version.js b/src/version.js index f053db0..7748ce6 100644 --- a/src/version.js +++ b/src/version.js @@ -1,5 +1,6 @@ const semver = require("semver"); const { fetch } = require("undici"); +const fs = require("fs"); const GIT_HASH_RE = /^[0-9a-fA-F]{40}$/; @@ -41,6 +42,26 @@ function parseVersionRange(version) { return null; } +/** + * Parses the version from the version file + * + * @param {string} versionFilePath + * @returns {string | undefined} + */ +function getDenoVersionFromFile(versionFilePath) { + if (!fs.existsSync(versionFilePath)) { + throw new Error( + `The specified node version file at: ${versionFilePath} does not exist` + ); + } + + const contents = fs.readFileSync(versionFilePath, "utf8"); + + const found = contents.match(/^(?:deno?\s+)?v?(?[^\s]+)$/m); + + return (found && found.groups && found.groups.version) || contents.trim(); +} + /** * @param {VersionRange} range * @returns {Promise} @@ -49,11 +70,11 @@ async function resolveVersion({ range, isCanary }) { if (isCanary) { if (range === "latest") { const res = await fetchWithRetries( - "https://dl.deno.land/canary-latest.txt", + "https://dl.deno.land/canary-latest.txt" ); if (res.status !== 200) { throw new Error( - "Failed to fetch canary version info from dl.deno.land. Please try again later.", + "Failed to fetch canary version info from dl.deno.land. Please try again later." ); } const version = (await res.text()).trim(); @@ -65,7 +86,7 @@ async function resolveVersion({ range, isCanary }) { const res = await fetchWithRetries("https://deno.com/versions.json"); if (res.status !== 200) { throw new Error( - "Failed to fetch stable version info from deno.com/versions.json. Please try again later.", + "Failed to fetch stable version info from deno.com/versions.json. Please try again later." ); } const versionJson = await res.json(); @@ -115,4 +136,5 @@ async function fetchWithRetries(url, maxRetries = 5) { module.exports = { parseVersionRange, resolveVersion, + getDenoVersionFromFile, };