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
() => stringReturns — 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) => voidParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute path to the project root directory. |
event | AuditEvent | The 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
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute path to the project root directory. |
options | : ReadAuditOptions | Optional 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 10formatAuditEvent(event)
Formats a single audit event as a human-readable string.
Signature
(event: AuditEvent) => stringParameters
| Name | Type | Description |
|---|---|---|
event | AuditEvent | The 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>) => BypassRecordParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute path to the project root directory. |
reason | string | Mandatory justification for the bypass. |
rule | : string | Specific 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
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute 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) => booleanParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute path to the project root directory. |
ruleCode | string | The rule code to check (e.g., "E009", "E010"). |
Returns — true 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>) => numberParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute 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) => numberParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute 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
| Name | Type | Description |
|---|---|---|
config | Partial<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) => ForgeConfigParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute 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); // trueloadConfig(rootDir)
Loads the forge-ts configuration for a project.
Signature
(rootDir?: string) => Promise<ForgeConfig>Parameters
| Name | Type | Description |
|---|---|---|
rootDir | : string | The 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 defaultsreadLockFile(rootDir)
Reads the .forge-lock.json file from the given project root.
Signature
(rootDir: string) => ForgeLockManifest | nullParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute 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) => voidParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute path to the project root. |
manifest | ForgeLockManifest | The 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) => booleanParameters
| Name | Type | Description |
|---|---|---|
rootDir | string | Absolute path to the project root. |
Returns — true 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) => ForgeLockManifestParameters
| Name | Type | Description |
|---|---|---|
config | ForgeConfig | The fully-resolved ForgeConfig to snapshot. |
lockedBy | : string | Identifier 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
| Name | Type | Description |
|---|---|---|
config | ForgeConfig | The current fully-resolved ForgeConfig. |
lock | ForgeLockManifest | The 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) => VisibilityParameters
| Name | Type | Description |
|---|---|---|
tags | Record<string | The parsed tags map from ForgeSymbol.documentation. |
Returns — The resolved Visibility value.
Example
import { resolveVisibility } from "@forge-ts/core";
const vis = resolveVisibility({ internal: [] });
// vis === Visibility.InternalmeetsVisibility(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") => booleanParameters
| Name | Type | Description |
|---|---|---|
candidate | Visibility | "public" | "beta" | "internal" | "private" | The visibility of the symbol being tested. |
minVisibility | Visibility | "public" | "beta" | "internal" | "private" | The minimum visibility threshold. |
Returns — true 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
| Name | Type | Description |
|---|---|---|
symbols | ForgeSymbol[] | The full list of symbols to filter. |
minVisibility | Visibility | "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
() => voidloadTSDocConfiguration(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) => TSDocConfigurationParameters
| Name | Type | Description |
|---|---|---|
folderPath | string | Absolute 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) => ASTWalkerParameters
| Name | Type | Description |
|---|---|---|
config | ForgeConfig | The 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`);