Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: init sync spec registry #433

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/core/service/PackageSyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class PackageSyncerService extends AbstractService {
// 1. 其次从 task.data.registryId (创建单包同步任务时传入)
// 2. 接着根据 scope 进行计算 (作为子包依赖同步时候,无 registryId)
// 3. 最后返回 default registryId (可能 default registry 也不存在)
public async initSpecRegistry(task: Task, pkg: Package | null = null): Promise<Registry | null> {
public async initSpecRegistry(task: Task, pkg: Package | null = null, scope?: string): Promise<Registry | null> {
const registryId = pkg?.registryId || (task.data as SyncPackageTaskOptions).registryId;
let targetHost: string = this.config.cnpmcore.sourceRegistry;
let registry: Registry | null = null;
Expand All @@ -317,8 +317,8 @@ export class PackageSyncerService extends AbstractService {
// 历史 Task 可能没有配置 registryId
if (registryId) {
registry = await this.registryManagerService.findByRegistryId(registryId);
} else if (pkg?.scope) {
const scopeModel = await this.scopeManagerService.findByName(pkg?.scope);
} else if (scope) {
const scopeModel = await this.scopeManagerService.findByName(scope);
if (scopeModel?.registryId) {
registry = await this.registryManagerService.findByRegistryId(scopeModel?.registryId);
}
Expand Down Expand Up @@ -349,7 +349,7 @@ export class PackageSyncerService extends AbstractService {
const [ scope, name ] = getScopeAndName(fullname);
const { tips, skipDependencies: originSkipDependencies, syncDownloadData, forceSyncHistory } = task.data as SyncPackageTaskOptions;
let pkg = await this.packageRepository.findPackage(scope, name);
const registry = await this.initSpecRegistry(task, pkg);
const registry = await this.initSpecRegistry(task, pkg, scope);
const registryHost = this.npmRegistry.registry;
let logs: string[] = [];
if (tips) {
Expand Down
25 changes: 25 additions & 0 deletions test/core/service/PackageSyncerService/executeTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,31 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => {
assert(pkg!.registryId === registry.registryId);
});

it('should sync from target registry when pkg not exists', async () => {
const pkgName = '@cnpm/banana';
await packageSyncerService.createTask(pkgName);
const task = await packageSyncerService.findExecuteTask();

// create custom scope
await scopeManagerService.createScope({
name: '@cnpm',
registryId: registry.registryId,
});

app.mockHttpclient('https://custom.npmjs.com/@cnpm/banana', 'GET', {
status: 500,
data: 'mock error',
persist: false,
repeats: 3,
});
await packageSyncerService.executeTask(task);
const stream = await packageSyncerService.findTaskLog(task);
assert(stream);
const log = await TestUtil.readStreamToLog(stream);
assert(log.includes('Syncing from https://custom.npmjs.com/@cnpm/banana'));
});


it('should not sync from target registry if not match', async () => {
const pkgName = '@cnpmcore/sync_not_match_registry_name';
await TestUtil.createPackage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('test/core/service/PackageSyncerService/getTaskRegistry.test.ts', () =>

describe('getTaskRegistry()', () => {
it('should work', async () => {
const taskRegistry = await packageSyncerService.initSpecRegistry(task);
const taskRegistry = await packageSyncerService.initSpecRegistry(task, null, '@cnpm');
assert(taskRegistry);
assert(taskRegistry.registryId === registry.registryId);
});
Expand All @@ -46,7 +46,7 @@ describe('test/core/service/PackageSyncerService/getTaskRegistry.test.ts', () =>
tips: `Sync cause by changes_stream(${registry.changeStream}) update seq: 1`,
});

const taskRegistry = await packageSyncerService.initSpecRegistry(task);
const taskRegistry = await packageSyncerService.initSpecRegistry(task, null, '@cnpm');
assert(taskRegistry === null);
});
});
Expand Down