|
1 | 1 | const core = require("@actions/core");
|
| 2 | +const github = require("@actions/github"); |
2 | 3 | const tc = require("@actions/tool-cache");
|
3 | 4 |
|
| 5 | +const owner = "koki-develop"; |
| 6 | +const repo = "qiita-cli"; |
| 7 | + |
4 | 8 | (async () => {
|
5 |
| - const version = (() => { |
6 |
| - const version = core.getInput("version"); |
7 |
| - if (!version.startsWith("v")) { |
8 |
| - return `v${version}`; |
| 9 | + const version = await _getVersion(core.getInput("version")); |
| 10 | + const cliPath = await _download(version); |
| 11 | + await _install(cliPath); |
| 12 | +})().catch((err) => { |
| 13 | + core.setFailed(err.message); |
| 14 | +}); |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {String} path |
| 18 | + * @returns {Promise<String>} |
| 19 | + */ |
| 20 | +const _install = async (path) => { |
| 21 | + core.info("Installing..."); |
| 22 | + |
| 23 | + const extractedPath = await (async () => { |
| 24 | + switch (process.platform) { |
| 25 | + case "win32": |
| 26 | + return tc.extractZip(path); |
| 27 | + case "tar.gz": |
| 28 | + return tc.extractTar(path); |
9 | 29 | }
|
10 | 30 | })();
|
| 31 | + const binPath = await tc.cacheDir(extractedPath, "qiita", version); |
| 32 | + core.addPath(binPath); |
| 33 | + |
| 34 | + core.info(`Installed to ${binPath}`); |
| 35 | + return binPath; |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * @param {String} version |
| 40 | + * @returns {Promise<String>} |
| 41 | + */ |
| 42 | +const _getVersion = async (version) => { |
| 43 | + const token = core.getInput("github-token"); |
| 44 | + const octo = github.getOctokit(token); |
| 45 | + |
| 46 | + let release; |
| 47 | + if (version === "latest") { |
| 48 | + release = await octo.repos.getLatestRelease({ owner, repo }); |
| 49 | + } else { |
| 50 | + if (!version.startsWith("v")) { |
| 51 | + version = `v${version}`; |
| 52 | + } |
| 53 | + release = await octo.repos.getReleaseByTag({ owner, repo, tag: version }); |
| 54 | + } |
| 55 | + |
| 56 | + const version = release.data.tag_name; |
11 | 57 | core.info(`version: ${version}`);
|
12 | 58 |
|
| 59 | + return version; |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * @param {String} version |
| 64 | + * @returns {Promise<String>} |
| 65 | + */ |
| 66 | +const _download = async (version) => { |
13 | 67 | core.info(`platform: ${process.platform}`);
|
14 | 68 | core.info(`arch: ${process.arch}`);
|
15 | 69 |
|
@@ -40,31 +94,19 @@ const tc = require("@actions/tool-cache");
|
40 | 94 | })();
|
41 | 95 |
|
42 | 96 | const ext = (() => {
|
43 |
| - switch (platform) { |
44 |
| - case "Windows": |
| 97 | + switch (process.platform) { |
| 98 | + case "win32": |
45 | 99 | return "zip";
|
46 | 100 | default:
|
47 | 101 | return "tar.gz";
|
48 | 102 | }
|
49 | 103 | })();
|
50 | 104 |
|
51 |
| - const url = `https://github.com/koki-develop/qiita-cli/releases/download/${version}/qiita_${platform}_${arch}.${ext}`; |
| 105 | + const url = `https://github.com/${owner}/${repo}/releases/download/${version}/qiita_${platform}_${arch}.${ext}`; |
| 106 | + |
52 | 107 | core.info(`Downloading... ${url}`);
|
53 | 108 | const cliPath = await tc.downloadTool(url);
|
54 | 109 | core.info(`Downloaded to ${cliPath}`);
|
55 | 110 |
|
56 |
| - core.info("Installing..."); |
57 |
| - const extractedPath = await (async () => { |
58 |
| - switch (ext) { |
59 |
| - case "zip": |
60 |
| - return tc.extractZip(cliPath); |
61 |
| - case "tar.gz": |
62 |
| - return tc.extractTar(cliPath); |
63 |
| - } |
64 |
| - })(); |
65 |
| - const binPath = await tc.cacheDir(extractedPath, "qiita", version); |
66 |
| - core.addPath(binPath); |
67 |
| - core.info(`Installed to ${binPath}`); |
68 |
| -})().catch((err) => { |
69 |
| - core.setFailed(err.message); |
70 |
| -}); |
| 111 | + return cliPath; |
| 112 | +}; |
0 commit comments