diff --git a/README.md b/README.md index 0548012..4555cfe 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,11 @@ const ignoreFormat = createFormat({ const unique = (array) => Array.from(new Set() < T[number] > array).sort() await fs.writeFile(resolvedPath, unique(expected).join('\n'), 'utf8') }, + // Optional: define a 'clone' function to control how the result of 'read' is cloned before being passed to 'update' + // Defaults to `structuredClone`[1] if available, otherwise `v8.deserialize(v8.serialize(obj))`. + clone(actual) { + return myCustomClone(actual) + }, }) export default async (_workspaceDir) => { diff --git a/src/index.ts b/src/index.ts index 4cad7ec..09dd677 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,7 +88,11 @@ export async function performUpdates< _writeProjectManifest: writeProjectManifest, } const actual = (await fileExists(resolvedPath)) ? await formatPlugin.read(formatHandlerOptions) : null - const expected = await formatPlugin.update(clone(actual), updateFile as any, formatHandlerOptions) + const expected = await formatPlugin.update( + formatPlugin.clone ? formatPlugin.clone(actual, formatHandlerOptions) : clone(actual), + updateFile as any, + formatHandlerOptions, + ) const equal = (actual == null && expected == null) || (actual != null && expected != null && (await formatPlugin.equal(expected, actual, formatHandlerOptions))) diff --git a/src/updater/formatPlugin.ts b/src/updater/formatPlugin.ts index 2693f6f..33c9715 100644 --- a/src/updater/formatPlugin.ts +++ b/src/updater/formatPlugin.ts @@ -24,6 +24,13 @@ export interface FormatPlugin { /** Called only if write is required (`--test` isn't specified, `expected != null` and `expected` is not equal to `actual`) */ write(expected: Content, options: FormatPluginFnOptions): PromiseOrValue + + /** + * Used to clone the object returned by `read` before passing it to `update`. + * Defaults to `structuredClone`[1] if available, otherwise `v8.deserialize(v8.serialize(obj))`. + * [1]: https://developer.mozilla.org/en-US/docs/web/api/structuredclone + */ + clone?(value: Content, options: FormatPluginFnOptions): Content } export interface FormatPluginFnOptions {