// pnpm 12's map-or-section settings: the default `catalog` and `overrides` // take the plain package map, or a section holding it under one key beside a // few options. A restatement of `pnpm_workspace::map_settings` (vendored at // `.repos/pnpm/pnpm/crates/workspace/src/map_settings.rs` and // `map_settings/specifiers.rs`, whose `ReadSection` visitor is the rule). import { Predicate, Result, Schema } from "effect"; import { UnusedPolicy, closed } from "./internal/sections.js"; import { OverrideKey } from "./OverrideKey.js"; /** * A pnpm 12 setting that takes a package map or a section holding it: * `"catalog"` (the default catalog, the map under `entries`) or * `"overrides"` (the map under `rules`). * * @public */ export type PnpmPackageMapSetting = "catalog" | "overrides"; /** * What {@link PnpmPackageMap.read} finds in a map-or-section value. * * @public */ export interface PnpmPackageMapRead { /** The package map: the value itself, or the section's nested map. Not checked to be a map of strings. */ readonly map: unknown; /** Whether the value was the section form. */ readonly section: boolean; /** The section's other keys (its options, or stray packages); empty for the plain map. */ readonly options: Readonly>; } const CATALOG_MODES = ["manual", "strict", "prefer"] as const; const CATALOG_UNUSED = ["ignore", "prune"] as const; /** The key each setting's section holds its map under. */ const SECTION_KEY = { catalog: "entries", overrides: "rules" } as const; /** * The options each section accepts beside its map, with the values each takes * (`null`: any string). The schemas below spell the same table. */ const SECTION_OPTIONS: Readonly | null>>>> = { catalog: { mode: CATALOG_MODES, defaultSaveTarget: null, unused: CATALOG_UNUSED }, overrides: { unused: UnusedPolicy.literals }, }; /** A mapping of strings to strings. */ const StringRecord = Schema.Record(Schema.String, Schema.String); /** * The `overrides` map: every key a selector `OverrideKey` parses, every value a * string. Checked over the whole record: a `Record` key schema drops a key that * fails it instead of reporting it. */ const Overrides = StringRecord.check( Schema.makeFilter((record) => { for (const key of Object.keys(record)) { const parsed = OverrideKey.parseResult(key); if (Result.isFailure(parsed)) return parsed.failure.message; } return true; }), ); /** Create an own data property, so a `__proto__` option name stays inert. */ const define = (target: Record, key: string, value: unknown): void => { Object.defineProperty(target, key, { value, writable: true, enumerable: true, configurable: true }); }; /** * Why pnpm 12 refuses a map-or-section setting's section. * * @remarks * `"UnknownOption"`: a key beside the section's map that is not one of its * options, which pnpm refuses with "put all package specifiers inside the * nested map". `"InvalidOption"`: an option that is not a string, or not one * of the values it takes. A problem with the package map itself is not this * error: {@link PnpmPackageMap.readResult} leaves the map to the caller. * * @public */ export class PnpmPackageMapError extends Schema.TaggedError()("PnpmPackageMapError", { /** The setting read. */ setting: Schema.Literals(["catalog", "overrides"]), /** The section key pnpm refused. */ option: Schema.String, /** What was wrong with it. */ code: Schema.Literals(["UnknownOption", "InvalidOption"]), /** The value the key held. */ value: Schema.Unknown, }) { override get message(): string { const allowed = Object.hasOwn(SECTION_OPTIONS[this.setting], this.option) ? SECTION_OPTIONS[this.setting][this.option] : null; return this.code === "UnknownOption" ? `"${this.setting}" declares "${this.option}" beside its "${SECTION_KEY[this.setting]}" section; put all package specifiers inside the nested map` : allowed === null || allowed === undefined || typeof this.value !== "string" ? `"${this.setting}.${this.option}" must be a string` : `"${this.setting}.${this.option}" must be one of ${allowed.join(", ")}, got "${this.value}"`; } } /** * pnpm 12's map-or-section settings, the default `catalog` and `overrides`, * as schemas and as the structural rule a `pnpm-workspace.yaml` reader applies. * * @remarks * Each setting is the plain package map, or a section holding it under one * key (`catalog.entries`, `overrides.rules`) beside a few options. The value * is the section exactly when that key holds a mapping; the key holding a * specifier string is a package of the plain map, so a package named * `entries` still reads. Named `catalogs` have no section form. * * Read flat, a section's keys become packages named `entries` and `mode`, and * every entry under it vanishes: that is the misreading this rule exists to * prevent, and why a reader should not restate it. * * The root entry carries these so a reader need not import the * `@effected/pnpm/settings` catalog, whose `catalog` and `overrides` entries * hold these same schemas. * * @public */ export class PnpmPackageMap { private constructor() {} /** * The default catalog, as pnpm 12's `DefaultCatalog` reads it: the map of * dependency names to version ranges, or a closed section with the map under * `entries` beside `mode` (`manual`, `strict` or `prefer`), * `defaultSaveTarget` (a catalog name) and `unused` (`ignore` or `prune`). * * @remarks * The section is tried first; an `entries` key holding a version range is a * package of the map. */ static readonly catalog = Schema.Union([ closed({ entries: StringRecord, mode: Schema.optionalKey(Schema.Literals(CATALOG_MODES)), defaultSaveTarget: Schema.optionalKey(Schema.String), unused: Schema.optionalKey(Schema.Literals(CATALOG_UNUSED)), }), StringRecord, ]); /** * `overrides`, as pnpm 12's `Rules` reads it: the map, its keys * {@link OverrideKey} selectors, or a closed section with the map under * `rules` beside `unused` (`warn` or `ignore`). * * @remarks * The section is tried first; a `rules` key holding a version is a package * of the map. */ static readonly overrides = Schema.Union([ closed({ rules: Overrides, unused: Schema.optionalKey(UnusedPolicy) }), Overrides, ]); /** * Split a parsed setting value into its package map and section options, * tolerantly: the options are not checked, and neither is the map. * * @remarks * Total. A value that is not a mapping (absent, `null`, an array, a scalar) * is returned as the `map` with `section: false`, for the caller to judge. * Option names are copied as own data properties, so a `__proto__` key is * inert. * * @example * ```ts * import { PnpmPackageMap } from "@effected/pnpm"; * * const read = PnpmPackageMap.read({ mode: "strict", entries: { effect: "^4.0.0" } }, "catalog"); * console.log(read.section, read.map, read.options); * // => true { effect: "^4.0.0" } { mode: "strict" } * ``` */ static readonly read = (value: unknown, setting: PnpmPackageMapSetting): PnpmPackageMapRead => { const key = SECTION_KEY[setting]; // `Predicate.isObject` refuses null and arrays: only a mapping opens a section. if (!Predicate.isObject(value) || !Object.hasOwn(value, key) || !Predicate.isObject(value[key])) { return { map: value, section: false, options: {} }; } const options: Record = {}; for (const [name, option] of Object.entries(value)) if (name !== key) define(options, name, option); return { map: value[key], section: true, options }; }; /** * {@link PnpmPackageMap.read}, failing as pnpm 12 does on a section option * it refuses. * * @remarks * Fails {@link PnpmPackageMapError} on the first refused option, in key * order. The package map is still not checked: hold it to the plain map's * rules yourself, or decode the whole value with * {@link PnpmPackageMap.catalog} or {@link PnpmPackageMap.overrides}. An * option set to `null` is refused, as the schemas refuse it. */ static readonly readResult = ( value: unknown, setting: PnpmPackageMapSetting, ): Result.Result => { const read = PnpmPackageMap.read(value, setting); const accepted = SECTION_OPTIONS[setting]; for (const [option, held] of Object.entries(read.options)) { const allowed = Object.hasOwn(accepted, option) ? accepted[option] : undefined; if (allowed === undefined) { return Result.fail(PnpmPackageMapError.make({ setting, option, code: "UnknownOption", value: held })); } if (typeof held !== "string" || (allowed !== null && !allowed.includes(held))) { return Result.fail(PnpmPackageMapError.make({ setting, option, code: "InvalidOption", value: held })); } } return Result.succeed(read); }; }