feat: Migrate JavaScript to TypeScript
This commit is contained in:
parent
b3cddbe7fa
commit
7b1d8d3b9b
17 changed files with 585 additions and 142 deletions
19
src/get-latest-version.ts
Normal file
19
src/get-latest-version.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
|
||||
|
||||
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}`);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
12
src/get-os.ts
Normal file
12
src/get-os.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
export default function getOS(platform: string) {
|
||||
if (platform === "linux") {
|
||||
return "Linux";
|
||||
} else if (platform === "darwin") {
|
||||
return "macOS";
|
||||
} else if (platform === "win32") {
|
||||
return "Windows";
|
||||
// throw new Error("Windows is not supported");
|
||||
} else {
|
||||
throw new Error(`${platform} is not supported`);
|
||||
}
|
||||
}
|
||||
31
src/get-url.ts
Normal file
31
src/get-url.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
export default function getURL(
|
||||
os: string,
|
||||
extended: string,
|
||||
version: string
|
||||
): string {
|
||||
const extendedStr = (extended: string) => {
|
||||
if (extended === "true") {
|
||||
return "extended_";
|
||||
} else {
|
||||
return "";
|
||||
// } else {
|
||||
// throw new Error(`Invalid input (extended): ${extended}`);
|
||||
}
|
||||
};
|
||||
|
||||
const ext = (os: string) => {
|
||||
if (os === "Windows") {
|
||||
return "zip";
|
||||
} else {
|
||||
return "tar.gz";
|
||||
}
|
||||
};
|
||||
|
||||
const hugoName: string = `hugo_${extendedStr(
|
||||
extended
|
||||
)}${version}_${os}-64bit`;
|
||||
const baseURL: string = "https://github.com/gohugoio/hugo/releases/download";
|
||||
const url: string = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
|
||||
|
||||
return url;
|
||||
}
|
||||
37
src/index.ts
Normal file
37
src/index.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as exec from "@actions/exec";
|
||||
import getLatestVersion from "./get-latest-version";
|
||||
import installer from "./installer";
|
||||
|
||||
// most @actions toolkit packages have async methods
|
||||
async function run() {
|
||||
try {
|
||||
getLatestVersion().then(
|
||||
async function(latestVersion): Promise<any> {
|
||||
const hugoVersion: string = core.getInput("hugo-version");
|
||||
console.log(`Hugo version: ${hugoVersion}`);
|
||||
const version = (v: string, latestVersion: string): string => {
|
||||
if (v === "" || v === "latest") {
|
||||
return latestVersion;
|
||||
} else {
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
await installer(version(hugoVersion, latestVersion));
|
||||
|
||||
// Show version
|
||||
await exec.exec("hugo version");
|
||||
await exec.exec("go version");
|
||||
await exec.exec("git --version");
|
||||
},
|
||||
function(error) {
|
||||
core.setFailed(error);
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
42
src/installer.ts
Normal file
42
src/installer.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as tc from "@actions/tool-cache";
|
||||
import * as io from "@actions/io";
|
||||
import getOS from "./get-os";
|
||||
import getURL from "./get-url";
|
||||
|
||||
export default async function installer(version: string) {
|
||||
try {
|
||||
const extended: string = core.getInput("extended");
|
||||
console.log(`Hugo extended: ${extended}`);
|
||||
|
||||
const osName: string = getOS(process.platform);
|
||||
console.log(`Operating System: ${osName}`);
|
||||
|
||||
const hugoURL: string = getURL(osName, extended, version);
|
||||
core.debug(`hugoURL: ${hugoURL}`);
|
||||
|
||||
const hugoPath: string = `${process.env.HOME}/bin`;
|
||||
await io.mkdirP(hugoPath);
|
||||
core.addPath(hugoPath);
|
||||
|
||||
// Download and extract Hugo binary
|
||||
const hugoAssets: string = await tc.downloadTool(hugoURL);
|
||||
let hugoBin: string = "";
|
||||
if (osName === "Windows") {
|
||||
const hugoExtractedFolder: string = await tc.extractZip(
|
||||
hugoAssets,
|
||||
"/tmp"
|
||||
);
|
||||
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
|
||||
} else {
|
||||
const hugoExtractedFolder: string = await tc.extractTar(
|
||||
hugoAssets,
|
||||
"/tmp"
|
||||
);
|
||||
hugoBin = `${hugoExtractedFolder}/hugo`;
|
||||
}
|
||||
await io.mv(hugoBin, hugoPath);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue