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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317/** Migration bookkeeping and step execution. Mirrors upstream `conf` 15.1.0's migration semantics: versions may be concrete semver versions or ranges, steps run in ascending order, a failed step restores the store to its pre-step snapshot, and the recorded version lives under the reserved `__internal__` key. @module */
import { getProperty, } from 'dot-prop';import semver from 'semver';
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
import { MigrationFailedError, MissingProjectVersionError,} from './errors.ts';import { MIGRATION_KEY, } from './internal-key.ts';import { createPlainObject, isStoreContentEqual,} from './store-access.ts';import type { MigrationHost, } from './migration-host.ts';import type { BeforeEachMigrationCallback, Migrations,} from './types.ts';
//region Constants
/** Version recorded when the file holds nothing comparable. @example ```ts UNKNOWN_VERSION; // '0.0.0' ``` */const UNKNOWN_VERSION = '0.0.0';
//endregion Constants
//region Version predicates
/** Reports whether a recorded or configured version is a semver range rather than a concrete version. @param version - Version or range string as written by the caller or file. @returns `true` when the string is not a clean concrete version. @example ```ts isVersionInRangeFormat('>=2.0.0'); // => true isVersionInRangeFormat('1.0.0'); // => false ``` */export function isVersionInRangeFormat(version: string,): boolean { return semver.clean(version,) === null;}
/** Decides whether one migration entry applies in a pass. Ranges apply when the target version satisfies them and a previous migration has not already covered them; concrete versions apply when they sit after the last migrated version and at or below the target. @param candidateVersion - Migration key: a concrete version or a range. @param previousMigratedVersion - Last version recorded as migrated. @param versionToMigrate - Project version this pass targets. @returns `true` when the step should run. @example ```ts shouldPerformMigration({ candidateVersion: '1.0.0', previousMigratedVersion: '0.9.0', versionToMigrate: '1.0.0', }); // => true ``` */export function shouldPerformMigration({ candidateVersion, previousMigratedVersion, versionToMigrate,}: { readonly candidateVersion: string; readonly previousMigratedVersion: string; readonly versionToMigrate: string;},): boolean { if (isVersionInRangeFormat(candidateVersion,)) { if ((previousMigratedVersion !== UNKNOWN_VERSION) && semver.satisfies( previousMigratedVersion, candidateVersion, )) return false; return semver.satisfies( versionToMigrate, candidateVersion, ); } if (semver.lte( candidateVersion, previousMigratedVersion, )) return false; if (semver.gt( candidateVersion, versionToMigrate, )) return false; return true;}
//endregion Version predicates
//region Step execution
/** Runs every applicable migration step against the store, restoring the pre-step snapshot when a step throws. @param host - Store access surface this runner mutates through. @param migrations - Version-to-handler map from the `migrations` option. @param projectVersion - Target version for the whole pass. @param beforeEachMigration - Optional per-step hook. @throws MigrationFailedError When a step throws; the store is restored first. @example ```ts runMigrationSteps({ host, migrations: { '1.0.0': function toV1(store): void { store.delete('debugPhase'); }, }, projectVersion: '1.0.0', }); ``` */export function runMigrationSteps<T extends Record<string, unknown>>({ host, migrations, projectVersion, beforeEachMigration,}: { readonly host: MigrationHost<T>; readonly migrations: Migrations<T>; readonly projectVersion: string; readonly beforeEachMigration?: BeforeEachMigrationCallback<T>;},): void { /** Version recorded in the file, defaulting to the unknown sentinel. */ const storedVersion = getProperty( host.readRawStore(), MIGRATION_KEY, UNKNOWN_VERSION, ); /** Loop state: the version each step migrates from and the snapshot a failed step restores. */ const state = { previousMigratedVersion: isVersionInRangeFormat(storedVersion,) ? UNKNOWN_VERSION : storedVersion, backup: structuredClone(host.readUserStore(),), }; /** Migration keys that apply in this pass, in their configured order. */ const newerVersions = Object.keys(migrations,) .filter( function isCandidate(candidateVersion: string,): boolean { return shouldPerformMigration({ candidateVersion, previousMigratedVersion: state.previousMigratedVersion, versionToMigrate: projectVersion, },); }, ); for (const version of newerVersions) { try { beforeEachMigration?.({ store: host.store, context: { fromVersion: state.previousMigratedVersion, toVersion: version, finalVersion: projectVersion, versions: newerVersions, }, },); /** Migration handler for this version, absent for range keys that only order the pass. */ const migration = migrations[version]; migration?.(host.store,); if (!isVersionInRangeFormat(version,)) host.recordVersion(version,); state.previousMigratedVersion = version; state.backup = structuredClone(host.readUserStore(),); } catch (error) { host.writeUserStore(state.backup,); throw new MigrationFailedError({ reason: caughtValueText(error,), },); } } if (isVersionInRangeFormat(state.previousMigratedVersion,) || (!semver.eq( state.previousMigratedVersion, projectVersion, ))) host.recordVersion(projectVersion,);}
//endregion Step execution
//region Pass orchestration
/** Runs the whole migration pass for one store: merge defaults into an existing file, start a brand-new store at the current version without running steps, then run and record the steps. @param host - Store access surface this runner mutates through. @param migrations - Version-to-handler map from the `migrations` option. @param projectVersion - Target version for the whole pass. @param defaults - Default values merged into the store before steps run. @param beforeEachMigration - Optional per-step hook. @throws MissingProjectVersionError When no target version is provided. @throws MigrationFailedError When a step throws. @example ```ts applyMigrations({ host, migrations, projectVersion: '2.0.0', defaults: { theme: 'light', }, }); ``` */export function applyMigrations<T extends Record<string, unknown>>({ host, migrations, projectVersion, defaults, beforeEachMigration,}: { readonly host: MigrationHost<T>; readonly migrations: Migrations<T>; readonly projectVersion: string; readonly defaults?: Readonly<T>; readonly beforeEachMigration?: BeforeEachMigrationCallback<T>;},): void { if (projectVersion === '') throw new MissingProjectVersionError(); /** Whether the config file pre-existed this store; a store that does not exist yet has nothing to migrate. */ const isNewStore = !host.configFileExists(); /** File contents before defaults merge. */ const fileStore = host.readRawStore(); /** File contents with defaults filled in for missing keys. */ const storeWithDefaults = Object.assign( createPlainObject(), defaults ?? {}, fileStore, ); if (!isStoreContentEqual({ left: fileStore, right: storeWithDefaults, },)) host.writeStoreWithoutEvents(storeWithDefaults,); if (isNewStore) { host.recordVersion(projectVersion,); return; } runMigrationSteps({ host, migrations, projectVersion, ...(beforeEachMigration === undefined ? {} : { beforeEachMigration }), },);}
//endregion Pass orchestration