fix version detecting

This commit is contained in:
CodeHz
2020-03-20 15:44:09 +08:00
Unverified
parent 2a9e6f8016
commit fe8512df8d
2 changed files with 57 additions and 30 deletions
+13 -10
View File
@@ -2,26 +2,29 @@
Object.defineProperty(exports, "__esModule", { value: true });
const core = require("@actions/core");
const semver = require("semver");
const tool_cache_1 = require("@actions/tool-cache");
const _fs = require("fs");
const fs = _fs.promises;
async function fetchVersions() {
const file = await tool_cache_1.downloadTool('https://raw.githubusercontent.com/xmake-io/github-action-setup-xmake/data/versions.json');
const versions = await fs.readFile(file, 'utf-8');
return JSON.parse(versions);
const tags = await (await fetch("https://api.github.com/repos/xmake-io/xmake/git/refs/tags")).json();
return tags.map(({ ref, object: { sha } }) => [ref.slice(11), sha]).reduce((o, [k, v]) => {
o[k] = v;
return o;
}, {});
}
exports.fetchVersions = fetchVersions;
async function selectVersion(version) {
version = version || core.getInput('xmake-version') || 'latest';
if (version.toLowerCase() === 'latest')
version = '';
version = version || core.getInput("xmake-version") || "latest";
if (version.toLowerCase() === "latest")
version = "";
version = semver.validRange(version);
if (!version)
throw new Error(`Invalid input xmake-version: ${core.getInput('xmake-version')}`);
if (!version) {
throw new Error(`Invalid input xmake-version: ${core.getInput("xmake-version")}`);
}
const versions = await fetchVersions();
const ver = semver.maxSatisfying(Object.keys(versions), version);
if (!ver)
if (!ver) {
throw new Error(`No matched releases of xmake-version: ${version}`);
}
const sha = versions[ver];
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`);
return { version: ver, sha };
+43 -19
View File
@@ -1,28 +1,52 @@
import * as core from '@actions/core'
import * as semver from 'semver'
import { downloadTool } from '@actions/tool-cache'
import * as _fs from 'fs'
const fs = _fs.promises
import * as core from "@actions/core";
import * as semver from "semver";
import { downloadTool } from "@actions/tool-cache";
import * as _fs from "fs";
const fs = _fs.promises;
type VersionMap = { [v: string]: string }
type TagItem = {
ref: string;
node_id: string;
url: string;
object: {
sha: string;
type: string;
url: string;
};
};
export async function fetchVersions() {
const file = await downloadTool('https://raw.githubusercontent.com/xmake-io/github-action-setup-xmake/data/versions.json')
const versions = await fs.readFile(file, 'utf-8')
return JSON.parse(versions) as VersionMap
const tags: [TagItem] = await (await fetch(
"https://api.github.com/repos/xmake-io/xmake/git/refs/tags"
)).json();
return tags.map(({ ref, object: {sha} }) => [ref.slice(11), sha]).reduce(
(o, [k, v]) => {
o[k] = v;
return o;
},
{} as Record<string, string>
);
}
export async function selectVersion(version?: string) {
version = version || core.getInput('xmake-version') || 'latest'
if (version.toLowerCase() === 'latest') version = ''
version = semver.validRange(version)
if (!version) throw new Error(`Invalid input xmake-version: ${core.getInput('xmake-version')}`)
version = version || core.getInput("xmake-version") || "latest";
if (version.toLowerCase() === "latest") version = "";
version = semver.validRange(version);
if (!version) {
throw new Error(
`Invalid input xmake-version: ${core.getInput("xmake-version")}`
);
}
const versions = await fetchVersions()
const ver = semver.maxSatisfying(Object.keys(versions), version)
if (!ver) throw new Error(`No matched releases of xmake-version: ${version}`)
const versions = await fetchVersions();
const ver = semver.maxSatisfying(Object.keys(versions), version);
if (!ver) {
throw new Error(
`No matched releases of xmake-version: ${version}`
);
}
const sha = versions[ver]
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`)
return { version: ver, sha }
const sha = versions[ver];
core.info(`selected xmake v${ver} (commit: ${sha.substr(0, 8)})`);
return { version: ver, sha };
}