refactor: Use node-fetch instead of xmlhttprequest (#130)

* deps: Add node-fetch, remove xmlhttprequest
* refactor: Use node-fetch instead of xmlhttprequest
This commit is contained in:
Shohei Ueda 2020-01-18 04:36:10 +09:00 committed by GitHub
parent 3130d100df
commit 4642226db0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 47 deletions

19
package-lock.json generated
View File

@ -594,6 +594,15 @@
"integrity": "sha512-HU0q9GXazqiKwviVxg9SI/+t/nAsGkvLDkIdxz+ObejG2nX6Si00TeLqHMoS+a/1tjH7a8YpKVQwtgHuMQsldg==", "integrity": "sha512-HU0q9GXazqiKwviVxg9SI/+t/nAsGkvLDkIdxz+ObejG2nX6Si00TeLqHMoS+a/1tjH7a8YpKVQwtgHuMQsldg==",
"dev": true "dev": true
}, },
"@types/node-fetch": {
"version": "2.5.4",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.4.tgz",
"integrity": "sha512-Oz6id++2qAOFuOlE1j0ouk1dzl3mmI1+qINPNBhi9nt/gVOz0G+13Ao6qjhdF0Ys+eOkhu6JnFmt38bR3H0POQ==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/parse-json": { "@types/parse-json": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz",
@ -6216,6 +6225,11 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true "dev": true
}, },
"node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
},
"node-int64": { "node-int64": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz",
@ -8262,11 +8276,6 @@
"integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==", "integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==",
"dev": true "dev": true
}, },
"xmlhttprequest": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz",
"integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw="
},
"xtend": { "xtend": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",

View File

@ -52,11 +52,12 @@
"@actions/exec": "^1.0.3", "@actions/exec": "^1.0.3",
"@actions/io": "^1.0.2", "@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.3.0", "@actions/tool-cache": "^1.3.0",
"xmlhttprequest": "^1.8.0" "node-fetch": "^2.6.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^24.9.0", "@types/jest": "^24.9.0",
"@types/node": "^13.1.7", "@types/node": "^13.1.7",
"@types/node-fetch": "^2.5.4",
"@typescript-eslint/parser": "^2.16.0", "@typescript-eslint/parser": "^2.16.0",
"@zeit/ncc": "^0.21.0", "@zeit/ncc": "^0.21.0",
"eslint": "^6.8.0", "eslint": "^6.8.0",

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 {
let result: actionResult = {
exitcode: 0,
output: ''
}; };
try { const options = {
const hugoVersion: string = core.getInput('hugo-version'); listeners: {
stdout: (data: Buffer) => {
result.output += data.toString();
}
}
};
if (hugoVersion === '' || hugoVersion === 'latest') { result.exitcode = await exec.exec(cmd, args, options);
getLatestVersion().then( core.debug(`
async function(latestVersion): Promise<void> { exit code: ${result.exitcode}
console.log(`Hugo version: ${latestVersion} (${hugoVersion})`); stdout: ${result.output}
await installer(latestVersion); `);
await showVersion(); return result;
}, } catch (e) {
function(error) { return e;
core.setFailed(error);
} }
); }
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}`);