Skip to content

Commit

Permalink
Improve build script
Browse files Browse the repository at this point in the history
Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com>
  • Loading branch information
M4RC3L05 committed Dec 20, 2024
1 parent 47711f0 commit da7ece6
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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));

0 comments on commit da7ece6

Please sign in to comment.