-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_tooltip.sh
More file actions
executable file
·73 lines (62 loc) · 2.42 KB
/
Copy pathadd_tooltip.sh
File metadata and controls
executable file
·73 lines (62 loc) · 2.42 KB
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
67
68
69
70
71
72
73
#!/usr/bin/env bash
# add_tooltip.sh — Append a new placeholder tooltip entry to tooltips.json.
# Called by the bot admin panel "Add Tooltip (Script)" button, or manually.
#
# Usage:
# ./add_tooltip.sh [--text "Custom tooltip text"]
#
# Outputs: new tooltip ID on stdout.
# On success exits 0; on failure exits non-zero.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_DIR="${RUNEWAGER_DIR:-$SCRIPT_DIR}"
DATA_DIR="$APP_DIR/data"
TOOLTIPS_FILE="$DATA_DIR/tooltips.json"
TMP_FILE="$TOOLTIPS_FILE.tmp.$$"
CUSTOM_TEXT=""
while [[ $# -gt 0 ]]; do
case "$1" in
--text) CUSTOM_TEXT="$2"; shift 2 ;;
*) shift ;;
esac
done
info() { echo "[add_tooltip] INFO: $*"; }
error() { echo "[add_tooltip] ERROR: $*" >&2; exit 1; }
mkdir -p "$DATA_DIR" || error "Cannot create data dir"
# Ensure tooltips.json exists
if [[ ! -f "$TOOLTIPS_FILE" ]]; then
info "tooltips.json not found — initialising empty list"
echo '[]' > "$TOOLTIPS_FILE"
fi
# Validate existing file
node -e "JSON.parse(require('fs').readFileSync('$TOOLTIPS_FILE','utf8'))" 2>/dev/null \
|| error "Existing $TOOLTIPS_FILE is not valid JSON"
TOOLTIP_TEXT="${CUSTOM_TEXT:-New tooltip — edit in admin panel via /tips.}"
# Append new entry and get new ID using Node.js
# Text and tmp path are passed via env vars to avoid shell injection on special characters
NEW_ID=$(TOOLTIP_TEXT_ENV="$TOOLTIP_TEXT" TOOLTIP_TMP_FILE="$TMP_FILE" node - "$TOOLTIPS_FILE" <<'EOF'
const fs = require('fs');
const file = process.argv[2];
const text = process.env.TOOLTIP_TEXT_ENV;
const tmpFile = process.env.TOOLTIP_TMP_FILE;
const list = JSON.parse(fs.readFileSync(file, 'utf8'));
if (!Array.isArray(list)) {
throw new Error('tooltips.json must contain a JSON array');
}
// Only count entries that have a genuine finite numeric id
const numericIds = list
.map((t) => (t && Object.prototype.hasOwnProperty.call(t, 'id') ? Number(t.id) : NaN))
.filter((id) => Number.isFinite(id));
const maxId = numericIds.length ? Math.max(...numericIds) : 0;
const newId = maxId + 1;
list.push({ id: newId, text, enabled: true });
fs.writeFileSync(tmpFile, JSON.stringify(list, null, 2));
console.log(newId);
EOF
)
# Validate and move
node -e "JSON.parse(require('fs').readFileSync('$TMP_FILE','utf8'))" 2>/dev/null \
|| { rm -f "$TMP_FILE"; error "Generated JSON failed validation"; }
mv "$TMP_FILE" "$TOOLTIPS_FILE"
info "Added tooltip #${NEW_ID}: ${TOOLTIP_TEXT:0:60}"
echo "$NEW_ID"