Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568/** The config store factory. Rewrites upstream `conf` 15.1.0's `Conf` class as {@link createConf}, a factory returning one frozen store object, because this repository bans classes for long-lived stateful objects. Behavior, error texts, and the file format stay upstream's: a JSON config file with defaults, schema validation, dot-notation access, optional encryption, migrations, watching, and change events. @module */
import path from 'node:path';
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';import { tagged, type Logger,} from '@monochromatic-dev/module-logger/ts';
import { createCrud, type CrudApi,} from './conf-crud.ts';import { createEventMethods, type EventApi,} from './conf-events.ts';import { createPlainObject, getStoreValue, hasStoreValue, isStoreContentEqual, withStoreValue,} from './store-access.ts';import { INTERNAL_KEY, MIGRATION_KEY, isReservedKeyPath,} from './internal-key.ts';import { createStoreFile, isMissingFileError, type StoreFile,} from './store-file.ts';import { applyMigrations, } from './migrate.ts';import { prepareOptions, type PreparedOptions,} from './prepare-options.ts';import { createSchemaValidator, } from './schema.ts';import { createConfigWatcher, type ConfigWatcher,} from './watcher.ts';import type { MigrationHost, } from './migration-host.ts';import type { Conf, } from './conf-type.ts';import type { Options, } from './options.ts';
//region Helpers
/** Resolves the config file path from prepared options, with upstream `conf`'s extension handling. @param options - Prepared store options. @returns Absolute config file path. @example ```ts resolveConfigPath(prepared); // => '/home/me/.config/foo-nodejs/config.json' ``` */function resolveConfigPath<T extends Record<string, unknown>>(options: PreparedOptions<T>,): string { /** Extension suffix including its leading dot; empty when the extension is empty. */ const extensionSuffix = options.fileExtension === '' ? '' : `.${options.fileExtension}`; return path.resolve( options.cwd, `${options.configName}${extensionSuffix}`, );}
/** Mutable per-store runtime state shared by the store's closures. @example ```ts const state: StoreState<Record<string, unknown>> = { isInMigration: false, }; ``` */type StoreState<T extends Record<string, unknown>> = { /** While true, reads and writes skip schema validation, as upstream `conf` does during migrations. */ isInMigration: boolean; /** Memoized user store when `cache` is enabled. */ cachedStore?: T; /** Running file watcher when `watch` is enabled. */ watcher?: ConfigWatcher;};
/** Marks the migration window so validation is bypassed while steps run and restored when the scope ends. @param state - Store state whose flag is managed. @returns Disposable resetting the flag at scope exit. @example ```ts { using _scope = createMigrationScope(state); applyMigrations({ host, migrations, projectVersion: '1.0.0', }); } ``` */function createMigrationScope<T extends Record<string, unknown>>(state: StoreState<T>,): { readonly [Symbol.dispose]: () => void;} { state.isInMigration = true; return { [Symbol.dispose]: function leaveMigrationScope(): void { state.isInMigration = false; }, };}
/** Serializes the store like upstream `conf`'s default: indented JSON. @param value - Store object to serialize. @returns JSON text written to the config file. @example ```ts defaultSerialize({ theme: 'dark', }); ``` */function defaultSerialize(value: unknown,): string { return JSON.stringify( value, undefined, '\t', );}
//endregion Helpers
//region Factory
/** Creates one config store bound to a config file. @param options - Store configuration; pass `cwd` or `projectName` to locate the file. @returns Frozen store object persisting to the resolved config file. @throws InvalidEncryptionAlgorithmError when `encryptionAlgorithm` names an unsupported algorithm. @throws MissingProjectNameError when neither `cwd` nor `projectName` resolves a config directory. @throws MissingProjectVersionError when `migrations` is set without `projectVersion`. @example ```ts const config = createConf({ projectName: 'foo', defaults: { theme: 'light', }, }); ``` */export function createConf<T extends Record<string, unknown> = Record<string, unknown>>(options: Options<T> = {},): Conf<T> { /** Options with defaults filled and paths resolved. */ const prepared = prepareOptions(options,); /** Schema validator carrying the schema's declared defaults. */ const validator = createSchemaValidator(prepared,); /** Logger wrapped with this factory's name so store diagnostics name their origin. */ const log: Logger = prepared.logger ?? tagged({ tag: createConf.name, },); /** Mutable runtime state shared by the closures below. */ const state: StoreState<T> = { isInMigration: false, }; /** Absolute config file path. */ const configPath = resolveConfigPath(prepared,); /** Deserializes config text with JSON when no custom deserializer is set. @param text - Config file text to parse. @returns Parsed store contents. */ function deserializeWithJson(text: string,): T { // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- JSON.parse's dynamic output becomes the caller's store type; no narrower static type exists for parsed JSON. return JSON.parse(text,) as T; } /** Validation hook honoring the migration bypass, mirroring upstream `conf`'s `#isInMigration` gate. @param data - Parsed store contents to validate. */ function validateParsedStore(data: unknown,): void { if (state.isInMigration) return; validator.validate(data,); } /** Read/write pipeline over the config file. */ const storeFile = createStoreFile<T>({ path: configPath, ...(prepared.encryptionKey === undefined ? {} : { encryptionKey: prepared.encryptionKey, }), encryptionAlgorithm: prepared.encryptionAlgorithm, serialize: prepared.serialize ?? defaultSerialize, deserialize: prepared.deserialize ?? deserializeWithJson, clearInvalidConfig: prepared.clearInvalidConfig, configFileMode: prepared.configFileMode, validate: validateParsedStore, logger: log, },); /** Event target backing the change subscriptions. */ const events = new EventTarget(); /** Default values: schema defaults overwritten by `defaults`, exactly as upstream `conf` merges them. */ const defaultValues: Record<string, unknown> = { ...validator.schemaDefaults, ...prepared.defaults, };
/** Drops the memoized store so the next read hits the file. */ function dropCache(): void { delete state.cachedStore; }
/** Reads the user-visible store: file contents validated and stripped of reserved bookkeeping keys. @returns User-visible store contents. */ function readUserStore(): T { /** File contents including bookkeeping. */ const store = storeFile.readStore(); for (const key of Object.keys(store)) { if (isReservedKeyPath(key,)) Reflect.deleteProperty( store, key, ); } // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- file contents are parsed into a generic dictionary and surfaced as the caller's store type; the shape proof lives with the schema validator. return store as T; }
/** Reads the whole store, through the cache when caching is enabled. @returns Whole user-visible store. */ function getStore(): T { if (prepared.cache !== true) return readUserStore(); state.cachedStore ??= readUserStore(); return state.cachedStore; }
/** Writes raw store contents, dropping the cache first like upstream `conf`'s `_write`. @param store - Store contents to persist. */ function writeStore(store: Record<string, unknown>,): void { dropCache(); storeFile.writeStore(store,); }
/** Merges the reserved bookkeeping key back into a replacement store, leaving the caller's object untouched. @param value - Replacement store from the caller. @returns Store to persist, carrying preserved bookkeeping. */ function preserveInternalKey(value: Record<string, unknown>,): Record<string, unknown> { if (hasStoreValue({ store: value, key: INTERNAL_KEY, accessPropertiesByDotNotation: true, },)) return value; try { /** Current file contents carrying any bookkeeping to preserve. */ const currentStore = storeFile.readParsedFile(); if (!hasStoreValue({ store: currentStore, key: INTERNAL_KEY, accessPropertiesByDotNotation: true, },)) return value; /** Caller value copy carrying the preserved bookkeeping. */ const merged = createPlainObject(); Object.assign( merged, value, ); return withStoreValue({ store: merged, key: INTERNAL_KEY, value: getStoreValue({ store: currentStore, key: INTERNAL_KEY, accessPropertiesByDotNotation: true, },), accessPropertiesByDotNotation: true, },); } catch (error) { if (!isMissingFileError(error,)) log.debug(`internal-data preservation skipped: ${caughtValueText(error,)}`,); return value; } }
/** Replaces the whole store the way the `store` setter does: preserve bookkeeping, validate, persist, dispatch `change`. @param value - Replacement store contents. */ function assignStore(value: Record<string, unknown>,): void { storeFile.ensureDirectory(); /** Value actually persisted. */ const valueToWrite = preserveInternalKey(value,); if (!state.isInMigration) validateParsedStore(valueToWrite,); writeStore(valueToWrite,); events.dispatchEvent(new Event('change',),); }
/** Initializes a non-migrating store: merge defaults, validate, and persist only when the merge changed something. */ function initializeStore(): void { /** File contents before the defaults merge. */ const fileStore = storeFile.readStore(); /** File contents with defaults filled in for missing keys. */ const storeWithDefaults = Object.assign( createPlainObject(), prepared.defaults ?? {}, fileStore, ); validateParsedStore(storeWithDefaults,); if (!isStoreContentEqual({ left: fileStore, right: storeWithDefaults, },)) assignStore(storeWithDefaults,); }
/** CRUD methods bound to this store's context. */ const crud: CrudApi = createCrud<T>({ options: prepared, defaultValues, storeFile, getStore, assignStore, },); /** Change-subscription methods bound to this store's context. */ const eventMethods: EventApi = createEventMethods<T>({ events, getStore, readValue: function readValue(key: string,): unknown { return crud.get(key,); }, },);
/** The frozen store object handed to the caller. */ const conf = { path: configPath, events, /** Item count of the user-visible store. */ get size(): number { return Object.keys(getStore(),) .length; }, /** The whole user-visible store; assigning replaces everything. */ get store(): T { return getStore(); }, /** Replaces the whole store contents. */ set store(value: T) { assignStore(value,); }, get: crud.get, set: crud.set, has: crud.has, appendToArray: crud.appendToArray, reset: crud.reset, delete: crud.delete, clear: crud.clear, onDidChange: eventMethods.onDidChange, onDidAnyChange: eventMethods.onDidAnyChange, closeWatcher: function closeWatcher(): void { state.watcher ?.close(); delete state.watcher; }, * [Symbol.iterator](): IterableIterator<[ keyof T, T[keyof T] ]> { for (const [key, value,] of Object.entries(getStore(),)) { /* oxlint-disable typescript/no-unsafe-type-assertion -- entry pairs come from a generic dictionary and are re-exposed as the caller's key and value types at this iteration boundary. */ yield [ key as keyof T, value as T[keyof T], ]; /* oxlint-enable typescript/no-unsafe-type-assertion */ } }, // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the implementation object satisfies Conf<T> member by member; the overload set is restated at this single factory edge because per-member overload assignability cannot be expressed on an object literal. } as unknown as Conf<T>; Object.freeze(conf,);
if (prepared.migrations !== undefined) { { /** Migration window suppressing validation while steps run. */ using _migrationScope = createMigrationScope(state,); applyMigrations<T>({ host: { store: conf, configFileExists: storeFile.fileExists, readRawStore: storeFile.readStore, readUserStore: getStore, writeUserStore: function writeUserStore(store: T,): void { assignStore(store,); }, writeStoreWithoutEvents: writeStore, recordVersion: function recordVersion(version: string,): void { /** File contents awaiting the recorded version. */ const store = storeFile.readStore(); assignStore(withStoreValue({ store, key: MIGRATION_KEY, value: version, accessPropertiesByDotNotation: true, },),); }, }, migrations: prepared.migrations, projectVersion: prepared.projectVersion ?? '', ...(prepared.defaults === undefined ? {} : { defaults: prepared.defaults, }), ...(prepared.beforeEachMigration === undefined ? {} : { beforeEachMigration: prepared.beforeEachMigration, }), },); } validateParsedStore(getStore(),); } else { initializeStore(); }
if (prepared.watch === true) { storeFile.ensureDirectory(); if (!storeFile.fileExists()) writeStore(createPlainObject(),); state.watcher = createConfigWatcher({ path: configPath, onChange: function onConfigFileChanged(): void { dropCache(); events.dispatchEvent(new Event('change',),); }, logger: log, },); }
return conf;}
//endregion Factory
export type { Conf, } from './conf-type.ts';