Add .tool-versions and .dvmrc support

This commit is contained in:
Jesse Dijkstra 2024-04-11 14:15:36 +02:00
parent 041b854f97
commit 355fe3a070
4 changed files with 54 additions and 8 deletions

View File

@ -4,6 +4,20 @@ Set up your GitHub Actions workflow with a specific version of Deno.
## Usage ## 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 ### Latest stable for a major
```yaml ```yaml

View File

@ -8,6 +8,8 @@ inputs:
deno-version: 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. 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" 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: outputs:
deno-version: deno-version:
description: "The Deno version that was installed." description: "The Deno version that was installed."

18
main.js
View File

@ -1,7 +1,11 @@
const process = require("process"); const process = require("process");
const core = require("@actions/core"); 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"); const { install } = require("./src/install.js");
/** /**
@ -15,7 +19,11 @@ function exit(message) {
async function main() { async function main() {
try { 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) { if (range === null) {
exit("The passed version range is not valid."); exit("The passed version range is not valid.");
} }
@ -26,9 +34,9 @@ async function main() {
} }
core.info( core.info(
`Going to install ${ `Going to install ${version.isCanary ? "canary" : "stable"} version ${
version.isCanary ? "canary" : "stable" version.version
} version ${version.version}.`, }.`
); );
await install(version); await install(version);

View File

@ -1,5 +1,6 @@
const semver = require("semver"); const semver = require("semver");
const { fetch } = require("undici"); const { fetch } = require("undici");
const fs = require("fs");
const GIT_HASH_RE = /^[0-9a-fA-F]{40}$/; const GIT_HASH_RE = /^[0-9a-fA-F]{40}$/;
@ -41,6 +42,26 @@ function parseVersionRange(version) {
return null; 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?(?<version>[^\s]+)$/m);
return (found && found.groups && found.groups.version) || contents.trim();
}
/** /**
* @param {VersionRange} range * @param {VersionRange} range
* @returns {Promise<Version | null>} * @returns {Promise<Version | null>}
@ -49,11 +70,11 @@ async function resolveVersion({ range, isCanary }) {
if (isCanary) { if (isCanary) {
if (range === "latest") { if (range === "latest") {
const res = await fetchWithRetries( const res = await fetchWithRetries(
"https://dl.deno.land/canary-latest.txt", "https://dl.deno.land/canary-latest.txt"
); );
if (res.status !== 200) { if (res.status !== 200) {
throw new Error( 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(); const version = (await res.text()).trim();
@ -65,7 +86,7 @@ async function resolveVersion({ range, isCanary }) {
const res = await fetchWithRetries("https://deno.com/versions.json"); const res = await fetchWithRetries("https://deno.com/versions.json");
if (res.status !== 200) { if (res.status !== 200) {
throw new Error( 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(); const versionJson = await res.json();
@ -115,4 +136,5 @@ async function fetchWithRetries(url, maxRetries = 5) {
module.exports = { module.exports = {
parseVersionRange, parseVersionRange,
resolveVersion, resolveVersion,
getDenoVersionFromFile,
}; };