mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 11:33:10 +00:00
refactor(config): share the final YAML member edit (#15766)
This commit is contained in:
1 parent
142bc62471
commit
c19f474453
3 files changed
+45
-70
No files matched your search
@@ -12,15 +12,12 @@ import {
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
import {
|
||||
copyYamlAliasSource,
|
||||
preserveYamlAliasValues,
|
||||
resolveYamlEditPath,
|
||||
} from "./YamlConfigurationAliases.js";
|
||||
import {
|
||||
materializeUnresolvedYamlAliases,
|
||||
retainYamlConfigurationSource,
|
||||
editYamlConfigurationMember,
|
||||
initializeEmptyYamlMapping,
|
||||
yamlConfigurationValue,
|
||||
yamlAliasValues,
|
||||
retainYamlScalarSource,
|
||||
} from "./YamlConfigurationSource.js";
|
||||
|
||||
@@ -255,29 +252,6 @@ export const editGooseConfigurationYaml = (
|
||||
);
|
||||
}
|
||||
const sourcePath = resolveYamlEditPath(document, path);
|
||||
const previous = document.getIn(sourcePath, true);
|
||||
const parent = document.getIn(sourcePath.slice(0, -1), true);
|
||||
const removedKey =
|
||||
value === undefined && isMap(parent)
|
||||
? parent.items.find((pair) => pair.key === sourcePath.at(-1))?.key
|
||||
: undefined;
|
||||
const aliasValues = yamlAliasValues(previous, document);
|
||||
const retained = preserveYamlAliasValues(document, [previous, removedKey]);
|
||||
if (value === undefined) {
|
||||
if (!document.deleteIn(sourcePath)) return text;
|
||||
} else {
|
||||
document.setIn(
|
||||
sourcePath,
|
||||
isMap(previous) || isSeq(previous)
|
||||
? retainYamlConfigurationSource(
|
||||
previous,
|
||||
document.createNode(value),
|
||||
retained,
|
||||
document,
|
||||
)
|
||||
: value,
|
||||
);
|
||||
materializeUnresolvedYamlAliases(document, aliasValues);
|
||||
}
|
||||
if (!editYamlConfigurationMember(document, sourcePath, value)) return text;
|
||||
return document.toString({ lineWidth: 0 });
|
||||
};
|
||||
@@ -16,16 +16,13 @@ import {
|
||||
} from "yaml";
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
import {
|
||||
preserveYamlAliasValues,
|
||||
copyYamlAliasSource,
|
||||
resolveYamlEditPath,
|
||||
} from "./YamlConfigurationAliases.js";
|
||||
import {
|
||||
materializeUnresolvedYamlAliases,
|
||||
retainYamlConfigurationSource,
|
||||
editYamlConfigurationMember,
|
||||
initializeEmptyYamlMapping,
|
||||
yamlConfigurationValue,
|
||||
yamlAliasValues,
|
||||
retainYamlScalarSource,
|
||||
yamlNumericValuesEqual,
|
||||
} from "./YamlConfigurationSource.js";
|
||||
@@ -412,46 +409,15 @@ export const editHermesConfigurationYaml = (
|
||||
)
|
||||
inheritedYamlMapOverride(document, inheritedSourcePath, path.at(-1)!);
|
||||
const sourcePath = resolveYamlEditPath(document, path);
|
||||
const previous = document.getIn(sourcePath, true);
|
||||
// Deletion removes the pair's key too; replacement retains that key node.
|
||||
const parent =
|
||||
value === undefined
|
||||
? document.getIn(sourcePath.slice(0, -1), true)
|
||||
: undefined;
|
||||
if (
|
||||
value === undefined &&
|
||||
!document.hasIn(sourcePath) &&
|
||||
projectedYamlPathExists(document, path)
|
||||
)
|
||||
throw inheritedRegistrationRemovalError();
|
||||
const removedKey = isMap(parent)
|
||||
? parent.items.find((pair) => pair.key === sourcePath.at(-1))?.key
|
||||
: undefined;
|
||||
const aliasValues = yamlAliasValues(previous, document);
|
||||
const retainedSources = preserveYamlAliasValues(document, [
|
||||
previous,
|
||||
removedKey,
|
||||
]);
|
||||
if (value === undefined) {
|
||||
if (!document.deleteIn(sourcePath)) return text;
|
||||
if (projectedYamlPathExists(document, path))
|
||||
throw inheritedRegistrationRemovalError();
|
||||
} else {
|
||||
// Let YAML's scalar update preserve numeric formats for a direct edit.
|
||||
// Replacing containers needs explicit retention of unchanged child nodes.
|
||||
document.setIn(
|
||||
sourcePath,
|
||||
isMap(previous) || isSeq(previous)
|
||||
? retainYamlConfigurationSource(
|
||||
previous,
|
||||
document.createNode(value),
|
||||
retainedSources,
|
||||
document,
|
||||
)
|
||||
: value,
|
||||
);
|
||||
materializeUnresolvedYamlAliases(document, aliasValues);
|
||||
}
|
||||
if (!editYamlConfigurationMember(document, sourcePath, value)) return text;
|
||||
if (value === undefined && projectedYamlPathExists(document, path))
|
||||
throw inheritedRegistrationRemovalError();
|
||||
// Editing REA must not reflow unrelated user prompts or other YAML scalars.
|
||||
return document.toString({
|
||||
lineWidth: 0,
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
} from "yaml";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
import { preserveYamlAliasValues } from "./YamlConfigurationAliases.js";
|
||||
import { retainYamlSequenceAnnotations } from "./YamlSequenceAnnotations.js";
|
||||
|
||||
export const yamlNumericValuesEqual = (
|
||||
@@ -85,13 +86,13 @@ const containsRetainedSource = (
|
||||
};
|
||||
|
||||
/** What each alias inside `root` resolves to before an edit. */
|
||||
export interface YamlAliasBinding {
|
||||
interface YamlAliasBinding {
|
||||
readonly target: Node;
|
||||
readonly value: unknown;
|
||||
}
|
||||
|
||||
/** The node and value each alias inside `root` resolves to before an edit. */
|
||||
export const yamlAliasValues = (
|
||||
const yamlAliasValues = (
|
||||
root: unknown,
|
||||
document: Document,
|
||||
): ReadonlyMap<Alias, YamlAliasBinding> => {
|
||||
@@ -112,7 +113,7 @@ export const yamlAliasValues = (
|
||||
* its anchor is gone or an earlier same-name anchor now captures it, with the
|
||||
* value it resolved to before the edit. Its comments and spacing are kept.
|
||||
*/
|
||||
export const materializeUnresolvedYamlAliases = (
|
||||
const materializeUnresolvedYamlAliases = (
|
||||
document: Document,
|
||||
bindings: ReadonlyMap<Alias, YamlAliasBinding>,
|
||||
): void => {
|
||||
@@ -133,8 +134,42 @@ export const materializeUnresolvedYamlAliases = (
|
||||
});
|
||||
};
|
||||
|
||||
/** Edit the resolved member while preserving its source and surviving aliases. */
|
||||
export const editYamlConfigurationMember = (
|
||||
document: Document,
|
||||
sourcePath: readonly (string | Node)[],
|
||||
value: unknown,
|
||||
): boolean => {
|
||||
const previous = document.getIn(sourcePath, true);
|
||||
const parent =
|
||||
value === undefined
|
||||
? document.getIn(sourcePath.slice(0, -1), true)
|
||||
: undefined;
|
||||
const removedKey = isMap(parent)
|
||||
? parent.items.find((pair) => pair.key === sourcePath.at(-1))?.key
|
||||
: undefined;
|
||||
const aliasValues = yamlAliasValues(previous, document);
|
||||
const retained = preserveYamlAliasValues(document, [previous, removedKey]);
|
||||
if (value === undefined) return document.deleteIn(sourcePath);
|
||||
|
||||
// Let YAML preserve scalar formatting. Containers retain unchanged child nodes.
|
||||
document.setIn(
|
||||
sourcePath,
|
||||
isMap(previous) || isSeq(previous)
|
||||
? retainYamlConfigurationSource(
|
||||
previous,
|
||||
document.createNode(value),
|
||||
retained,
|
||||
document,
|
||||
)
|
||||
: value,
|
||||
);
|
||||
materializeUnresolvedYamlAliases(document, aliasValues);
|
||||
return true;
|
||||
};
|
||||
|
||||
/** Keep source nodes and annotations when updating a value. */
|
||||
export const retainYamlConfigurationSource = (
|
||||
const retainYamlConfigurationSource = (
|
||||
previous: unknown,
|
||||
next: Node,
|
||||
retainedSources: ReadonlySet<Node>,
|
||||
|
||||
Reference in new issue
Block a user