From 6a842e30b0058d71c936acc3006bbdf8a706a36f Mon Sep 17 00:00:00 2001 From: Yohe-Am <56622350+Yohe-Am@users.noreply.github.com> Date: Fri, 15 Dec 2023 12:03:54 +0000 Subject: [PATCH] fix: fix version fetching bug --- .github/workflows/ci.yml | 1 + index.js | 132 ++++++++++++++++++++------------------- index.ts | 2 +- package.json | 2 +- 4 files changed, 70 insertions(+), 67 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15bdad8..6a2762c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,7 @@ jobs: id: test-action uses: ./ env: + # FIXME: once #14@ghjk lands GHJK_VERSION: 6040bb3 - name: Check avail of pnpm diff --git a/index.js b/index.js index 275ee3b..2e8ac53 100644 --- a/index.js +++ b/index.js @@ -81701,10 +81701,10 @@ const os = __importStar(__nccwpck_require__(2037)); const node_fetch_1 = __importDefault(__nccwpck_require__(467)); const crypto_1 = __importDefault(__nccwpck_require__(6113)); // TODO: auto-manage these versions -const DENO_VERSION = '1.39.0'; +const DENO_VERSION = "1.39.0"; async function latestGhjkVersion() { const resp = await (0, node_fetch_1.default)(`https://api.github.com/repos/metatypedev/ghjk/releases/latest`); - if (resp.ok) { + if (!resp.ok) { throw new Error(`error fetching latest ghjk release meta: ${resp.statusText}`); } const meta = (await resp.json()); @@ -81716,73 +81716,75 @@ async function latestGhjkVersion() { */ async function main() { try { - const inputVersion = core.getInput('version'); - const inputInstallerUrl = core.getInput('installer-url'); - const inputSync = core.getInput('sync'); - const inputSkipDenoInstall = core.getInput('skip-deno-install'); - const inputCacheDisable = core.getInput('cache-disable'); - const inputCacheKeyPrefix = core.getInput('cache-key-prefix'); - const inputCacheSaveIf = core.getInput('cache-save-if'); - const inputCacheKeyEnvVars = core.getInput('cache-key-env-vars'); + const inputVersion = core.getInput("version"); + const inputInstallerUrl = core.getInput("installer-url"); + const inputSync = core.getInput("sync"); + const inputSkipDenoInstall = core.getInput("skip-deno-install"); + const inputCacheDisable = core.getInput("cache-disable"); + const inputCacheKeyPrefix = core.getInput("cache-key-prefix"); + const inputCacheSaveIf = core.getInput("cache-save-if"); + const inputCacheKeyEnvVars = core.getInput("cache-key-env-vars"); const version = inputVersion.length > 0 ? inputVersion - : process.env['GHJK_VERSION'] ?? (await latestGhjkVersion()); + : process.env["GHJK_VERSION"] ?? (await latestGhjkVersion()); const installerUrl = inputInstallerUrl.length > 0 ? inputInstallerUrl : `https://raw.github.com/metatypedev/ghjk/${version}/install.ts`; - const execDir = await installGhjk(version, installerUrl, inputSkipDenoInstall === 'true'); + const execDir = await installGhjk(version, installerUrl, inputSkipDenoInstall === "true"); core.addPath(execDir); - const configStr = (await exec.getExecOutput('ghjk', ['print', 'config'])) + const configStr = (await exec.getExecOutput("ghjk", ["print", "config"])) .stdout; - const ghjkDir = (await exec.getExecOutput('ghjk', ['print', 'ghjk-dir-path'], { - silent: true + const ghjkDir = (await exec.getExecOutput("ghjk", ["print", "ghjk-dir-path"], { + silent: true, })).stdout.trim(); - if (inputCacheDisable === 'false' && cache.isFeatureAvailable()) { - const ghjkVersion = (await exec.getExecOutput('ghjk', ['--version'], { silent: true })).stdout.trim(); - const configPath = (await exec.getExecOutput('ghjk', ['print', 'config-path'], { - silent: true + if (inputCacheDisable === "false" && cache.isFeatureAvailable()) { + const ghjkVersion = (await exec.getExecOutput("ghjk", ["--version"], { silent: true })).stdout.trim(); + const configPath = (await exec.getExecOutput("ghjk", ["print", "config-path"], { + silent: true, })).stdout.trim(); - const hasher = crypto_1.default.createHash('sha1'); + const hasher = crypto_1.default.createHash("sha1"); hasher.update(ghjkVersion); hasher.update(configPath); // TODO: consider ignoring config to avoid misses just for one dep change hasher.update(configStr); const hashedEnvs = [ - 'GHJK', - 'DENO', + "GHJK", + "DENO", ...inputCacheKeyEnvVars - .split(',') - .filter(str => str.length > 0) - .map(str => str.trim()) + .split(",") + .filter((str) => str.length > 0) + .map((str) => str.trim()), ]; for (const [key, val] of Object.entries(process.env)) { - if (hashedEnvs.some(pfix => key.startsWith(pfix))) { + if (hashedEnvs.some((pfix) => key.startsWith(pfix))) { hasher.update(`${key}=${val}`); } } - const hash = hasher.digest('hex'); - const keyPrefix = inputCacheKeyPrefix.length > 0 ? inputCacheKeyPrefix : 'v0-ghjk'; + const hash = hasher.digest("hex"); + const keyPrefix = inputCacheKeyPrefix.length > 0 + ? inputCacheKeyPrefix + : "v0-ghjk"; const key = `${keyPrefix}-${hash}`; - const envsDir = core.toPlatformPath(path.resolve(ghjkDir, 'envs')); + const envsDir = core.toPlatformPath(path.resolve(ghjkDir, "envs")); const cacheDirs = [envsDir]; core.info(JSON.stringify({ cacheDirs, envsDir, ghjkDir })); // NOTE: restoreCache modifies the array it's given for some reason await cache.restoreCache([...cacheDirs], key); - if (inputCacheSaveIf === 'true') { + if (inputCacheSaveIf === "true") { core.info(`enabling cache with key ${key}: [${cacheDirs}]`); - core.saveState('ghjk-cache-save', true); - core.saveState('ghjk-post-args', { + core.saveState("ghjk-cache-save", true); + core.saveState("ghjk-post-args", { key, - cacheDirs + cacheDirs, }); } } - if (inputSync === 'true') { - await exec.exec('ghjk', ['ports', 'sync']); + if (inputSync === "true") { + await exec.exec("ghjk", ["ports", "sync"]); } - core.setOutput('GHJK_DIR', ghjkDir); - core.exportVariable('GHJK_DIR', ghjkDir); - core.exportVariable('BASH_ENV', `${ghjkDir}/env.sh`); + core.setOutput("GHJK_DIR", ghjkDir); + core.exportVariable("GHJK_DIR", ghjkDir); + core.exportVariable("BASH_ENV", `${ghjkDir}/env.sh`); } catch (error) { // Fail the workflow run if an error occurs @@ -81792,18 +81794,18 @@ async function main() { } exports.main = main; async function installGhjk(version, installerUrl, skipDenoInstall) { - let denoExec = 'deno'; - if (skipDenoInstall && !process.env['GHJK_INSTALL_DENO_EXEC']) { - const denoOut = await exec.getExecOutput('deno', ['--version']); + let denoExec = "deno"; + if (skipDenoInstall && !process.env["GHJK_INSTALL_DENO_EXEC"]) { + const denoOut = await exec.getExecOutput("deno", ["--version"]); if (denoOut.exitCode !== 0) { - throw new Error('skip-deno-install set but no deno binary found'); + throw new Error("skip-deno-install set but no deno binary found"); } core.debug(`skipping deno install & using found "deno" bin`); } else { - denoExec = await installDeno(process.env['DENO_VERSION'] ?? DENO_VERSION); + denoExec = await installDeno(process.env["DENO_VERSION"] ?? DENO_VERSION); } - const foundExecDir = tc.find('ghjk', version); + const foundExecDir = tc.find("ghjk", version); if (foundExecDir.length !== 0) { core.debug(`found cached ghjk tool under version ${version}: ${foundExecDir}`); return foundExecDir; @@ -81812,20 +81814,20 @@ async function installGhjk(version, installerUrl, skipDenoInstall) { core.debug(`unable to find cached ghjk tool under version ${version}`); } core.debug(`installing ghjk using install.ts`); - const installDir = process.env['GHJK_INSTALL_EXE_DIR'] ?? - core.toPlatformPath(path.resolve(os.homedir(), '.local', 'bin')); + const installDir = process.env["GHJK_INSTALL_EXE_DIR"] ?? + core.toPlatformPath(path.resolve(os.homedir(), ".local", "bin")); const env = { ...process.env, GHJK_INSTALL_EXE_DIR: installDir, - SHELL: 'bash' + SHELL: "bash", }; // NOTE: we make the ghjk bin use whichver deno is avail in path // to avoid it hardcoding the current deno bin path // which won't be the same after tool cache restore - env['GHJK_INSTALL_DENO_EXEC'] = 'deno'; - core.debug(JSON.stringify({ denoExec, env }, undefined, ' ')); + env["GHJK_INSTALL_DENO_EXEC"] = "deno"; + core.debug(JSON.stringify({ denoExec, env }, undefined, " ")); await exec.exec(`"${denoExec}" run -A`, [installerUrl], { env }); - return await tc.cacheDir(installDir, 'ghjk', version); + return await tc.cacheDir(installDir, "ghjk", version); } exports.installGhjk = installGhjk; async function installDeno(version) { @@ -81836,32 +81838,32 @@ async function installDeno(version) { function zipName() { let arch; switch (process.arch) { - case 'arm64': - arch = 'aarch64'; + case "arm64": + arch = "aarch64"; break; - case 'x64': - arch = 'x86_64'; + case "x64": + arch = "x86_64"; break; default: throw new Error(`Unsupported architechture ${process.arch}.`); } let platform; switch (process.platform) { - case 'linux': - platform = 'unknown-linux-gnu'; + case "linux": + platform = "unknown-linux-gnu"; break; - case 'darwin': - platform = 'apple-darwin'; + case "darwin": + platform = "apple-darwin"; break; - case 'win32': - platform = 'pc-windows-msvc'; + case "win32": + platform = "pc-windows-msvc"; break; default: throw new Error(`Unsupported platform ${process.platform}.`); } return `deno-${arch}-${platform}.zip`; } - const cachedPath = tc.find('ghjk-deno', version); + const cachedPath = tc.find("ghjk-deno", version); if (cachedPath) { core.info(`Using cached Deno installation from ${cachedPath}.`); core.addPath(cachedPath); @@ -81872,7 +81874,7 @@ async function installDeno(version) { core.info(`Downloading Deno from ${url}.`); const zipPath = await tc.downloadTool(url); const extractedFolder = await tc.extractZip(zipPath); - const newCachedPath = await tc.cacheDir(extractedFolder, 'ghjk-deno', version); + const newCachedPath = await tc.cacheDir(extractedFolder, "ghjk-deno", version); core.info(`Cached Deno to ${newCachedPath}.`); core.addPath(newCachedPath); return `${newCachedPath}/deno`; @@ -81885,15 +81887,15 @@ exports.installDeno = installDeno; async function post() { try { if (cache.isFeatureAvailable() && - core.getState('ghjk-cache-save') === 'true') { - const argsStr = core.getState('ghjk-post-args'); + core.getState("ghjk-cache-save") === "true") { + const argsStr = core.getState("ghjk-post-args"); core.info(argsStr); const args = JSON.parse(argsStr); const { key, cacheDirs } = args; await cache.saveCache(cacheDirs, key); } else { - core.info('cache-save flag is false, skipping'); + core.info("cache-save flag is false, skipping"); } } catch (error) { diff --git a/index.ts b/index.ts index 83dc4cc..6775e41 100644 --- a/index.ts +++ b/index.ts @@ -14,7 +14,7 @@ async function latestGhjkVersion() { const resp = await fetch( `https://api.github.com/repos/metatypedev/ghjk/releases/latest` ) - if (resp.ok) { + if (!resp.ok) { throw new Error( `error fetching latest ghjk release meta: ${resp.statusText}` ) diff --git a/package.json b/package.json index 2ff933b..3b254a0 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "node": ">=20" }, "scripts": { - "lint": "pnpm dlx eslint . -c ./.github/linters/.eslintrc.yml", + "lint": "npx eslint . -c ./.github/linters/.eslintrc.yml", "format:write": "prettier --write *", "format:check": "prettier --check *", "build": "ncc build index.ts -o .",