From da7ece6bf64a4efb885f2aac0076d3a8db8e16c1 Mon Sep 17 00:00:00 2001 From: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com> Date: Fri, 20 Dec 2024 20:55:04 +0000 Subject: [PATCH] Improve build script Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com> --- scripts/build.ts | 71 ++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/scripts/build.ts b/scripts/build.ts index d12a658..9eeb6c1 100755 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -1,10 +1,11 @@ #!/usr/bin/env -S deno run -A --cached-only import $ from "@david/dax"; -import { basename, resolve } from "@std/path"; -const rootDir = resolve(import.meta.dirname!, "../"); -const binDir = resolve(rootDir, ".bin"); +$.setPrintCommand(true); + +const rootDir = $.path(import.meta.dirname!).resolve(".."); +const binDir = rootDir.resolve(".bin"); const binName = "denotag" as const; const targets = [ @@ -15,40 +16,40 @@ const targets = [ "aarch64-unknown-linux-gnu", ] as const; -for (const file of Deno.readDirSync(binDir)) { - if ( - file.name.match(/.*\.(zip|tar\.gz)(\.sha256)?$/) || - targets.some((target) => file.name.includes(target)) - ) { - Deno.removeSync(resolve(binDir, file.name), { recursive: true }); - } -} - const buildFor = async (target: typeof targets[number]) => { - const finalBinDir = resolve(binDir, `${binName}-${target}`); - const finalBinName = target.includes("windows") ? `${binName}.exe` : binName; - const finalCompressName = target.includes("windows") - ? `${basename(finalBinDir)}.zip` - : `${basename(finalBinDir)}.tar.gz`; - const finalCompressPath = resolve(binDir, finalCompressName); - const checksumName = `${finalCompressName}.sha256`; - const checksumPath = resolve(binDir, checksumName); - const finalBinPath = resolve(finalBinDir, finalBinName); - const compressCmd = target.includes("windows") - ? `zip -j ${finalCompressPath} ${finalBinPath}` - : `tar -czvf ${finalCompressPath} -C ${finalBinDir} ${finalBinName}`; - const scriptPath = resolve(rootDir, "src", "main.ts"); - - await $`deno compile --allow-env=ENV --allow-net=127.0.0.1 --include ./data/index.html --env=${ - $.path(resolve(rootDir, ".env")) - } --target=${target} --output ${$.path(finalBinPath)} ${$.path(scriptPath)}`; - await $.raw`${compressCmd}`; - await $`cd ${$.path(binDir)} && sha256sum ${finalCompressName} > ${ - $.path(checksumPath) + const compileDirName = `${binName}-${target}`; + const compiledBinName = target.includes("windows") + ? `${binName}.exe` + : binName; + const compiledPath = binDir + .resolve(compileDirName) + .resolve(compiledBinName); + + const compressedBinName = target.includes("windows") + ? `${compileDirName}.zip` + : `${compileDirName}.tar.gz`; + const compressedPath = binDir.resolve(compressedBinName); + const checksumPath = compressedPath.parentOrThrow().join( + `${compressedBinName}.sha256`, + ); + + await $`deno compile --allow-env=ENV --allow-net=127.0.0.1 --include=./data/index.html --env=${ + rootDir.resolve(".env") + } --target=${target} --output=${compiledPath} ${ + rootDir.resolve("src", "main.ts") }`; - await Deno.remove(finalBinDir, { recursive: true }); -}; -await $`echo "ENV=production" > ${$.path(resolve(rootDir, ".env"))}`; + if (target.includes("windows")) { + await $`zip -j ${compressedPath} ${compiledPath}`; + } else { + await $`tar -czvf ${compressedPath} -C ${compiledPath.parentOrThrow()} ${binName}`; + } + + await $`rm -r ${compiledPath.parentOrThrow()}`; + + await $`cd ${binDir} && sha256sum ${compressedBinName} > ${checksumPath}`; +}; +await binDir.emptyDir(); +await $`echo "ENV=production" > ${rootDir.resolve(".env")}`; await Promise.all(targets.map(buildFor));