refactor: Use node-fetch instead of xmlhttprequest

This commit is contained in:
peaceiris 2020-01-17 18:59:21 +00:00
parent 5cef9f1b40
commit 123c3e8fc5
3 changed files with 87 additions and 41 deletions

View file

@ -1,19 +1,34 @@
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; import fetch from 'node-fetch';
export default function getLatestVersion(): Promise<string> { export function getURL(org: string, repo: string, api: string): string {
return new Promise((resolve, reject) => { let url: string = '';
const xhr = new XMLHttpRequest();
const url: string = 'https://formulae.brew.sh/api/formula/hugo.json'; if (api === 'brew') {
xhr.open('GET', url); url = `https://formulae.brew.sh/api/formula/${repo}.json`;
xhr.send(); } else if (api === 'github') {
xhr.onreadystatechange = function() { url = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
if (xhr.readyState === 4 && xhr.status === 200) { }
const result = JSON.parse(xhr.responseText);
const latestVersion: string = result.versions.stable; return url;
resolve(latestVersion); }
} else if (xhr.readyState === 4 && xhr.status !== 200) {
reject(`ERROR: got status ${xhr.status} of ${url}`); export async function getLatestVersion(
} org: string,
}; repo: string,
}); api: string
): Promise<string> {
try {
const url = getURL(org, repo, api);
const response = await fetch(url);
const json = await response.json();
let latestVersion: string = '';
if (api === 'brew') {
latestVersion = json.versions.stable;
} else if (api === 'github') {
latestVersion = json.tag_name;
}
return latestVersion;
} catch (e) {
return e;
}
} }

View file

@ -1,35 +1,66 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import getLatestVersion from './get-latest-version'; import {getLatestVersion} from './get-latest-version';
import installer from './installer'; import {installer} from './installer';
// most @actions toolkit packages have async methods export interface actionResult {
async function run() { exitcode: number;
const showVersion = async () => { output: string;
await exec.exec('hugo version'); }
};
export async function showVersion(
cmd: string,
args: string[]
): Promise<actionResult> {
try { try {
const hugoVersion: string = core.getInput('hugo-version'); let result: actionResult = {
exitcode: 0,
output: ''
};
if (hugoVersion === '' || hugoVersion === 'latest') { const options = {
getLatestVersion().then( listeners: {
async function(latestVersion): Promise<void> { stdout: (data: Buffer) => {
console.log(`Hugo version: ${latestVersion} (${hugoVersion})`); result.output += data.toString();
await installer(latestVersion);
await showVersion();
},
function(error) {
core.setFailed(error);
} }
); }
};
result.exitcode = await exec.exec(cmd, args, options);
core.debug(`
exit code: ${result.exitcode}
stdout: ${result.output}
`);
return result;
} catch (e) {
return e;
}
}
async function run() {
try {
const toolVersion: string = core.getInput('hugo-version');
let installVersion: string = '';
let result: actionResult = {
exitcode: 0,
output: ''
};
if (toolVersion === '' || toolVersion === 'latest') {
installVersion = await getLatestVersion('gohugoio', 'hugo', 'brew');
} else { } else {
console.log(`Hugo version: ${hugoVersion}`); installVersion = toolVersion;
await installer(hugoVersion);
await showVersion();
} }
} catch (error) {
core.setFailed(error.message); core.info(`hugo version: ${installVersion}`);
await installer(installVersion);
result = await showVersion('hugo', ['version']);
return result;
} catch (e) {
core.setFailed(`Action failed with error ${e}`);
return e;
} }
} }

View file

@ -16,7 +16,7 @@ if (!tempDir) {
tempDir = path.join(baseTempLocation, 'tmp'); tempDir = path.join(baseTempLocation, 'tmp');
} }
export default async function installer(version: string) { export async function installer(version: string) {
try { try {
const extended: string = core.getInput('extended'); const extended: string = core.getInput('extended');
console.log(`Hugo extended: ${extended}`); console.log(`Hugo extended: ${extended}`);