Skip to content

Commit

Permalink
feat: implement custom.<customMgrName> syntax for matchManagers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Aug 27, 2023
1 parent b2c6cbe commit 9c322cd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/config/migrations/custom/match-managers-migration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MatchManagersMigration } from './match-managers-migration';

describe('config/migrations/custom/match-managers-migration', () => {
it('migrates old custom manager syntax to new one', () => {
expect(MatchManagersMigration).toMigrate(
{
matchManagers: ['npm', 'regex', 'custom.someMgr'],
},
{
matchManagers: ['npm', 'custom.regex', 'custom.someMgr'],
}
);
});

// coverage
it('only migrates when necessary', () => {
expect(MatchManagersMigration).not.toMigrate(
{
matchManagers: undefined,
},
{
matchManagers: undefined,
}
);
});
});
18 changes: 18 additions & 0 deletions lib/config/migrations/custom/match-managers-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import is from '@sindresorhus/is';
import { isCustomManager } from '../../../modules/manager/custom';
import { AbstractMigration } from '../base/abstract-migration';

export class MatchManagersMigration extends AbstractMigration {
override readonly propertyName = 'matchManagers';

override run(value: unknown): void {
if (!is.array<string>(value, is.string)) {
return;
}

const newValue = value.map((manager) =>
isCustomManager(manager) ? `custom.${manager}` : manager
);
this.rewrite(newValue);
}
}
2 changes: 1 addition & 1 deletion lib/config/validation-helpers/managers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { check } from './managers';
describe('config/validation-helpers/managers', () => {
it('should have no errors', () => {
const res = check({
resolvedRule: { matchManagers: ['npm', 'regex'] },
resolvedRule: { matchManagers: ['npm', 'regex', 'custom.regex'] },
currentPath: '',
});
expect(res).toEqual([]);
Expand Down
3 changes: 2 additions & 1 deletion lib/config/validation-helpers/managers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function check({
if (Array.isArray(resolvedRule.matchManagers)) {
if (
resolvedRule.matchManagers.find(
(confManager) => !allManagersList.includes(confManager)
(confManager) =>
!allManagersList.includes(confManager.replace('custom.', ''))
)
) {
managersErrMessage = `${currentPath}:
Expand Down
12 changes: 12 additions & 0 deletions lib/util/package-rules/managers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,17 @@ describe('util/package-rules/managers', () => {
);
expect(result).toBeFalse();
});

it('should match custom managers', () => {
const result = managersMatcher.matches(
{
manager: 'regex',
},
{
matchManagers: ['custom.regex'],
}
);
expect(result).toBeTrue();
});
});
});
4 changes: 4 additions & 0 deletions lib/util/package-rules/managers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { isCustomManager } from '../../modules/manager/custom';
import { Matcher } from './base';

export class ManagersMatcher extends Matcher {
Expand All @@ -13,6 +14,9 @@ export class ManagersMatcher extends Matcher {
if (is.undefined(manager) || !manager) {
return false;
}
if (isCustomManager(manager)) {
return matchManagers.includes(`custom.${manager}`);
}
return matchManagers.includes(manager);
}
}

0 comments on commit 9c322cd

Please sign in to comment.