Files
den-repo/build.fish
2026-08-14 15:56:15 -04:00

71 lines
1.9 KiB
Fish
Executable File

#!/usr/bin/env fish
# den.js build: minify, obfuscate, syntax-check, bump cache version.
# needs: npm install -g terser javascript-obfuscator
set -l src src/den.js
set -l min build/den.min.js
set -l obf build/den.obf.js
set -l banner "/* den.js - an invisible kobold. (c) 2026 owo.ing. hands off the hoard. */"
if not test -f $src
echo "no $src here. run this from the repo root."
exit 1
end
mkdir -p build
echo ":: minifying"
terser $src --compress passes=2 --mangle --format ascii_only=true -o $min
or begin
echo "terser failed"
exit 1
end
echo ":: obfuscating"
javascript-obfuscator $src --output $obf \
--compact true \
--self-defending true \
--string-array true --string-array-encoding base64 --string-array-threshold 0.9 \
--string-array-rotate true --string-array-shuffle true \
--control-flow-flattening true --control-flow-flattening-threshold 0.4 \
--dead-code-injection true --dead-code-injection-threshold 0.2 \
--unicode-escape-sequence false
or begin
echo "obfuscator failed"
exit 1
end
echo ":: syntax check"
node --check $min; and node --check $obf
or begin
echo "SYNTAX CHECK FAILED - not touching your pages"
exit 1
end
# banner goes on after the tools run, or they'd strip it
for f in $min $obf
set -l tmp (mktemp)
echo $banner > $tmp
cat $f >> $tmp
mv $tmp $f
end
# bump ?v=N wherever a build is referenced, so browsers actually refetch
set -l bumped 0
for f in *.php example/*.html example/*.php
test -f $f; or continue
set -l cur (grep -oP 'den\.(min|obf)\.js\?v=\K\d+' $f | head -1)
if test -n "$cur"
set -l new (math $cur + 1)
sed -i "s/\(den\.\(min\|obf\)\.js?v=\)$cur/\1$new/g" $f
echo " $f: v$cur -> v$new"
set bumped (math $bumped + 1)
end
end
if test $bumped -eq 0
echo " (no pages with ?v= found - remember to bust cache yourself)"
end
echo ":: done"
ls -la $src $min $obf | awk '{printf " %8s %s\n", $5, $9}'