feat: extract tag fetch logic to file

This commit is contained in:
Eli Bosley
2023-08-31 11:59:17 -04:00
parent bd65db1c88
commit 1fe76f3ed4
3 changed files with 107 additions and 23 deletions

71
api/scripts/build.mjs Executable file
View File

@@ -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);
}

28
api/scripts/get-tags.mjs Normal file
View File

@@ -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;

View File

@@ -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}"`;
},
});