forked from jason-dour/action-setup-hugo-preproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (75 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require("fs");
const os = require("os");
const path = require("path");
const core = require("@actions/core");
const gh = require("@actions/github");
const tc = require("@actions/tool-cache");
// Grab the OS and Arch.
const myPlat = os.platform();
const myArch = os.arch();
// Leverage the GitHub Action environment variables to authenticate with GitHub
const octokit = new gh.getOctokit(process.env.GITHUB_TOKEN);
// getRelease returns the octokit release object for the given version
async function getRelease(version) {
var release;
try {
if (version === "latest") {
release = await octokit.rest.repos.getLatestRelease({
owner: "jason-dour",
repo: "hugo-preproc",
});
} else {
release = await octokit.rest.repos.getReleaseByTag({
owner: "jason-dour",
repo: "hugo-preproc",
tag: version,
});
}
} catch (e) {
core.setFailed(e);
}
return release
}
// getDownloadObject returns an object with the following properties:
// url: the url to download the tool from
async function getDownloadObject(version) {
const release = await getRelease(version);
core.debug("release: " + release.data.name);
const asset = release.data.assets.find((asset) =>
asset.name.includes(
`_${myPlat}_${myArch}`
)
);
core.debug("asset: " + asset)
const url = asset.browser_download_url;
core.info("url: " + url);
return { url };
}
// setup downloads the tool and installs it to the given path
async function setup() {
try {
// Get version of tool to be installed
const version = core.getInput("hugo-preproc-version");
core.debug("version: " + version);
// Download the specific version of the tool.
const download = await getDownloadObject(version);
var exeName = "";
if (myPlat === "win32") {
exeName = path.join(process.env.RUNNER_TEMP,"hugo-preproc.exe");
} else {
exeName = path.join(process.env.RUNNER_TEMP,"hugo-preproc");
}
const pathToCLI = await tc.downloadTool(
download.url,
exeName
);
fs.chmodSync(pathToCLI, 0o755); // make the binary executable
core.debug("pathToCLI: " + pathToCLI);
// Expose the tool by adding it to the PATH
core.addPath(process.env.RUNNER_TEMP);
} catch (e) {
core.setFailed(e);
}
}
export default setup;
setup();