aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/build.sh
blob: ac7651f10551d6529cccfd9aed6c05758a7e00ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh

set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PROJECT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
OUTPUT_DIR="$PROJECT_DIR/dist"
OUTPUT_FILE="$OUTPUT_DIR/tdwiki.html"
TIDDLYWIKI_BIN=${TIDDLYWIKI_BIN:-tiddlywiki}
BUILD_TMP_DIR=
BUILD_WIKI_DIR=

cleanup() {
	if [ -n "$BUILD_TMP_DIR" ] && [ -d "$BUILD_TMP_DIR" ]; then
		rm -rf -- "$BUILD_TMP_DIR"
	fi
}

prepare_build_wiki() {
	BUILD_TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/tdwiki-build.XXXXXX")
	BUILD_WIKI_DIR="$BUILD_TMP_DIR/wiki"
	mkdir -p "$BUILD_WIKI_DIR"
	cp -R "$PROJECT_DIR/tiddlers" "$BUILD_WIKI_DIR/tiddlers"
	cp -R "$PROJECT_DIR/plugins" "$BUILD_WIKI_DIR/plugins"

	node -e '
const fs = require("fs");
const source = process.argv[1];
const destination = process.argv[2];
const info = JSON.parse(fs.readFileSync(source,"utf8"));
const serverPlugins = new Set([
  "tiddlywiki/filesystem",
  "tiddlywiki/tiddlyweb"
]);
info.plugins = (info.plugins || []).filter(name => !serverPlugins.has(name));
fs.writeFileSync(destination,JSON.stringify(info,null,4) + "\n");
' "$PROJECT_DIR/tiddlywiki.info" "$BUILD_WIKI_DIR/tiddlywiki.info"
}

verify_output() {
	node -e '
const fs = require("fs");
const filename = process.argv[1];
const html = fs.readFileSync(filename,"utf8");

for(const title of ["$:/plugins/local/utilities"]) {
  if(!html.includes(`"title":"${title}"`)) {
    throw new Error(`single-file wiki is missing ${title}`);
  }
}

' "$OUTPUT_FILE"
}

trap cleanup EXIT HUP INT TERM

prepare_build_wiki
mkdir -p "$OUTPUT_DIR"

"$TIDDLYWIKI_BIN" "$BUILD_WIKI_DIR" \
	--output "$OUTPUT_DIR" \
	--rendertiddler '$:/core/save/all' \
	tdwiki.html text/plain

verify_output
printf 'Built %s\n' "$OUTPUT_FILE"