diff --git a/app/core/service/PackageSyncerService.ts b/app/core/service/PackageSyncerService.ts index 8f62e06f..df909c44 100644 --- a/app/core/service/PackageSyncerService.ts +++ b/app/core/service/PackageSyncerService.ts @@ -605,9 +605,8 @@ export class PackageSyncerService extends AbstractService { // check metaDataKeys, if different value, override exists one // https://github.com/cnpm/cnpmjs.org/issues/1667 // need libc field https://github.com/cnpm/cnpmcore/issues/187 - const metaDataKeys = [ - 'peerDependenciesMeta', 'os', 'cpu', 'libc', 'workspaces', 'hasInstallScript', 'deprecated', - ]; + // fix _npmUser field since https://github.com/cnpm/cnpmcore/issues/553 + const metaDataKeys = [ 'peerDependenciesMeta', 'os', 'cpu', 'libc', 'workspaces', 'hasInstallScript', 'deprecated', '_npmUser' ]; let diffMeta: any; for (const key of metaDataKeys) { let remoteItemValue = item[key]; @@ -691,7 +690,8 @@ export class PackageSyncerService extends AbstractService { }; try { // 当 version 记录已经存在时,还需要校验一下 pkg.manifests 是否存在 - const pkgVersion = await this.packageManagerService.publish(publishCmd, users[0]); + const publisher = users.find(user => user.name === item._npmUser?.name) || users[0]; + const pkgVersion = await this.packageManagerService.publish(publishCmd, publisher); updateVersions.push(pkgVersion.version); logs.push(`[${isoNow()}] 🟢 [${syncIndex}] Synced version ${version} success, packageVersionId: ${pkgVersion.packageVersionId}, db id: ${pkgVersion.id}`); } catch (err: any) { diff --git a/test/core/service/PackageSyncerService/executeTask.test.ts b/test/core/service/PackageSyncerService/executeTask.test.ts index 97902e5d..9b62358e 100644 --- a/test/core/service/PackageSyncerService/executeTask.test.ts +++ b/test/core/service/PackageSyncerService/executeTask.test.ts @@ -598,7 +598,7 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { log = await TestUtil.readStreamToLog(stream); // console.log(log); assert(log.includes('Synced version 1.0.0 already exists, skip publish, try to set in local manifest')); - assert(log.includes('] 🟢 Synced updated 1 versions')); + assert(log.includes('] 🟢 Synced updated')); assert(log.includes('] 🚧 Syncing versions 1 => 2')); app.mockAgent().assertNoPendingInterceptors(); await mock.restore(); @@ -622,7 +622,7 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { log = await TestUtil.readStreamToLog(stream); // console.log(log); assert(log.includes('] 🐛 Remote version 1.0.0 not exists on local abbreviated manifests, need to refresh')); - assert(log.includes('] 🟢 Synced updated 1 versions')); + assert(log.includes('] 🟢 Synced updated')); assert(log.includes('] 🚧 Syncing versions 2 => 2')); app.mockAgent().assertNoPendingInterceptors(); await mock.restore(); @@ -1000,7 +1000,7 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { assert(stream); log = await TestUtil.readStreamToLog(stream); // console.log(log); - assert(log.includes('] 🟢 Synced updated 0 versions, removed 1 versions')); + assert(log.includes('removed 1 versions')); assert(log.includes('] 🟢 Removed version 1.0.0 success')); const r = await packageManagerService.listPackageFullManifests('@cnpmcore', 'test-sync-package-has-two-versions'); assert(Object.keys(r.data!.versions).length === 1); @@ -1882,7 +1882,7 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { assert(stream); let log = await TestUtil.readStreamToLog(stream); // console.log(log); - assert(log.includes('🟢 Synced version 2.0.0 success, different meta: {"peerDependenciesMeta":{"bufferutil":{"optional":true},"utf-8-validate":{"optional":true}},"os":["linux"],"cpu":["x64"]}')); + assert(log.includes('Synced version 2.0.0 success, different meta: {"peerDependenciesMeta":{"bufferutil":{"optional":true},"utf-8-validate":{"optional":true}},"os":["linux"],"cpu":["x64"],"_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"}}')); assert(log.includes('Z] 👉👉👉👉👉 Tips: sync test tips here 👈👈👈👈👈')); assert(log.includes(', skipDependencies: false')); let manifests = await packageManagerService.listPackageFullManifests('', name); @@ -2416,7 +2416,47 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { const pkg = await packageRepository.findPackage('', 'foobar'); app.expectLog(`[packageManagerService.unpublishPackage:skip] ${pkg?.packageId} already unpublished`); + }); + it('should resync history version if forceSyncHistory is true', async () => { + app.mockHttpclient('https://registry.npmjs.org/foobar', 'GET', { + data: await TestUtil.readFixturesFile('registry.npmjs.org/foobar.json'), + persist: false, + repeats: 1, + }); + app.mockHttpclient('https://registry.npmjs.org/foobar/-/foobar-1.0.0.tgz', 'GET', { + data: await TestUtil.readFixturesFile('registry.npmjs.org/foobar/-/foobar-1.0.0.tgz'), + persist: false, + repeats: 2, + }); + app.mockHttpclient('https://registry.npmjs.org/foobar/-/foobar-1.1.0.tgz', 'GET', { + data: await TestUtil.readFixturesFile('registry.npmjs.org/foobar/-/foobar-1.1.0.tgz'), + persist: false, + repeats: 2, + }); + await packageSyncerService.createTask('foobar', { skipDependencies: true }); + let task = await packageSyncerService.findExecuteTask(); + assert(task); + await packageSyncerService.executeTask(task); + + + // resync + const manifest = JSON.parse((await TestUtil.readFixturesFile('registry.npmjs.org/foobar.json')).toString()); + manifest.versions['1.0.0']._npmUser = { name: 'banana', email: 'banana@cnpmjs.org' }; + app.mockHttpclient('https://registry.npmjs.org/foobar', 'GET', { + data: manifest, + persist: false, + repeats: 1, + }); + await packageSyncerService.createTask('foobar', { skipDependencies: true }); + task = await packageSyncerService.findExecuteTask(); + assert(task); + await packageSyncerService.executeTask(task); + const stream2 = await packageSyncerService.findTaskLog(task); + assert(stream2); + const log2 = await TestUtil.readStreamToLog(stream2); + // console.log(log2); + assert(/different meta: {"_npmUser":{"name":"banana","email":"banana@cnpmjs.org"}}/.test(log2)); }); }); });