PackagesCoreReference

core — Functions

Functions and classes for the core package

Functions and classes exported by this package.

getCurrentUser()

Returns the current OS username, or "unknown" if unavailable.

Signature

() => string

Returns — The OS username string.

Example

import { getCurrentUser } from "@forge-ts/core/audit";
const user = getCurrentUser(); // e.g. "alice"

appendAuditEvent(rootDir, event)

Appends a single audit event to the .forge-audit.jsonl file.

Signature

(rootDir: string, event: AuditEvent) => void

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.
eventAuditEventThe audit event to record.

Example

import { appendAuditEvent } from "@forge-ts/core";
const event = { timestamp: new Date().toISOString(), event: "config.lock" as const, user: "alice", reason: "Stabilize", details: {} };
appendAuditEvent("/path/to/project", event);

readAuditLog(rootDir, options)

Reads the .forge-audit.jsonl file and returns parsed audit events.

Signature

(rootDir: string, options?: ReadAuditOptions) => AuditEvent[]

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.
options: ReadAuditOptionsOptional limit and event type filter.

Returns — Array of audit events, newest first.

Example

import { readAuditLog } from "@forge-ts/core";
const events = readAuditLog("/path/to/project", { limit: 10 });
console.log(events.length); // up to 10

formatAuditEvent(event)

Formats a single audit event as a human-readable string.

Signature

(event: AuditEvent) => string

Parameters

NameTypeDescription
eventAuditEventThe audit event to format.

Returns — A single-line human-readable representation.

Example

import { formatAuditEvent } from "@forge-ts/core";
const event = { timestamp: "2026-03-21T12:00:00.000Z", event: "config.lock" as const, user: "alice", reason: "Stabilize", details: {} };
const line = formatAuditEvent(event);
console.log(line);

createBypass(rootDir, reason, rule, config)

Creates a new bypass record, writes it to .forge-bypass.json, and appends an audit event.

Signature

(rootDir: string, reason: string, rule?: string, config?: Partial<BypassConfig>) => BypassRecord

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.
reasonstringMandatory justification for the bypass.
rule: stringSpecific rule code to bypass (e.g., "E009"), or "all". Defaults to "all".
config: Partial<BypassConfig>Optional bypass budget configuration overrides.

Returns — The created bypass record.

Throws

  • Error when the daily bypass budget is exhausted.

Example

import { createBypass } from "@forge-ts/core";
const bypass = createBypass("/path/to/project", "hotfix for release", "E009", { dailyBudget: 5, durationHours: 12 });
console.log(bypass.id);

getActiveBypasses(rootDir)

Returns all currently active (non-expired) bypass records.

Signature

(rootDir: string) => BypassRecord[]

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.

Returns — Array of active bypass records.

Example

import { getActiveBypasses } from "@forge-ts/core";
const active = getActiveBypasses("/path/to/project");
console.log(`${active.length} active bypass(es)`);

isRuleBypassed(rootDir, ruleCode)

Checks whether a specific rule has an active bypass.

Signature

(rootDir: string, ruleCode: string) => boolean

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.
ruleCodestringThe rule code to check (e.g., "E009", "E010").

Returnstrue if the rule is currently bypassed.

Example

import { isRuleBypassed } from "@forge-ts/core";
if (isRuleBypassed("/path/to/project", "E009")) {
  console.log("E009 is currently bypassed");
}

getRemainingBudget(rootDir, config)

Returns the number of bypass budget slots remaining for today.

Signature

(rootDir: string, config?: Partial<BypassConfig>) => number

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.
config: Partial<BypassConfig>Optional bypass budget configuration overrides.

Returns — Number of remaining bypass slots for today.

Example

import { getRemainingBudget } from "@forge-ts/core";
const remaining = getRemainingBudget("/path/to/project", { dailyBudget: 5, durationHours: 24 });
console.log(`${remaining} bypass(es) remaining today`);

expireOldBypasses(rootDir)

Removes expired bypass records from .forge-bypass.json.

Signature

(rootDir: string) => number

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root directory.

Returns — The number of expired records removed.

Example

import { expireOldBypasses } from "@forge-ts/core";
const removed = expireOldBypasses("/path/to/project");
console.log(`${removed} expired bypass(es) removed`);

defineConfig(config)

Type-safe helper for defining a partial forge-ts configuration.

Signature

(config: Partial<ForgeConfig>) => Partial<ForgeConfig>

Parameters

NameTypeDescription
configPartial<ForgeConfig>Partial configuration overrides.

Returns — The same object (identity function for type checking).

Example

import { defineConfig } from "@forge-ts/core";

const config = { outDir: "docs", enforce: { strict: true } };
export default defineConfig(config);

defaultConfig(rootDir)

Constructs a sensible default ForgeConfig rooted at rootDir.

Signature

(rootDir: string) => ForgeConfig

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root.

Returns — A fully-populated default configuration.

Example

import { defaultConfig } from "@forge-ts/core";
const config = defaultConfig("/path/to/project");
console.log(config.enforce.enabled); // true

loadConfig(rootDir)

Loads the forge-ts configuration for a project.

Signature

(rootDir?: string) => Promise<ForgeConfig>

Parameters

NameTypeDescription
rootDir: stringThe project root to search for config. Defaults to process.cwd().

Returns — A fully-resolved ForgeConfig.

Example

import { loadConfig } from "@forge-ts/core";
const config = await loadConfig("/path/to/project");
// config is fully resolved with defaults

readLockFile(rootDir)

Reads the .forge-lock.json file from the given project root.

Signature

(rootDir: string) => ForgeLockManifest | null

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root.

Returns — The parsed lock manifest, or null if no lock file exists or is invalid.

Example

import { readLockFile } from "@forge-ts/core";
const lock = readLockFile("/path/to/project");
if (lock) {
  console.log(`Locked at ${lock.lockedAt} by ${lock.lockedBy}`);
}

writeLockFile(rootDir, manifest)

Writes a ForgeLockManifest to .forge-lock.json in the project root.

Signature

(rootDir: string, manifest: ForgeLockManifest) => void

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root.
manifestForgeLockManifestThe lock manifest to write.

Example

import { writeLockFile, createLockManifest, loadConfig } from "@forge-ts/core";
const config = await loadConfig("/path/to/project");
const manifest = createLockManifest(config, "alice");
writeLockFile("/path/to/project", manifest);

removeLockFile(rootDir)

Removes the .forge-lock.json file from the project root.

Signature

(rootDir: string) => boolean

Parameters

NameTypeDescription
rootDirstringAbsolute path to the project root.

Returnstrue if the file existed and was removed, false otherwise.

Example

import { removeLockFile } from "@forge-ts/core";
const removed = removeLockFile("/path/to/project");
console.log(removed ? "Lock removed" : "No lock file found");

createLockManifest(config, lockedBy)

Creates a ForgeLockManifest from the current project config.

Signature

(config: ForgeConfig, lockedBy?: string) => ForgeLockManifest

Parameters

NameTypeDescription
configForgeConfigThe fully-resolved ForgeConfig to snapshot.
lockedBy: stringIdentifier of the user or agent creating the lock. Defaults to "forge-ts lock".

Returns — A new lock manifest ready to be written with writeLockFile.

Example

import { createLockManifest, loadConfig } from "@forge-ts/core";
const config = await loadConfig();
const manifest = createLockManifest(config, "alice");
console.log(manifest.config.rules);

validateAgainstLock(config, lock)

Validates the current config against a locked manifest.

Signature

(config: ForgeConfig, lock: ForgeLockManifest) => LockViolation[]

Parameters

NameTypeDescription
configForgeConfigThe current fully-resolved ForgeConfig.
lockForgeLockManifestThe lock manifest to validate against.

Returns — An array of LockViolation entries. Empty means no weakening detected.

Example

import { validateAgainstLock, readLockFile, loadConfig } from "@forge-ts/core";
const config = await loadConfig();
const lock = readLockFile(config.rootDir);
if (lock) {
  const violations = validateAgainstLock(config, lock);
  for (const v of violations) {
    console.error(`LOCK VIOLATION: ${v.message}`);
  }
}

resolveVisibility(tags)

Determines the visibility level of a symbol from its TSDoc release tags.

Signature

(tags: Record<string, string[]> | undefined) => Visibility

Parameters

NameTypeDescription
tagsRecord<stringThe parsed tags map from ForgeSymbol.documentation.

Returns — The resolved Visibility value.

Example

import { resolveVisibility } from "@forge-ts/core";
const vis = resolveVisibility({ internal: [] });
// vis === Visibility.Internal

meetsVisibility(candidate, minVisibility)

Returns whether candidate meets or exceeds the required minimum visibility.

Signature

(candidate: Visibility | "public" | "beta" | "internal" | "private", minVisibility: Visibility | "public" | "beta" | "internal" | "private") => boolean

Parameters

NameTypeDescription
candidateVisibility | "public" | "beta" | "internal" | "private"The visibility of the symbol being tested.
minVisibilityVisibility | "public" | "beta" | "internal" | "private"The minimum visibility threshold.

Returnstrue if candidate is at least as visible as minVisibility.

Example

import { meetsVisibility, Visibility } from "@forge-ts/core";
meetsVisibility(Visibility.Public, Visibility.Public); // true
meetsVisibility(Visibility.Internal, Visibility.Public); // false
meetsVisibility("public", "beta"); // true (string literals also accepted)

filterByVisibility(symbols, minVisibility)

Filters an array of ForgeSymbol objects to only include symbols whose visibility meets or exceeds minVisibility.

Signature

(symbols: ForgeSymbol[], minVisibility: Visibility | "public" | "beta" | "internal" | "private") => ForgeSymbol[]

Parameters

NameTypeDescription
symbolsForgeSymbol[]The full list of symbols to filter.
minVisibilityVisibility | "public" | "beta" | "internal" | "private"The minimum visibility threshold to keep.

Returns — A new array containing only symbols that pass the visibility check.

Example

import { filterByVisibility, Visibility } from "@forge-ts/core";
const publicOnly = filterByVisibility(symbols, Visibility.Public);

clearTSDocConfigCache()

Clears the TSDoc configuration cache. Intended for use in tests only.

Signature

() => void

loadTSDocConfiguration(folderPath)

Resolve the TSDoc configuration to use when parsing comments in files under folderPath. If a tsdoc.json file exists in or above the folder and can be loaded without errors, its settings are applied to a fresh TSDocConfiguration via TSDocConfigFile.configureParser(). Otherwise the default TSDocConfiguration is returned (backward-compatible behaviour). Results are cached per folder path so the file system is only consulted once per unique directory.

Signature

(folderPath: string) => TSDocConfiguration

Parameters

NameTypeDescription
folderPathstringAbsolute directory path of the source file being parsed.

Returns — A configured TSDocConfiguration instance.

createWalker(config)

Creates an ASTWalker configured for the given forge config.

Signature

(config: ForgeConfig) => ASTWalker

Parameters

NameTypeDescription
configForgeConfigThe resolved ForgeConfig for the project.

Returns — An ASTWalker instance whose walk() method performs the extraction.

Example

import { loadConfig, createWalker } from "@forge-ts/core";
const config = await loadConfig();
const walker = createWalker(config);
const symbols = walker.walk();
console.log(`Found ${symbols.length} symbols`);

On this page