Skip to content

Commit

Permalink
fix: auto fix invalid version to any version (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jun 7, 2023
1 parent 7fbf79f commit 0cf6470
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
13 changes: 10 additions & 3 deletions app/core/entity/PackageVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface PackageVersionData extends EntityData {
tarDist: Dist;
readmeDist: Dist;
publishTime: Date;
paddingVersion?: string | null;
isPreRelease?: boolean | null;
}

export class PackageVersion extends Entity {
Expand All @@ -36,9 +38,14 @@ export class PackageVersion extends Entity {
this.tarDist = data.tarDist;
this.readmeDist = data.readmeDist;
this.publishTime = data.publishTime;
const paddingSemVer = new PaddingSemVer(this.version);
this.paddingVersion = paddingSemVer.paddingVersion;
this.isPreRelease = paddingSemVer.isPreRelease;
if (data.paddingVersion && typeof data.isPreRelease === 'boolean') {
this.paddingVersion = data.paddingVersion;
this.isPreRelease = data.isPreRelease;
} else {
const paddingSemVer = new PaddingSemVer(this.version);
this.paddingVersion = paddingSemVer.paddingVersion;
this.isPreRelease = paddingSemVer.isPreRelease;
}
}

static create(data: EasyData<PackageVersionData, 'packageVersionId'>): PackageVersion {
Expand Down
8 changes: 7 additions & 1 deletion app/core/entity/PaddingSemVer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SemVer } from 'semver';
import { SemVer, valid } from 'semver';

export class PaddingSemVer {
private readonly semver: SemVer;
Expand All @@ -7,6 +7,12 @@ export class PaddingSemVer {
readonly isPreRelease: boolean;

constructor(semver: string | SemVer) {
// ignore invalid version, e.g.: '1000000000000000000.0.0' on https://registry.npmjs.com/latentflip-test
if (!valid(semver)) {
this.isPreRelease = true;
this._paddingVersion = PaddingSemVer.anyVersion();
return;
}
this.semver = new SemVer(semver);
if ((this.semver as any).includePrerelease) {
this.isPreRelease = true;
Expand Down
12 changes: 2 additions & 10 deletions app/core/service/FixNoPaddingVersionService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
import { EggLogger } from 'egg';
import pMap from 'p-map';
import semver from 'semver';
import { PackageVersionRepository } from '../../repository/PackageVersionRepository';
import { PaddingSemVer } from '../entity/PaddingSemVer';

Expand All @@ -26,15 +25,8 @@ export class FixNoPaddingVersionService {
this.logger.info('[FixNoPaddingVersionService] fix padding version ids %j', packageVersions.map(t => t.id));

await pMap(packageVersions, async packageVersion => {
// ignore invalid version, e.g.: '1000000000000000000.0.0' on https://registry.npmjs.com/latentflip-test
if (!semver.valid(packageVersion.version)) return;
try {
const paddingSemver = new PaddingSemVer(packageVersion.version);
await this.packageVersionRepository.fixPaddingVersion(packageVersion.packageVersionId, paddingSemver);
} catch (err) {
this.logger.error('[FixNoPaddingVersionService:error] package_version_id: %s, version: %j, error: %s', packageVersion.packageVersionId, packageVersion.version, err);
throw err;
}
const paddingSemver = new PaddingSemVer(packageVersion.version);
await this.packageVersionRepository.fixPaddingVersion(packageVersion.packageVersionId, paddingSemver);
}, { concurrency: 30 });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ describe('test/core/service/BugVersionService/fixPackageBugVersion.test.ts', ()
tarDist: dist,
readmeDist: dist,
publishTime: new Date(),
paddingVersion: null,
isPreRelease: null,
});
});
});
Expand Down

0 comments on commit 0cf6470

Please sign in to comment.