Skip to content

Commit

Permalink
refactor: Promise to async (#24)
Browse files Browse the repository at this point in the history
* refactor: use onload and onerror
* refactor: Promise tp async await getLatestVersion()
* chore: run ncc
  • Loading branch information
peaceiris authored Dec 30, 2019
1 parent be86ec4 commit bbf6904
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
44 changes: 24 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,15 @@ function coerce (version, options) {

"use strict";

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
Expand Down Expand Up @@ -2878,21 +2887,21 @@ function getLatest(api, data) {
return latestVersion;
}
function getLatestVersion(org, repo, api) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url = getURL(org, repo, api);
xhr.open('GET', url);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url = getURL(org, repo, api);
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
const result = JSON.parse(xhr.responseText);
const latestVersion = getLatest(api, result);
resolve(latestVersion);
}
else if (xhr.readyState === 4 && xhr.status !== 200) {
};
xhr.onerror = () => {
reject(`ERROR: got status ${xhr.status} of ${url}`);
}
};
};
});
});
}
exports.default = getLatestVersion;
Expand Down Expand Up @@ -3564,15 +3573,10 @@ function run() {
try {
const mdbookVersion = core.getInput('mdbook-version');
if (mdbookVersion === '' || mdbookVersion === 'latest') {
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);
yield showVersion();
});
}, function (error) {
core.setFailed(error);
});
const latestVersion = yield get_latest_version_1.default('rust-lang', 'mdbook', 'brew');
console.log(`mdbook version: ${latestVersion} (${mdbookVersion})`);
yield installer_1.default(latestVersion);
yield showVersion();
}
else {
console.log(`mdbook version: ${mdbookVersion}`);
Expand Down
17 changes: 8 additions & 9 deletions src/get-latest-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getLatest(api: string, data: Json): string {
return latestVersion;
}

export default function getLatestVersion(
export default async function getLatestVersion(
org: string,
repo: string,
api: string
Expand All @@ -50,14 +50,13 @@ export default function getLatestVersion(
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 = 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}`);
}
xhr.onload = () => {
const result: Json = JSON.parse(xhr.responseText);
const latestVersion: string = getLatest(api, result);
resolve(latestVersion);
};
xhr.onerror = () => {
reject(`ERROR: got status ${xhr.status} of ${url}`);
};
});
}
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ async function run() {
const mdbookVersion: string = core.getInput('mdbook-version');

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

0 comments on commit bbf6904

Please sign in to comment.