commit 45ac405505669267cea76c4244fd1dd3efac7a97 Author: Robin Chappatte Date: Wed Jun 5 17:48:16 2024 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80ea977 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +dist/ \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..e9f9de7 --- /dev/null +++ b/build.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +echo "This script will remove / overwrite the content of some \"dist/\" directories." +read -p "Do you want to continue ? [y/N]: " -n 1 -r +echo +if [[ ! $REPLY =~ ^[Yy]$ ]] +then + echo "Aborted" + exit 1 +fi + +function clean { + rm -rf ./frontend/dist && rm -rf ./backend/dist && rm -rf ./dist +} + +function build_frontend { + dir=$(pwd) + cd frontend + npm run build + cd "$dir" +} + +function build_backend { + deno run --allow-read=./frontend/dist,./dist --allow-write=./dist ./bundle.ts ./frontend/dist ./dist/bundled-view-files.json + cp ./dist/bundled-view-files.json ./backend/src/modules/view/bundled-view-files.json +} + +function compile_for { + target=$1 + mkdir -p "./dist/bin" + deno compile --target "$target" --output "./dist/bin/$target" --allow-net --allow-read --allow-write --allow-sys ./backend/src/main.ts +} + +function compile_all { + compile_for x86_64-unknown-linux-gnu + compile_for aarch64-unknown-linux-gnu + compile_for x86_64-pc-windows-msvc + compile_for x86_64-apple-darwin + compile_for aarch64-apple-darwin +} + + +clean +build_frontend +build_backend +compile_all \ No newline at end of file diff --git a/bundle.ts b/bundle.ts new file mode 100644 index 0000000..23c5df8 --- /dev/null +++ b/bundle.ts @@ -0,0 +1,59 @@ +import { encodeBase64 } from 'https://deno.land/std@0.224.0/encoding/base64.ts'; +import { ensureFile } from 'https://deno.land/std@0.224.0/fs/mod.ts'; + +/** + * { [path]: base64EncodedContent } + */ +type StaticFilesRecord = Record; + +/** + * Traverse a directory recursively, adding every base64 encoded file content to the record. + * + * @param staticFileRecord the record in which to add base64 encoded file content + * @param root the path of the root directory for traversal, that should not be included in the record file path + * @param subdir the current subdir to traverse + */ +async function traverse( + staticFileRecord: StaticFilesRecord, + root: string, + subdir: string = '', +) { + for await (const dirEntry of Deno.readDir(`${root}/${subdir}`)) { + const path = `${subdir}/${dirEntry.name}`.slice(1); + console.log(`encoding: ${path}`); + if (dirEntry.isDirectory) { + await traverse(staticFileRecord, root, `${subdir}/${dirEntry.name}`); + } else { + staticFileRecord[path] = encodeBase64( + await Deno.readFile( + `${root}/${subdir}/${dirEntry.name}`, + ), + ); + } + } +} + +/** + * Bundle all files of the given directory into a single json file at the given path. + * + * @param sourceDir path of the directory to bundle + * @param bundleFilePath path of the json file + */ +async function buildFromDir( + sourceDir: string, + bundleFilePath: string, +) { + const staticFiles: StaticFilesRecord = {}; + await traverse(staticFiles, sourceDir); + + await ensureFile(bundleFilePath); + await Deno.writeTextFile(bundleFilePath, JSON.stringify(staticFiles)); +} + +if (Deno.args.length !== 2) { + console.error('Incorrect usage: Two parameters expected !'); + console.error('