Skip to content

Commit

Permalink
Add .tool-versions and .dvmrc support
Browse files Browse the repository at this point in the history
  • Loading branch information
jessedijkstra committed Apr 11, 2024
1 parent 041b854 commit 355fe3a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
18 changes: 13 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
@@ -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");

/**
Expand All @@ -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.");
}
Expand All @@ -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);
Expand Down
28 changes: 25 additions & 3 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -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}$/;

Expand Down Expand Up @@ -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?(?<version>[^\s]+)$/m);

return (found && found.groups && found.groups.version) || contents.trim();
}

/**
* @param {VersionRange} range
* @returns {Promise<Version | null>}
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -115,4 +136,5 @@ async function fetchWithRetries(url, maxRetries = 5) {
module.exports = {
parseVersionRange,
resolveVersion,
getDenoVersionFromFile,
};

0 comments on commit 355fe3a

Please sign in to comment.