Skip to content

Commit

Permalink
refactor: Use node-fetch instead of xmlhttprequest (#130)
Browse files Browse the repository at this point in the history
* deps: Add node-fetch, remove xmlhttprequest
* refactor: Use node-fetch instead of xmlhttprequest
  • Loading branch information
peaceiris authored Jan 17, 2020
1 parent 3130d10 commit 4642226
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 48 deletions.
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
"@actions/exec": "^1.0.3",
"@actions/io": "^1.0.2",
"@actions/tool-cache": "^1.3.0",
"xmlhttprequest": "^1.8.0"
"node-fetch": "^2.6.0"
},
"devDependencies": {
"@types/jest": "^24.9.0",
"@types/node": "^13.1.7",
"@types/node-fetch": "^2.5.4",
"@typescript-eslint/parser": "^2.16.0",
"@zeit/ncc": "^0.21.0",
"eslint": "^6.8.0",
Expand Down
49 changes: 32 additions & 17 deletions src/get-latest-version.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
import fetch from 'node-fetch';

export default function getLatestVersion(): Promise<string> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url: string = 'https://formulae.brew.sh/api/formula/hugo.json';
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;
resolve(latestVersion);
} else if (xhr.readyState === 4 && xhr.status !== 200) {
reject(`ERROR: got status ${xhr.status} of ${url}`);
}
};
});
export 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`;
}

return 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;
}
}
79 changes: 55 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import getLatestVersion from './get-latest-version';
import installer from './installer';
import {getLatestVersion} from './get-latest-version';
import {installer} from './installer';

// most @actions toolkit packages have async methods
async function run() {
const showVersion = async () => {
await exec.exec('hugo version');
};
export interface actionResult {
exitcode: number;
output: string;
}

export async function showVersion(
cmd: string,
args: string[]
): Promise<actionResult> {
try {
const hugoVersion: string = core.getInput('hugo-version');

if (hugoVersion === '' || hugoVersion === 'latest') {
getLatestVersion().then(
async function(latestVersion): Promise<void> {
console.log(`Hugo version: ${latestVersion} (${hugoVersion})`);
await installer(latestVersion);
await showVersion();
},
function(error) {
core.setFailed(error);
let result: actionResult = {
exitcode: 0,
output: ''
};

const options = {
listeners: {
stdout: (data: Buffer) => {
result.output += data.toString();
}
);
}
};

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 {
console.log(`Hugo version: ${hugoVersion}`);
await installer(hugoVersion);
await showVersion();
installVersion = toolVersion;
}
} 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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (!tempDir) {
tempDir = path.join(baseTempLocation, 'tmp');
}

export default async function installer(version: string) {
export async function installer(version: string) {
try {
const extended: string = core.getInput('extended');
console.log(`Hugo extended: ${extended}`);
Expand Down

0 comments on commit 4642226

Please sign in to comment.