From 8af1e08a20dd27b6a796cf20f8c8722d6c1dedb7 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Tue, 21 Mar 2023 14:05:45 +0300 Subject: [PATCH] fix: do no mutate __metadata closes #287 --- src/main/ts/lockfile/v2.ts | 7 ++++--- src/main/ts/util.ts | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/ts/lockfile/v2.ts b/src/main/ts/lockfile/v2.ts index ff5e45b7..8d7c13ec 100644 --- a/src/main/ts/lockfile/v2.ts +++ b/src/main/ts/lockfile/v2.ts @@ -4,10 +4,11 @@ import { TLockfileEntry, TLockfileObject, } from '../ifaces' -import { formatFlags, formatYaml, invoke, mapFlags, parseYaml } from '../util' +import {addHiddenProp, formatFlags, formatYaml, invoke, mapFlags, parseYaml} from '../util' export const parse = (raw: string): TLockfileObject => { const data = parseYaml(raw) + const {__metadata} = data delete data.__metadata return Object.entries(data).reduce>( @@ -17,7 +18,7 @@ export const parse = (raw: string): TLockfileObject => { }) return m }, - {}, + addHiddenProp({}, '__metadata', __metadata), ) } @@ -66,7 +67,7 @@ export const format = (lockfile: TLockfileObject): string => { return m }, { - __metadata: { + __metadata: lockfile.__metadata || { version: 5, cacheKey: 8, }, diff --git a/src/main/ts/util.ts b/src/main/ts/util.ts index 59b19ebd..b1906cdc 100644 --- a/src/main/ts/util.ts +++ b/src/main/ts/util.ts @@ -216,3 +216,9 @@ export const getBinVersion = (bin: string, cwd = process.cwd()): string => export const getSelfManifest = () => readJson( join(pkgDir(__dirname) + '', 'package.json'), // eslint-disable-line ) + +export const addHiddenProp = (obj: Record, prop: string, value: any) => + Object.defineProperty(obj, prop, { + value, + enumerable: false + })