Skip to content

Commit

Permalink
refactor: Enhance getLatestVersion() (#22)
Browse files Browse the repository at this point in the history
* refactor: Split functions getURL and getLatest
* chore: 🤖 run ncc
  • Loading branch information
peaceiris authored Dec 30, 2019
1 parent c11d861 commit be86ec4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
43 changes: 39 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2840,18 +2840,53 @@ function coerce (version, options) {

"use strict";

var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const XMLHttpRequest = __webpack_require__(308).XMLHttpRequest;
function getLatestVersion() {
function getURL(org, repo, api) {
let url = '';
if (api === 'brew') {
url = `https://formulae.brew.sh/api/formula/${repo}.json`;
}
else if (api === 'github') {
url = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
}
else {
core.setFailed(`Source API ${api} is not supported.`);
}
return url;
}
function getLatest(api, data) {
let latestVersion = '';
if (api === 'brew') {
latestVersion = data.versions.stable;
}
else if (api === 'github') {
latestVersion = data.tag_name;
latestVersion = latestVersion.replace('v', '');
}
else {
core.setFailed(`Source API ${api} is not supported.`);
}
return latestVersion;
}
function getLatestVersion(org, repo, api) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url = 'https://formulae.brew.sh/api/formula/mdbook.json';
const url = getURL(org, repo, api);
xhr.open('GET', url);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
const result = JSON.parse(xhr.responseText);
const latestVersion = result.versions.stable;
const latestVersion = getLatest(api, result);
resolve(latestVersion);
}
else if (xhr.readyState === 4 && xhr.status !== 200) {
Expand Down Expand Up @@ -3529,7 +3564,7 @@ function run() {
try {
const mdbookVersion = core.getInput('mdbook-version');
if (mdbookVersion === '' || mdbookVersion === 'latest') {
get_latest_version_1.default().then(function (latestVersion) {
get_latest_version_1.default('rust-lang', 'mdbook', 'brew').then(function (latestVersion) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`mdbook version: ${latestVersion} (${mdbookVersion})`);
yield installer_1.default(latestVersion);
Expand Down
52 changes: 48 additions & 4 deletions src/get-latest-version.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
import * as core from '@actions/core';

interface BrewVersions {
stable: string;
}

interface Json {
tag_name: string;
versions: BrewVersions;
}

const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

export default function getLatestVersion(): Promise<string> {
function getURL(org: string, repo: string, api: string): string {
let url: string = '';

if (api === 'brew') {
url = `https://formulae.brew.sh/api/formula/${repo}.json`;
} else if (api === 'github') {
url = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
} else {
core.setFailed(`Source API ${api} is not supported.`);
}

return url;
}

function getLatest(api: string, data: Json): string {
let latestVersion: string = '';

if (api === 'brew') {
latestVersion = data.versions.stable;
} else if (api === 'github') {
latestVersion = data.tag_name;
latestVersion = latestVersion.replace('v', '');
} else {
core.setFailed(`Source API ${api} is not supported.`);
}

return latestVersion;
}

export default function getLatestVersion(
org: string,
repo: string,
api: string
): Promise<string> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url: string = 'https://formulae.brew.sh/api/formula/mdbook.json';
const url = getURL(org, repo, api);
xhr.open('GET', url);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const result = JSON.parse(xhr.responseText);
const latestVersion: string = result.versions.stable;
const result: Json = JSON.parse(xhr.responseText);
const latestVersion: string = getLatest(api, result);
resolve(latestVersion);
} else if (xhr.readyState === 4 && xhr.status !== 200) {
reject(`ERROR: got status ${xhr.status} of ${url}`);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function run() {
const mdbookVersion: string = core.getInput('mdbook-version');

if (mdbookVersion === '' || mdbookVersion === 'latest') {
getLatestVersion().then(
getLatestVersion('rust-lang', 'mdbook', 'brew').then(
async function(latestVersion): Promise<void> {
console.log(`mdbook version: ${latestVersion} (${mdbookVersion})`);
await installer(latestVersion);
Expand Down

0 comments on commit be86ec4

Please sign in to comment.