Skip to content

Commit

Permalink
chore(core): add config for granular versioning rules
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Aug 29, 2020
1 parent 821a8db commit 9a9a530
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/commands/fix-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getMismatchedDependencies } from './lib/get-installations';
import { getWrappers, SourceWrapper } from './lib/get-wrappers';
import { log } from './lib/log';

type Options = Pick<SyncpackConfig, 'dev' | 'filter' | 'indent' | 'peer' | 'prod' | 'source'>;
type Options = Pick<SyncpackConfig, 'dev' | 'filter' | 'indent' | 'peer' | 'prod' | 'source' | 'versionGroups'>;

export const fixMismatches = (wrappers: SourceWrapper[], options: Options): void => {
const iterator = getMismatchedDependencies(wrappers, options);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/list-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getMismatchedDependencies, InstalledPackage, sortByName } from './lib/g
import { getWrappers, SourceWrapper } from './lib/get-wrappers';
import { log } from './lib/log';

type Options = Pick<SyncpackConfig, 'dev' | 'filter' | 'peer' | 'prod' | 'source'>;
type Options = Pick<SyncpackConfig, 'dev' | 'filter' | 'peer' | 'prod' | 'source' | 'versionGroups'>;

export const listMismatches = (wrappers: SourceWrapper[], options: Options): InstalledPackage[] => {
const iterator = getMismatchedDependencies(wrappers, options);
Expand Down
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const SEMVER_ORDER: ValidRange[] = [
RANGE_ANY,
];

export interface VersionGroup {
packages: string[];
dependencies: string[];
}

export type SyncpackConfig = Readonly<{
dev: boolean;
filter: RegExp;
Expand All @@ -39,6 +44,7 @@ export type SyncpackConfig = Readonly<{
sortAz: string[];
sortFirst: string[];
source: string[];
versionGroups: VersionGroup[];
}>;

export const DEFAULT_CONFIG: SyncpackConfig = {
Expand All @@ -51,6 +57,7 @@ export const DEFAULT_CONFIG: SyncpackConfig = {
sortAz: ['contributors', 'dependencies', 'devDependencies', 'keywords', 'peerDependencies', 'resolutions', 'scripts'],
sortFirst: ['name', 'description', 'version', 'author'],
source: [],
versionGroups: [],
};

const MONOREPO_PATTERN = 'package.json';
Expand Down
1 change: 1 addition & 0 deletions src/lib/get-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('getConfig', () => {
sortAz: ['overridden'],
sortFirst: ['name', 'description', 'version', 'author'],
source: ['./from-config'],
versionGroups: [],
});
});

Expand Down
10 changes: 8 additions & 2 deletions src/lib/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cosmiconfigSync } from 'cosmiconfig';
import { isArray, isArrayOfStrings, isBoolean, isNonEmptyString, isObject, isRegExp } from 'expect-more';
import { isValidSemverRange } from '../commands/lib/is-semver';
import { DEFAULT_CONFIG, SyncpackConfig } from '../constants';
import { DEFAULT_CONFIG, SyncpackConfig, VersionGroup } from '../constants';

export interface CliOptions {
dev: boolean;
Expand All @@ -14,7 +14,7 @@ export interface CliOptions {
}

export const getConfig = (program: Partial<CliOptions>): SyncpackConfig => {
type OptionName = keyof CliOptions | 'sortAz' | 'sortFirst';
type OptionName = keyof CliOptions | 'sortAz' | 'sortFirst' | 'versionGroups';
type TypeChecker = (value: any) => boolean;

const rcSearch = cosmiconfigSync('syncpack').search();
Expand All @@ -23,6 +23,11 @@ export const getConfig = (program: Partial<CliOptions>): SyncpackConfig => {

const isCliOption = (key: string): key is keyof CliOptions => key in program;

const isVersionGroup = (value: any): value is VersionGroup =>
isObject(value) && isArrayOfStrings(value.packages) && isArrayOfStrings(value.dependencies);

const isArrayOfVersionGroups = (value: any): value is VersionGroup[] => isArray(value) && value.every(isVersionGroup);

const getOption = (name: OptionName, isValid: TypeChecker) => {
if (isCliOption(name) && isValid(program[name])) {
return program[name];
Expand All @@ -45,6 +50,7 @@ export const getConfig = (program: Partial<CliOptions>): SyncpackConfig => {
sortAz: getOption('sortAz', isArrayOfStrings),
sortFirst: getOption('sortFirst', isArrayOfStrings),
source: getOption('source', isArray),
versionGroups: getOption('versionGroups', isArrayOfVersionGroups),
};

if (program.prod || program.dev || program.peer) {
Expand Down

0 comments on commit 9a9a530

Please sign in to comment.