feat: add .tool-versions and .dvmrc support (#61)

---------

Signed-off-by: Jesse Dijkstra <mail@jessedijkstra.nl>
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Jesse Dijkstra
2024-07-05 08:33:22 +02:00
committed by GitHub
parent 041b854f97
commit edde9366ea
7 changed files with 84 additions and 3 deletions

View File

@@ -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,35 @@ 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");
// .tool-versions typically looks like
// ```
// ruby 2.6.5
// deno 1.43.1
// node 20.0.0
// ```
// This parses the version of Deno from the file
const denoVersionInToolVersions = contents.match(
/^deno\s+v?(?<version>[^\s]+)$/m,
);
return denoVersionInToolVersions?.groups?.version || contents.trim();
}
/**
* @param {VersionRange} range
* @returns {Promise<Version | null>}
@@ -115,4 +145,5 @@ async function fetchWithRetries(url, maxRetries = 5) {
module.exports = {
parseVersionRange,
resolveVersion,
getDenoVersionFromFile,
};