GitHub 0
Skip to content

JavaScript API

scan, check, measure, defineSource and the report types, for programs that use whatfrees as a library.

whatfrees is an ES module for Node.js 22.20 or later on Linux or macOS. It has four entries.

ImportExports
whatfreesscan, check, ScanOptions and every report type
whatfrees/measuremeasure, diskUsage and their types
whatfrees/sourcessources, defineSource, runSources, quote, isAllowed, ALLOWED_COMMANDS and the source types
whatfrees/schemaREPORT_SCHEMA, CHECK_SCHEMA and the report types

Nothing here deletes, moves or changes a file. scan writes only the size cache.

import { scan } from "whatfrees";

const report = await scan({ min: 10 * 1024 ** 2, budget: 2 });
console.log(report.totals, report.partial);
ts

scan(options?) returns a Report. It rejects on Windows. See the JSON report for its shape.

OptionTypeDefaultMeaning
onlystring[]all sourcesSource ids to run. An unknown id throws
rootsstring[]noneExtra folders for the worktrees source
budgetnumber5Seconds for the whole scan. With 0, owners answer and nothing is walked
exactbooleanfalseWalk every tree to the end, with no cache
cachebooleantrueRead and write the size cache
minnumber0Leave out entries that free fewer bytes. The totals still count them. The command line passes 50 MB
allbooleanfalseKeep the entries min and live would leave out
sourcesSource[]the built-in onesSources to run instead

The library's min defaults to 0. The command line's --min defaults to 50 MB.

import { check } from "whatfrees";

const answer = await check("/home/you/.codex/worktrees/9f2c/app");
if (answer.verdict === "safe") console.log("go on");
ts

check(path) returns a CheckResult: { schema, path, exists, verdict, deletable, evidence }. deletable is true only for safe and orphan on a path that exists. Check the verdict itself before automatic cleanup: an orphan still needs manual review. See whatfrees check.

import { measure } from "whatfrees/measure";

const [tree] = await measure(["/home/you/app/node_modules"], { deadlineMs: 2000 });
console.log(tree.frees, tree.apparent, tree.shared, tree.precision);
ts

measure(paths, options?) walks trees in parallel and returns one Measured per path. It doesn't follow symlinks or cross into another file system.

OptionDefaultMeaning
deadlineMsnoneTime budget. A tree still being read comes back as a lower-bound
workers4Walker threads. 0 walks in the calling thread
signalnoneAn AbortSignal. Aborting rejects the call

A Measured has path, frees, apparent, shared, precision, files and, when the path couldn't be read, error with the code, such as ENOENT. Here precision is exact, lower-bound or unmeasured. files counts everything that isn't a folder.

diskUsage(path) returns { free, total } in bytes for the file system that holds path.

A source is one place that holds space. Build it with defineSource, then pass it to scan.

import { scan } from "whatfrees";
import { defineSource } from "whatfrees/sources";

const scratch = defineSource({
  id: "scratch",
  description: "Folders named scratch in the project.",
  platforms: ["linux", "darwin"],
  detect: () => true,
  entries: () => [
    {
      what: "scratch folder",
      path: "/home/you/app/scratch",
      verdict: "ask",
      evidence: ["Nothing says what it is for."],
      command: ["rm", "-rf", "--", "/home/you/app/scratch"],
    },
  ],
});

const report = await scan({ sources: [scratch], min: 0 });
ts
FieldMeaning
idLowercase words joined by -. Anything else throws
descriptionWhat it covers. whatfrees sources prints it
platforms"linux", "darwin" or both
detect(ctx)Whether there is anything to look at. Keep it cheap: a file check or a PATH lookup
entries(ctx)What it found, as Found items. May be async

ctx holds platform, home, env, roots, a signal that aborts when the budget runs out, and a deadline. Stop work when the signal aborts.

A Found has what, verdict and command. It may also have key, path, bytes, evidence, then and lastUsed.

  • Leave out bytes and whatfrees measures path. Pass bytes when the owner already knows the size.
  • command is the words of a command, verb first. whatfrees quotes them. A command whose verb isn't in ALLOWED_COMMANDS throws, and the source is skipped with reason: "error".
  • key is unique within the source. It defaults to path. The entry's id is <source id>:<key>.

Ask the owner first. Run subprocesses with a timeout and pass them ctx.signal. Never delete in a source.

runSources(list, options) runs sources under one budget and returns { entries, skipped, partial }. options needs budgetMs, platform, home and env. scan builds on it and adds the totals, the disk and the filters.

whatfrees and whatfrees/schema export Report, Entry, Bytes, Disk, Skipped, Next, CheckResult, Verdict and Precision. whatfrees/schema also exports the two schema ids as constants.

import { CHECK_SCHEMA, REPORT_SCHEMA, type Report } from "whatfrees/schema";

function isReport(value: { schema?: string }): value is Report {
  return value.schema === REPORT_SCHEMA;
}
ts