Skip to content

Commit

Permalink
Improve code based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
szymmis committed Jul 27, 2023
1 parent 153d98e commit 5739c26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
34 changes: 16 additions & 18 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions lib/versions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import fs from "fs";
import fs from "fs/promises";
import * as core from "@actions/core";
import { HttpClient } from "@actions/http-client";

export async function determineVersion(repo, versionInput) {
versionInput = versionInput?.trim();

if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
}

if (!versionInput) {
let toolVersion = getVersionFromToolVersionsFile();
versionInput = toolVersion ?? (await fetchLatestTag(repo));
versionInput = toolVersion ?? "latest";
}

if (versionInput === "latest") {
versionInput = await fetchLatestTag(repo);
}

return versionInput.startsWith("v")
Expand Down Expand Up @@ -61,14 +61,12 @@ function fetchLatestTag(repo) {
);
}

function getVersionFromToolVersionsFile() {
const fileName = ".tool-versions";
if (!fs.existsSync(fileName)) return;

const toolVersions = fs.readFileSync(fileName, "utf-8").split("\n");
const scarbVersionInfo = toolVersions.find((tool) => tool.match(/^scarb/));
if (!scarbVersionInfo) return;

const [, version] = scarbVersionInfo.match(/scarb ([\w.-]+)/);
return version;
async function getVersionFromToolVersionsFile() {
try {
return (await fs.readFile(".tool-versions", "utf-8")).match(
/^scarb ([\w.-]+)/m,
)?.[1];
} catch (e) {
return;
}
}

0 comments on commit 5739c26

Please sign in to comment.