chore: incidental working-tree changes (not from the versioning work)

Changes already present in the working tree at the time of the versioning
commit, committed here so nothing is left dangling:

- index.js: template PUT preserves the untouched field (?? existing) and the
  preview handler tolerates a null/empty test_object
- update-deployed.sh: detect and restart a pm2-managed app process, and don't
  hard-fail when no bare Node process is found

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-08-03 00:27:31 -04:00
parent 42f4aacf7c
commit 5bf756833d
2 changed files with 44 additions and 27 deletions

View file

@ -606,14 +606,13 @@ app.post("/workshop/:structure_id/template", (req, res) => {
}); });
app.put("/workshop/:structure_id/template/:template_id", (req, res) => { app.put("/workshop/:structure_id/template/:template_id", (req, res) => {
let id = req.params.template_id; const id = req.params.template_id;
let content = req.body.content; const template = model.getTemplate(id);
let test_object = req.body.test_object;
model.updateTemplate({ model.updateTemplate({
...model.getTemplate(id), ...template,
content, content: req.body.content ?? template.content,
test_object, test_object: req.body.test_object ?? template.test_object,
}); });
return res.send("good"); return res.send("good");
@ -632,9 +631,12 @@ app.get("/workshop/:structure_id/template/:template_id/preview", (req, res) => {
const template = model.getTemplate(req.params.template_id); const template = model.getTemplate(req.params.template_id);
const struct = model.getStructure(req.params.structure_id); const struct = model.getStructure(req.params.structure_id);
const context = vm.createContext({ it: null }); const context = vm.createContext({ it: {} });
if (template.test_object) {
vm.runInContext(template.test_object, context); vm.runInContext(template.test_object, context);
}
context.it ??= {};
context.it.route = makeRoute(struct); context.it.route = makeRoute(struct);
return res.send( return res.send(

View file

@ -70,6 +70,25 @@ else
echo "Dependencies unchanged; skipping npm install." echo "Dependencies unchanged; skipping npm install."
fi fi
PM2_TARGET=""
if command -v pm2 >/dev/null 2>&1; then
PM2_TARGET="$(pm2 jlist 2>/dev/null | APP_DIR="$APP_DIR" node -e '
let input = "";
process.stdin.on("data", chunk => input += chunk);
process.stdin.on("end", () => {
try {
const appDir = process.env.APP_DIR;
const processes = JSON.parse(input);
const match = processes.find(process => {
const env = process.pm2_env || {};
return env.pm_cwd === appDir || env.pm_exec_path === `${appDir}/index.js`;
});
if (match) process.stdout.write(String(match.pm_id));
} catch (_) {}
});
' || true)"
fi
mapfile -t APP_PIDS < <( mapfile -t APP_PIDS < <(
for process_dir in /proc/[0-9]*; do for process_dir in /proc/[0-9]*; do
[[ "$(readlink "$process_dir/cwd" 2>/dev/null || true)" == "$APP_DIR" ]] || continue [[ "$(readlink "$process_dir/cwd" 2>/dev/null || true)" == "$APP_DIR" ]] || continue
@ -78,35 +97,31 @@ mapfile -t APP_PIDS < <(
done done
) )
if [[ "${#APP_PIDS[@]}" -eq 0 ]]; then APP_PID="${APP_PIDS[0]:-}"
echo "No running Node process found for $APP_DIR; refusing to guess how to start it." >&2
exit 1
fi
APP_PID="${APP_PIDS[0]}"
SERVICE="" SERVICE=""
while IFS= read -r unit; do if [[ -n "$APP_PID" ]]; then
while IFS= read -r unit; do
[[ -n "$unit" ]] || continue [[ -n "$unit" ]] || continue
if [[ "$(systemctl show -p MainPID --value "$unit" 2>/dev/null || true)" == "$APP_PID" ]]; then if [[ "$(systemctl show -p MainPID --value "$unit" 2>/dev/null || true)" == "$APP_PID" ]]; then
SERVICE="$unit" SERVICE="$unit"
break break
fi fi
done < <(systemctl list-units --type=service --all --no-legend --plain 2>/dev/null | awk '{print $1}') done < <(systemctl list-units --type=service --all --no-legend --plain 2>/dev/null | awk '{print $1}')
fi
if [[ -n "$SERVICE" ]]; then if [[ -n "$SERVICE" ]]; then
echo "Restarting systemd service $SERVICE..." echo "Restarting systemd service $SERVICE..."
systemctl restart "$SERVICE" systemctl restart "$SERVICE"
systemctl is-active --quiet "$SERVICE" systemctl is-active --quiet "$SERVICE"
systemctl --no-pager --full status "$SERVICE" | sed -n '1,12p' systemctl --no-pager --full status "$SERVICE" | sed -n '1,12p'
elif command -v pm2 >/dev/null 2>&1 && pm2 pid all 2>/dev/null | grep -qx "$APP_PID"; then elif [[ -n "$PM2_TARGET" ]]; then
echo "Restarting PM2 process $APP_PID..." echo "Restarting PM2 process $PM2_TARGET..."
pm2 restart "$APP_PID" --update-env pm2 restart "$PM2_TARGET" --update-env
pm2 show "$APP_PID" pm2 show "$PM2_TARGET"
else else
echo "Found Node PID $APP_PID, but it is not managed by systemd or PM2." >&2 echo "Could not find a systemd or PM2 app configured for $APP_DIR." >&2
echo "The code was updated from $OLD_HEAD to $NEW_HEAD, but the process was not restarted." >&2 echo "The code was updated from $OLD_HEAD to $NEW_HEAD, but the process was not restarted." >&2
echo "Refusing to kill it without knowing the correct startup mechanism." >&2
exit 1 exit 1
fi fi