Skip to content

Commit

Permalink
reorganized repository to account for client-side functionality
Browse files Browse the repository at this point in the history
we're including reusable library functionality for client apps in this
repo (limited to crypto for now), but client-side environments require
slightly different settings for testing and type checking (at least if
we wanna remain strict) - so unfortunately, we need non-trivial scripts
to make that work

in the process, simplified type checking as Deno now supports globbing
throughout (see denoland/deno#24668)
  • Loading branch information
FND committed Aug 24, 2024
1 parent dd7a108 commit 45601c9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Contributing

* `./bin/check` performs static type checking

* `deno test` runs the test suite (optionally with `--parallel` or `--watch`)
* `./bin/test` runs the test suites for both server and client (optionally
with `--parallel` or `--watch`)
41 changes: 32 additions & 9 deletions bin/check
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/env bash

# poor man's globbing substitute
# type checking with support for directory-based configuration inheritance

set -eu

root_dir=`dirname "$0"`
root_dir=`realpath "$root_dir/.."`

entry_point="./index.ts"
tsconfig_base="./tsconfig.json"
tsconfig_actual="./tsconfig.tmp.json"
projects=(./src/server ./src/client)

quit() {
rm -rf "$entry_point"
rm "$tsconfig_actual" || true
}
trap quit EXIT

Expand All @@ -22,10 +24,31 @@ abort() {
}

cd "$root_dir"
if [ -f "$entry_point" ]; then
abort "ERROR: \`$entry_point\` already exists"
fi
# check each subproject seperately to account for varying configuration
for dir in "${projects[@]}"; do
custom="$dir/tsconfig.json"
if [ ! -f "$custom" ]; then
cp "$tsconfig_base" "$tsconfig_actual"
else # merge configuration files
deno run --import-map ./deno.json - > "$tsconfig_actual" <<-EOS
import defaults from "$tsconfig_base" with { "type": "json" };
import custom from "$custom" with { "type": "json" };
import { dirname, join, normalize } from "\$deno/path/mod.ts";
// ensure we adhere to established conventions
if(normalize("$tsconfig_base") !== join(dirname("$custom"), custom.extends)) {
console.error("ERROR: missing or invalid \`extends\` in \`$custom\`; " +
"expected reference to \`" + join("$root_dir", "$tsconfig_base") + "\`");
Deno.exit(1);
}
find ./src -name "*.js" | \
while read fn; do echo "import \"$fn\";" >> "$entry_point"; done
deno task verify -- "$entry_point"
console.log(JSON.stringify({
compilerOptions: {
...defaults.compilerOptions,
...custom.compilerOptions
}
}, null, 4));
EOS
fi
deno task verify -- "$dir"
done
32 changes: 32 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

set -eu

root_dir=`dirname "$0"`
root_dir=`realpath "$root_dir/.."`

server_options=(--allow-env)

# special-casing watch mode
watch="false"
options=(--deny-net=example.org) # workaround: empty array is considered unbound variable
for arg in "$@"; do
case "$arg" in
-w | --watch)
watch="true"
;;
*)
options+=("$arg")
;;
esac
shift
done

cd "$root_dir"
if [ "$watch" = "true" ]; then
# XXX: crude; combining all options to avoid backgrounding subprocesses
deno test "${options[@]}" --watch "${server_options[@]}"
else
deno test "${options[@]}" "${server_options[@]}" ./src/server
deno test "${options[@]}" ./src/client
fi
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"tasks": {
"vet": "deno lint && deno fmt --check",
"verify": "deno check --config ./tsconfig.json --import-map ./deno.json"
"verify": "deno check --config ./tsconfig.tmp.json --import-map ./deno.json"
},
"fmt": {
"include": ["./src"],
Expand Down
2 changes: 1 addition & 1 deletion src/crypto.js → src/client/crypto.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cfg from "./config.js";
import * as cfg from "../config.js";

/* adapted from <https://prepitaph.org/articles/web-crypto-secrets/> */
let CRYPTO = globalThis.crypto.subtle;
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "dom"]
}
}

0 comments on commit 45601c9

Please sign in to comment.