From bbf6904d6786d5ca09e440147b2434a990d88d83 Mon Sep 17 00:00:00 2001 From: Shohei Ueda <30958501+peaceiris@users.noreply.github.com> Date: Mon, 30 Dec 2019 23:24:02 +0900 Subject: [PATCH] refactor: Promise to async (#24) * refactor: use onload and onerror * refactor: Promise tp async await getLatestVersion() * chore: run ncc --- lib/index.js | 44 +++++++++++++++++++++------------------ src/get-latest-version.ts | 17 +++++++-------- src/index.ts | 16 +++++++------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/lib/index.js b/lib/index.js index dc09288..5841e0f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 = {}; @@ -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; @@ -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}`); diff --git a/src/get-latest-version.ts b/src/get-latest-version.ts index 52a77ca..77a1004 100644 --- a/src/get-latest-version.ts +++ b/src/get-latest-version.ts @@ -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 @@ -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}`); }; }); } diff --git a/src/index.ts b/src/index.ts index 3332b21..c838c17 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { - 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);