Skip to content

SDK examples

The repository ships runnable examples under examples/sdk/ — the fastest way to learn the host contract without reading the CLI source.

Clone nodehunter and run any example from the repo root:

bash
pnpm install
pnpm run core:build    # required when importing @nodehunter/core from dist
pnpm tsx examples/sdk/scan/runScan.ts

During monorepo development, TypeScript resolves @nodehunter/core from source via tsconfig paths. At runtime, tsx uses the built package in packages/core/dist/ — run pnpm run core:build once after clone.

Layout

PathRole
_shared/hostContext.tsBuild HostOperationContext + fixture root
_shared/hooks.tsScanRequestHooks (onWalkDebug, onPerfDebug, onSizeNotice)
_shared/envelope.tsbuildAnalysisCommandEnvelope — same shape as CLI --json
fixtures/workspace/Committed sample monorepo (react semver drift, zod, lodash)
<op>/run*.tsRunnable script per command
<op>/README.mdOp-specific notes and CLI mapping

Examples by core operation

Every public run* entry point has a dedicated example:

Core APIExampleCLI equivalent
runScanexamples/sdk/scan/runScan.tsnodehunter scan
runSizeexamples/sdk/size/runSize.tsnodehunter size
runListexamples/sdk/list/runList.tsnodehunter list
runFindexamples/sdk/find/runFind.tsnodehunter find <pkg>
runWhyexamples/sdk/why/runWhy.tsnodehunter why <pkg>
runInspectexamples/sdk/inspect/runInspect.tsnodehunter inspect
runDeletePreviewexamples/sdk/delete/runDeletePreview.tsSized preview before confirm
runDeleteRefused(in runDeletePreview.ts)Non-interactive without --yes
runDeleteDeclinedexamples/sdk/delete/runDeleteDeclined.tsUser answered no at confirm
buildWorkspaceIndexexamples/sdk/workspace/buildWorkspaceIndex.tsLower-level inventory (no presentation)

runDeleteExecute is intentionally not demonstrated in examples — hosts must gate execution behind explicit user consent or policy.

Run all read-only examples

bash
pnpm tsx examples/sdk/scan/runScan.ts
pnpm tsx examples/sdk/size/runSize.ts
pnpm tsx examples/sdk/list/runList.ts
pnpm tsx examples/sdk/find/runFind.ts
pnpm tsx examples/sdk/why/runWhy.ts
pnpm tsx examples/sdk/inspect/runInspect.ts
pnpm tsx examples/sdk/workspace/buildWorkspaceIndex.ts
pnpm tsx examples/sdk/delete/runDeletePreview.ts
pnpm tsx examples/sdk/delete/runDeleteDeclined.ts

run* vs buildWorkspaceIndex

NeedUse
Human-style lines, tips, footer timingrunScan, runSize, runList, …
Custom UI, MCP tool, your own JSONbuildWorkspaceIndex + query helpers (findPackage, inspectDuplicates, …)
Identical CLI --json envelopebuildAnalysisCommandEnvelope via _shared/envelope.ts

Fixtures

fixtures/workspace/ contains three projects (apps/web, apps/api, packages/lib) with intentional react version drift for inspect --focus duplicates.

buildExampleHostContext() defaults scan.path to this tree and sets noDu: true so examples behave consistently in CI without relying on GNU du.

Compare with the CLI host

Each example mirrors a file under packages/cli/src/commands/<op>/run.ts. The CLI adds:

  • Commander argv → HostOperationContext
  • renderHostOutput() → chalk / logger
  • canAsk() + Inquirer around delete confirm
  • --json serialization on stdout

Core logic is identical.

Safety

  • Read-only ops use committed fixtures only.
  • Delete examples call preview, refused, and declined — never runDeleteExecute by default.
  • Import only from @nodehunter/core (public API). Avoid @nodehunter/core/internal in application code — see SDK exports.