fix: rewrite hook in pure node — no bash/awk/sed portability issues
This commit is contained in:
@@ -1,41 +1,32 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env node
|
||||||
# SessionStart hook for ArcheFlow plugin.
|
// SessionStart hook for ArcheFlow plugin.
|
||||||
# Injects the using-archeflow skill as additional context.
|
// Injects the using-archeflow skill as additional context.
|
||||||
|
|
||||||
PLUGIN_ROOT="$(cd "$(dirname "$0")/.." 2>/dev/null && pwd)" || {
|
const fs = require("fs");
|
||||||
echo '{}'
|
const path = require("path");
|
||||||
exit 0
|
|
||||||
|
try {
|
||||||
|
const pluginRoot = path.resolve(__dirname, "..");
|
||||||
|
const skillFile = path.join(pluginRoot, "skills", "using-archeflow", "SKILL.md");
|
||||||
|
|
||||||
|
if (!fs.existsSync(skillFile)) {
|
||||||
|
console.log("{}");
|
||||||
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SKILL_FILE="${PLUGIN_ROOT}/skills/using-archeflow/SKILL.md"
|
const raw = fs.readFileSync(skillFile, "utf8");
|
||||||
|
|
||||||
if [ ! -f "$SKILL_FILE" ]; then
|
// Strip YAML frontmatter
|
||||||
echo '{}'
|
const stripped = raw.replace(/^---\n[\s\S]*?\n---\n?/, "");
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Strip YAML frontmatter (everything between first two --- lines)
|
if (!stripped.trim()) {
|
||||||
CONTENT=$(awk 'BEGIN{skip=0} /^---$/{skip++; next} skip>=2{print}' "$SKILL_FILE" 2>/dev/null) || {
|
console.log("{}");
|
||||||
echo '{}'
|
process.exit(0);
|
||||||
exit 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ -z "$CONTENT" ]; then
|
console.log(JSON.stringify({
|
||||||
echo '{}'
|
hookSpecificOutput: { additionalContext: stripped }
|
||||||
exit 0
|
}));
|
||||||
fi
|
} catch (e) {
|
||||||
|
console.log("{}");
|
||||||
# JSON-escape and output
|
|
||||||
if command -v node &>/dev/null; then
|
|
||||||
node -e '
|
|
||||||
const content = require("fs").readFileSync(0, "utf8");
|
|
||||||
console.log(JSON.stringify({ hookSpecificOutput: { additionalContext: content } }));
|
|
||||||
' <<< "$CONTENT"
|
|
||||||
else
|
|
||||||
# Portable fallback
|
|
||||||
ESCAPED=$(printf '%s' "$CONTENT" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e ':a;N;$!ba;s/\n/\\n/g' 2>/dev/null) || {
|
|
||||||
echo '{}'
|
|
||||||
exit 0
|
|
||||||
}
|
}
|
||||||
printf '{"hookSpecificOutput":{"additionalContext":"%s"}}' "$ESCAPED"
|
|
||||||
fi
|
|
||||||
|
|||||||
Reference in New Issue
Block a user