CLI: surface result.debug via --json field or stderr log#331
Open
kaznak wants to merge 2 commits into
Open
Conversation
The Defuddle library populates `result.debug = { contentSelector,
removals: DebugRemoval[] }` when `debug: true`, but the CLI never
exposed it: `result.debug` was omitted from the `--json` payload's
field list, and nothing was written to stderr either, so `--debug`
was effectively a no-op for CLI users.
This change routes the debug payload through the CLI:
- With `--json --debug`: the JSON object gains a `debug` field
alongside the existing fields. The shape matches `DebugInfo` from
`src/types.ts` so machine consumers can parse it directly.
- With `--debug` and no `--json`: `parseSource` returns a
`debugLog` string formatted one removal per line (step, reason,
selector, text preview). The action wrapper writes it to stderr
so it does not interleave with the content body on stdout.
- Without `--debug`: no behavioral change. The JSON output omits
`debug` and `debugLog` is undefined.
Three tests cover the three modes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Defuddle({ debug: true })populatesresult.debug = { contentSelector, removals: DebugRemoval[] }so callers can see which step dropped what. The CLI accepts a--debugflag and passesdebug: truethrough, but nothing in the CLI ever surfaces the resultingresult.debug:--jsonpayload's field list insrc/cli.tsdoes not includedebug, so it is dropped silently.The practical effect is that
--debugis a no-op for CLI users. The most common reason to enable it on the command line — figuring out which removal pass stripped an element while developing a site-specific extractor — therefore can't be served by the CLI today.Proposal
Route
result.debugthrough the CLI:1. With
--json --debug— include the existingDebugInfopayload in the JSON object, alongside the other fields:{ "content": "...", "title": "...", ..., "debug": { "contentSelector": "html > body > main > article", "removals": [ { "step": "removeByContentPattern", "reason": "blog metadata list", "text": "Hugging Face ModelScope" }, ... ] } }Field shape is unchanged — it is
DebugInfofromsrc/types.ts. Machine consumers (jq pipelines, extractor-development scripts) can read it directly.2. With
--debugand no--json— format the same payload as a one-line-per-removal human-readable log and write it to stderr. Stdout still gets the article content unchanged, so existing pipelines keep working. Example stderr output:3. Without
--debug— no behavioral change. JSON output omits thedebugfield, plain output is identical to before.Implementation lives entirely in
src/cli.ts: a newformatDebugLog()helper, adebugLog?: stringfield on theparseSourcereturn value, and the action wrapper writes it toprocess.stderr.Tests
tests/cli.test.tsadds:--debug --jsonembedsdebugfield withcontentSelector(string) andremovals(array).debugLogis undefined (the JSON path owns the output).--debugwithout--jsonreturns adebugLogstring starting with# defuddle --debugand containingcontentSelector:andremovals:. The primaryoutputfield stays content-only.--debug: JSON has nodebugfield, plain mode has nodebugLog.All 371 existing tests continue to pass.
Notes
DebugInfo/DebugRemovaltypes are imported astypeso the CLI bundle does not pull in any runtime code fromsrc/types.ts.r.textto keep each removal on one line; the API'stextfield already gives a short preview, so no extra truncation is done.--source-url(CLI: add --source-url flag for site-specific extraction on stdin/file input #322) and--extractor(CLI: add --extractor flag and defuddle/extractor public entry #328). All three combine naturally:--extractor my.mjs --debug --jsonis the workflow this PR is aimed at.