API reference
The purgeit package exports a framework-agnostic API for scanning, deleting, and configuring rules. It does not include the TUI components.
Load config
Section titled “Load config”import { loadConfig } from 'purgeit';
const { config, filepath } = await loadConfig({ cwd: process.cwd(),});Build a ruleset
Section titled “Build a ruleset”import { defaultRuleSet, mergeRuleSets, restrictRuleSetToTargets, applyCliFilters } from 'purgeit';
const base = defaultRuleSet();const withUser = mergeRuleSets(base, config);
// Restrict to one or more rule names, or target groups defined in config.const restricted = restrictRuleSetToTargets(withUser, ['node_modules', 'dist']);
// Or apply the same CLI-style filters the headless/TUI paths use.const filtered = applyCliFilters(withUser, /* noGated */ false, /* targets */ ['node_modules']);import { scan } from 'purgeit';
for await (const event of scan('/path/to/projects', ruleSet, { mode: 'projects' })) { if (event.type === 'found') { console.log('found', event.entry.path); } else if (event.type === 'size') { console.log('size', event.path, event.bytes); } else if (event.type === 'done') { console.log('total', event.totalBytes); }}Delete
Section titled “Delete”import { deleteEntries } from 'purgeit';
for await (const event of deleteEntries(paths, { dryRun: true })) { console.log(event);}Exclude matcher
Section titled “Exclude matcher”import { createExcludeMatcher } from 'purgeit';
const isExcluded = createExcludeMatcher(root, ['legacy/*', '*.log']);console.log(isExcluded('/root/legacy/dist')); // trueExported types include:
ScanEntry,ScanEvent,ScanOptionsDeleteEvent,DeleteOptionsArtifactRule,Gate,GateContext,ResolvedRuleSet,ValidationWarningPurgeitUserConfig,UserGatedRule,GateConditionLoadConfigOptions,LoadedConfig
Gated rules
Section titled “Gated rules”You can provide a custom Gate function in user configs:
export default { gated: [ { name: 'build', gate: (ctx) => ctx.siblingFile('package.json') && !ctx.siblingFile('keep-build'), }, ],};The GateContext provides:
path: the candidate pathparent: the candidate’s parent directorysiblingFile(name): true if a sibling file existssiblingGlob(pattern): true if any sibling entry matches the globsiblingGrep(name, pattern): true if the sibling file exists and matches the regex
CLI-style filters
Section titled “CLI-style filters”applyCliFilters is the same helper used by the headless runner and the TUI to apply --no-gated and --targets after merging user config:
import { applyCliFilters } from 'purgeit';
const ruleSet = applyCliFilters(withUser, /* noGated */ true, /* targets */ ['frontend']);restrictRuleSetToTargets is the lower-level helper that expands target group names to their member rule names and returns a ruleset containing only those names.