From 1fe76f3ed47f9a30ecb839b6593a524796baa100 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 31 Aug 2023 11:59:17 -0400 Subject: [PATCH] feat: extract tag fetch logic to file --- api/scripts/build.mjs | 71 ++++++++++++++++++++++++++++++++++++++++ api/scripts/get-tags.mjs | 28 ++++++++++++++++ api/tsup.config.ts | 31 +++++------------- 3 files changed, 107 insertions(+), 23 deletions(-) create mode 100755 api/scripts/build.mjs create mode 100644 api/scripts/get-tags.mjs diff --git a/api/scripts/build.mjs b/api/scripts/build.mjs new file mode 100755 index 000000000..82bfee11d --- /dev/null +++ b/api/scripts/build.mjs @@ -0,0 +1,71 @@ +#!/usr/bin/env zx +import { exit } from 'process'; +import { cd, $ } from 'zx'; + +import getTags from './get-tags.mjs' + +try { + // Enable colours in output + process.env.FORCE_COLOR = '1'; + + // Ensure we have the correct working directory + process.env.WORKDIR = process.env.WORKDIR ?? process.env.PWD; + cd(process.env.WORKDIR); + + // Clean up last deploy + await $`rm -rf ./deploy/release`; + await $`rm -rf ./deploy/pre-pack`; + await $`mkdir -p ./deploy/release/`; + await $`mkdir -p ./deploy/pre-pack/`; + + // Ensure all deps are installed + await $`npm i`; + + // Build Generated Types + await $`npm run codegen`; + // Build binary + await $`npm run build`; + + // Copy binary + extra files to deployment directory + await $`cp ./dist/api ./deploy/pre-pack/unraid-api`; + await $`cp ./.env.production ./deploy/pre-pack/.env.production`; + await $`cp ./.env.staging ./deploy/pre-pack/.env.staging`; + + // Get package details + const { name, version } = await import('../package.json', { + assert: { type: 'json' }, + }).then(pkg => pkg.default); + + const tags = getTags(); + + // Decide whether to use full version or just tag + const isTaggedRelease = tags.isTagged; + const gitShaShort = tags.shortSha; + const deploymentVersion = isTaggedRelease ? version : `${version}+${gitShaShort}`; + + // Create deployment package.json + await $`echo ${JSON.stringify({ name, version: deploymentVersion })} > ./deploy/pre-pack/package.json`; + + // # Create final tgz + await $`cp ./README.md ./deploy/pre-pack/`; + cd('./deploy/pre-pack'); + await $`npm pack`; + + // Move unraid-api.tgz to release directory + await $`mv unraid-api-${deploymentVersion}.tgz ../release`; + + // Set API_VERSION output based on this command + await $`echo "::set-output name=API_VERSION::${deploymentVersion}"`; +} catch (error) { + // Error with a command + if (Object.keys(error).includes('stderr')) { + console.log(`Failed building package. Exit code: ${error.exitCode}`); + console.log(`Error: ${error.stderr}`); + } else { + // Normal js error + console.log('Failed building package.'); + console.log(`Error: ${error.message}`); + } + + exit(error.exitCode); +} diff --git a/api/scripts/get-tags.mjs b/api/scripts/get-tags.mjs new file mode 100644 index 000000000..07f47a102 --- /dev/null +++ b/api/scripts/get-tags.mjs @@ -0,0 +1,28 @@ +const runCommand = (command) => { + try { + return execSync(command, { stdio: 'pipe' }).toString().trim(); + } catch { + return; + } +}; + +const getTags = () => { + const GIT_SHA_ENV = process.env.GIT_SHA + const IS_TAGGED_ENV = Boolean(process.env.IS_TAGGED); + if (GIT_SHA_ENV && IS_TAGGED_ENV) { + return { + shortSha: GIT_SHA_ENV, + isTagged: IS_TAGGED_ENV + } + } else { + const gitShortSHA = runCommand('git rev-parse --short HEAD'); + const isCommitTagged = runCommand('git describe --tags --abbrev=0 --exact-match') !== undefined; + + return { + shortSha: gitShortSHA, + isTagged: isCommitTagged + } + } +} + +export default getTags; \ No newline at end of file diff --git a/api/tsup.config.ts b/api/tsup.config.ts index 2e8a092d3..d61608a63 100644 --- a/api/tsup.config.ts +++ b/api/tsup.config.ts @@ -2,14 +2,9 @@ import { execSync } from 'child_process'; import 'dotenv/config'; import { defineConfig } from 'tsup'; import { version } from './package.json'; +import getTags from './scripts/get-tags.mjs' + -const runCommand = (command: string) => { - try { - return execSync(command, { stdio: 'pipe' }).toString().trim(); - } catch { - return; - } -}; export default defineConfig({ name: 'tsup', @@ -25,21 +20,11 @@ export default defineConfig({ external: ['@vmngr/libvirt'], esbuildOptions(options) { if (!options.define) options.define = {}; - console.log('IS TAGGED', process.env.IS_TAGGED); - if (process.env.GIT_SHA && process.env.IS_TAGGED) { - const gitShortSHA = process.env.GIT_SHA; - const isCommitTagged = process.env.IS_TAGGED; - options.define['process.env.VERSION'] = isCommitTagged - ? `"${version}"` - : `"${version}+${gitShortSHA}"`; - } else { - const gitShortSHA = runCommand('git rev-parse --short HEAD'); - const isCommitTagged = - runCommand('git describe --tags --abbrev=0 --exact-match') !== - undefined; - options.define['process.env.VERSION'] = isCommitTagged - ? `"${version}"` - : `"${version}+${gitShortSHA}"`; - } + + const tags = getTags(); + + options.define['process.env.VERSION'] = tags.isTagged + ? `"${version}"` + : `"${version}+${tags.shortSha}"`; }, });