Skip to content

Commit

Permalink
fix(config): prevent default source overriding rcfile
Browse files Browse the repository at this point in the history
Closes #123

Co-Authored-By: bpolge-kr <benjamin.polge@kroger.com>
  • Loading branch information
JamieMason and bpolge-kr committed Feb 27, 2023
1 parent 2f1ffba commit 1d6a4ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/get-context/get-config/get-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('semverRange', () => {
});

describe('source', () => {
it('uses default when not set', () => {
it('defaults to empty array when not set', () => {
const disk = mockDisk();
const config = getConfig(disk, {});
expect(R.getExn(config).source).toEqual([]);
Expand All @@ -155,7 +155,7 @@ describe('source', () => {
it('uses config value when set', () => {
const disk = mockDisk();
disk.readConfigFileSync.mockReturnValue({ source: ['projects/*'] });
const config = getConfig(disk, {});
const config = getConfig(disk, { source: [] });
expect(R.getExn(config).source).toEqual(['projects/*']);
});

Expand Down
34 changes: 22 additions & 12 deletions src/get-context/get-config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { pipe, R } from '@mobily/ts-belt';
import {
isArrayOfObjects,
isArrayOfStrings,
isNonEmptyObject,
isNonEmptyString,
isString,
} from 'expect-more';
import type { Disk } from '../../lib/disk';
import { BaseError } from '../../lib/error';
import { verbose } from '../../lib/log';
Expand Down Expand Up @@ -34,17 +41,17 @@ function unSafeGetConfig(
verbose('rcfile contents:', fromRcFile);

const fromPublic = ConfigSchema.Public.parse({
customTypes: getConfigByName('customTypes'),
customTypes: getConfigByName('customTypes', isNonEmptyObject),
dependencyTypes: fromRcFile?.dependencyTypes,
filter: getConfigByName('filter'),
indent: getConfigByName('indent'),
semverGroups: getConfigByName('semverGroups'),
semverRange: getConfigByName('semverRange'),
sortAz: getConfigByName('sortAz'),
sortFirst: getConfigByName('sortFirst'),
source: getConfigByName('source'),
filter: getConfigByName('filter', isNonEmptyString),
indent: getConfigByName('indent', isString),
semverGroups: getConfigByName('semverGroups', isArrayOfObjects),
semverRange: getConfigByName('semverRange', isString),
sortAz: getConfigByName('sortAz', isArrayOfStrings),
sortFirst: getConfigByName('sortFirst', isArrayOfStrings),
source: getConfigByName('source', isArrayOfStrings),
types: fromCli?.types,
versionGroups: getConfigByName('versionGroups'),
versionGroups: getConfigByName('versionGroups', isArrayOfObjects),
});

verbose('user config:', fromPublic);
Expand Down Expand Up @@ -78,10 +85,13 @@ function unSafeGetConfig(

return allConfig;

function getConfigByName(name: keyof Syncpack.Config.Public): unknown {
if (typeof (fromCli as any)[name] !== 'undefined')
function getConfigByName(
name: keyof Syncpack.Config.Public,
isValid: (value: unknown) => boolean,
): unknown {
if (isValid((fromCli as any)[name]))
return (fromCli as Syncpack.Config.Public)[name];
if (typeof (fromRcFile as any)[name] !== 'undefined')
if (isValid((fromRcFile as any)[name]))
return (fromRcFile as Syncpack.Config.Public)[name];
}
}

0 comments on commit 1d6a4ba

Please sign in to comment.