From af1e06b734b827ade3b435d59322bc69689145af Mon Sep 17 00:00:00 2001 From: elrrrrrrr Date: Fri, 4 Aug 2023 13:15:08 +0800 Subject: [PATCH] feat: use isEqual instead of json diff --- app/core/service/PackageSyncerService.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/app/core/service/PackageSyncerService.ts b/app/core/service/PackageSyncerService.ts index b608e9993..63612586b 100644 --- a/app/core/service/PackageSyncerService.ts +++ b/app/core/service/PackageSyncerService.ts @@ -8,7 +8,7 @@ import { Pointcut } from '@eggjs/tegg/aop'; import { EggHttpClient } from 'egg'; import { setTimeout } from 'timers/promises'; import { rm } from 'fs/promises'; -import { isEqual } from 'lodash'; +import { isEqual, isEmpty } from 'lodash'; import semver from 'semver'; import semverRcompare from 'semver/functions/rcompare'; import semverPrerelease from 'semver/functions/prerelease'; @@ -608,7 +608,7 @@ export class PackageSyncerService extends AbstractService { // fix _npmUser field since https://github.com/cnpm/cnpmcore/issues/553 const metaDataKeys = [ 'peerDependenciesMeta', 'os', 'cpu', 'libc', 'workspaces', 'hasInstallScript', 'deprecated', '_npmUser' ]; const ignoreInAbbreviated = ['_npmUser']; - let diffMeta: Partial; + let diffMeta: Partial = {}; for (const key of metaDataKeys) { let remoteItemValue = item[key]; // make sure hasInstallScript exists @@ -617,22 +617,18 @@ export class PackageSyncerService extends AbstractService { remoteItemValue = true; } } - const remoteItemDiffValue = JSON.stringify(remoteItemValue); - if (remoteItemDiffValue !== JSON.stringify(existsItem[key])) { - if (!diffMeta) diffMeta = {}; + if (!isEqual(remoteItemValue, existsItem[key])) { diffMeta[key] = remoteItemValue; - } else if (!ignoreInAbbreviated.includes(key) && existsAbbreviatedItem && remoteItemDiffValue !== JSON.stringify(existsAbbreviatedItem[key])) { + } else if (!ignoreInAbbreviated.includes(key) && existsAbbreviatedItem && !isEqual(remoteItemValue, existsAbbreviatedItem[key])) { // should diff exists abbreviated item too - if (!diffMeta) diffMeta = {}; diffMeta[key] = remoteItemValue; } } // should delete readme if (shouldDeleteReadme) { - if (!diffMeta) diffMeta = {}; diffMeta.readme = undefined; } - if (diffMeta) { + if (!isEmpty(diffMeta)) { differentMetas.push([ existsItem, diffMeta ]); } continue;