Skip to content

Commit a2b5af0

Browse files
committed
feat(core): support multiple glob patterns
closes #5, #6 BREAKING CHANGE: --packages option replaced with variadic arguments
1 parent a6a61b5 commit a2b5af0

11 files changed

Lines changed: 115 additions & 66 deletions

File tree

src/bin-fix-mismatches.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import chalk from 'chalk';
44
import * as program from 'commander';
55
import * as _ from 'lodash';
66
import { relative } from 'path';
7-
import { DEFAULT_PATTERN, OPTION_PACKAGES } from './constants';
7+
import { FIX_MISMATCHES } from './constants';
88
import { setVersionsToNewestMismatch } from './manifests';
99

10-
program.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description).parse(process.argv);
10+
let packages: string[] = [];
1111

12-
const { packages = DEFAULT_PATTERN } = program;
12+
program
13+
.command(`${FIX_MISMATCHES.command} ${FIX_MISMATCHES.args}`)
14+
.action((...args) => {
15+
packages = args.filter((arg) => arg && typeof arg === 'string');
16+
})
17+
.parse(process.argv);
1318

14-
setVersionsToNewestMismatch(packages).then((descriptors) => {
19+
const patterns: string[] = packages.length ? packages : FIX_MISMATCHES.defaultPatterns;
20+
21+
setVersionsToNewestMismatch(...patterns).then((descriptors) => {
1522
_.each(descriptors, (descriptor) => {
1623
console.log(chalk.blue(`./${relative('.', descriptor.path)}`));
1724
});

src/bin-format.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import chalk from 'chalk';
44
import * as program from 'commander';
55
import * as _ from 'lodash';
66
import { relative } from 'path';
7-
import { DEFAULT_PATTERN, OPTION_PACKAGES } from './constants';
7+
import { FORMAT } from './constants';
88
import { format } from './manifests';
99

10-
program.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description).parse(process.argv);
10+
let packages: string[] = [];
1111

12-
const { packages = DEFAULT_PATTERN } = program;
12+
program
13+
.command(`${FORMAT.command} ${FORMAT.args}`)
14+
.action((...args) => {
15+
packages = args.filter((arg) => arg && typeof arg === 'string');
16+
})
17+
.parse(process.argv);
1318

14-
format(packages).then((descriptors) => {
19+
const patterns: string[] = packages.length ? packages : FORMAT.defaultPatterns;
20+
21+
format(...patterns).then((descriptors) => {
1522
_.each(descriptors, (descriptor) => {
1623
console.log(chalk.blue(`./${relative('.', descriptor.path)}`));
1724
});

src/bin-list-mismatches.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
import chalk from 'chalk';
44
import * as program from 'commander';
55
import * as _ from 'lodash';
6-
import { DEFAULT_PATTERN, OPTION_PACKAGES } from './constants';
6+
import { LIST_MISMATCHES } from './constants';
77
import { getMismatchedVersions } from './manifests';
88

9-
program.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description).parse(process.argv);
9+
let packages: string[] = [];
1010

11-
const { packages = DEFAULT_PATTERN } = program;
11+
program
12+
.command(`${LIST_MISMATCHES.command} ${LIST_MISMATCHES.args}`)
13+
.action((...args) => {
14+
packages = args.filter((arg) => arg && typeof arg === 'string');
15+
})
16+
.parse(process.argv);
1217

13-
getMismatchedVersions(packages).then((versionByName) => {
18+
const patterns: string[] = packages.length ? packages : LIST_MISMATCHES.defaultPatterns;
19+
20+
getMismatchedVersions(...patterns).then((versionByName) => {
1421
_.each(versionByName, (versions, name) => {
1522
console.log(chalk.yellow(name), chalk.dim(versions.join(', ')));
1623
});

src/bin-list.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
import chalk from 'chalk';
44
import * as program from 'commander';
55
import * as _ from 'lodash';
6-
import { DEFAULT_PATTERN, OPTION_PACKAGES } from './constants';
6+
import { LIST } from './constants';
77
import { getVersions } from './manifests';
88

9-
program.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description).parse(process.argv);
9+
let packages: string[] = [];
1010

11-
const { packages = DEFAULT_PATTERN } = program;
11+
program
12+
.command(`${LIST.command} ${LIST.args}`)
13+
.action((...args) => {
14+
packages = args.filter((arg) => arg && typeof arg === 'string');
15+
})
16+
.parse(process.argv);
1217

13-
getVersions(packages).then((versionByName) => {
18+
const patterns: string[] = packages.length ? packages : LIST.defaultPatterns;
19+
20+
getVersions(...patterns).then((versionByName) => {
1421
_.each(versionByName, (versions, name) => {
1522
if (versions.length > 1) {
1623
console.log(chalk.yellow(name), chalk.dim(versions.join(', ')));

src/bin-set-semver-ranges.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ import chalk from 'chalk';
44
import * as program from 'commander';
55
import * as _ from 'lodash';
66
import { relative } from 'path';
7-
import { DEFAULT_PATTERN, DEFAULT_SEMVER_RANGE, OPTION_PACKAGES, OPTION_SEMVER_RANGE } from './constants';
7+
import { OPTION_SEMVER_RANGE, SET_SEMVER_RANGES } from './constants';
88
import { setVersionRange } from './manifests';
99

10+
let packages: string[] = [];
11+
1012
program
11-
.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description)
13+
.command(`${SET_SEMVER_RANGES.command} ${SET_SEMVER_RANGES.args}`)
1214
.option(OPTION_SEMVER_RANGE.spec, OPTION_SEMVER_RANGE.description)
15+
.action((...args) => {
16+
packages = args.filter((arg) => arg && typeof arg === 'string');
17+
})
1318
.parse(process.argv);
1419

15-
const { packages = DEFAULT_PATTERN, semverRange = DEFAULT_SEMVER_RANGE } = program;
20+
const semverRange: string = program.semverRange || OPTION_SEMVER_RANGE.default;
21+
const patterns: string[] = packages.length ? packages : SET_SEMVER_RANGES.defaultPatterns;
1622

17-
setVersionRange(semverRange, packages).then((descriptors) => {
23+
setVersionRange(semverRange, ...patterns).then((descriptors) => {
1824
_.each(descriptors, (descriptor) => {
1925
console.log(chalk.blue(`./${relative('.', descriptor.path)}`));
2026
});

src/bin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { FIX_MISMATCHES, FORMAT, LIST, LIST_MISMATCHES, SET_SEMVER_RANGES, VERSI
55

66
program
77
.version(VERSION)
8-
.command(FIX_MISMATCHES.name, FIX_MISMATCHES.description)
9-
.command(FORMAT.name, FORMAT.description)
10-
.command(LIST.name, LIST.description, { isDefault: true })
11-
.command(LIST_MISMATCHES.name, LIST_MISMATCHES.description)
12-
.command(SET_SEMVER_RANGES.name, SET_SEMVER_RANGES.description);
8+
.command(FIX_MISMATCHES.command, FIX_MISMATCHES.description)
9+
.command(FORMAT.command, FORMAT.description)
10+
.command(LIST.command, LIST.description, { isDefault: true })
11+
.command(LIST_MISMATCHES.command, LIST_MISMATCHES.description)
12+
.command(SET_SEMVER_RANGES.command, SET_SEMVER_RANGES.description);
1313

1414
program.parse(process.argv);

src/constants.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,51 @@ export const SEMVER_ORDER = [
2828
RANGE_ANY
2929
];
3030

31+
const DEFAULT_SEMVER_RANGE = RANGE_EXACT;
32+
const MONOREPO_PATTERN = './package.json';
33+
const PACKAGES_PATTERN = './packages/*/package.json';
34+
const PACKAGES_PATTERNS = [PACKAGES_PATTERN];
35+
const ALL_PATTERNS = [MONOREPO_PATTERN, PACKAGES_PATTERN];
36+
3137
export const FIX_MISMATCHES = {
32-
description: 'set dependencies used with different versions to the same version',
33-
name: 'fix-mismatches'
38+
args: '[packages...]',
39+
command: 'fix-mismatches',
40+
defaultPatterns: PACKAGES_PATTERNS,
41+
description: 'set dependencies used with different versions to the same version'
3442
};
43+
3544
export const FORMAT = {
36-
description: 'sort and shorten properties according to a convention',
37-
name: 'format'
45+
args: '[packages...]',
46+
command: 'format',
47+
defaultPatterns: ALL_PATTERNS,
48+
description: 'sort and shorten properties according to a convention'
3849
};
50+
3951
export const LIST = {
40-
description: 'list every dependency used in your packages',
41-
name: 'list'
52+
args: '[packages...]',
53+
command: 'list',
54+
defaultPatterns: PACKAGES_PATTERNS,
55+
description: 'list every dependency used in your packages'
4256
};
57+
4358
export const LIST_MISMATCHES = {
44-
description: 'list every dependency used with different versions in your packages',
45-
name: 'list-mismatches'
59+
args: '[packages...]',
60+
command: 'list-mismatches',
61+
defaultPatterns: PACKAGES_PATTERNS,
62+
description: 'list every dependency used with different versions in your packages'
4663
};
64+
4765
export const SET_SEMVER_RANGES = {
48-
description: 'set semver ranges to the given format',
49-
name: 'set-semver-ranges'
66+
args: '[packages...]',
67+
command: 'set-semver-ranges',
68+
defaultPatterns: ALL_PATTERNS,
69+
description: 'set semver ranges to the given format'
5070
};
5171

52-
export const DEFAULT_PATTERN = './packages/*/package.json';
53-
export const DEFAULT_SEMVER_RANGE = RANGE_EXACT;
54-
export const OPTION_PACKAGES = {
55-
description: `location of packages. defaults to '${DEFAULT_PATTERN}'`,
56-
spec: '-p, --packages <glob>'
57-
};
5872
export const OPTION_SEMVER_RANGE = {
59-
description: `${[RANGE_ANY, RANGE_EXACT, RANGE_GT, RANGE_GTE, RANGE_LOOSE, RANGE_LT, RANGE_LTE, RANGE_MINOR]
60-
.map((value) => `'${value}'`)
61-
.join(', ')}, or '${RANGE_PATCH}'. defaults to '${DEFAULT_SEMVER_RANGE}'`,
73+
default: DEFAULT_SEMVER_RANGE,
74+
description:
75+
`${RANGE_LT}, ${RANGE_LTE}, "${RANGE_EXACT}", ${RANGE_PATCH}, ${RANGE_MINOR}, ` +
76+
`${RANGE_GTE}, ${RANGE_GT}, or ${RANGE_ANY}. defaults to "${DEFAULT_SEMVER_RANGE}"`,
6277
spec: '-r, --semver-range <range>'
6378
};

src/manifests/index.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import { IDictionary, IManifest, IManifestDescriptor } from '../typings';
33
import { getManifests } from './get-manifests';
44
import { manifestData } from './manifest-data';
55

6-
export type Format = (pattern: string) => Promise<IManifestDescriptor[]>;
7-
export type GetMismatchedVersions = (pattern: string) => Promise<IDictionary<string[]>>;
8-
export type GetVersions = (pattern: string) => Promise<IDictionary<string[]>>;
9-
export type SetVersion = (name: string, version: string, pattern: string) => Promise<IManifestDescriptor[]>;
10-
export type SetVersionRange = (range: string, pattern: string) => Promise<IManifestDescriptor[]>;
11-
export type SetVersionsToNewestMismatch = (pattern: string) => Promise<IManifestDescriptor[]>;
6+
export type Format = (...patterns: string[]) => Promise<IManifestDescriptor[]>;
7+
export type GetMismatchedVersions = (...patterns: string[]) => Promise<IDictionary<string[]>>;
8+
export type GetVersions = (...patterns: string[]) => Promise<IDictionary<string[]>>;
9+
export type SetVersion = (name: string, version: string, ...patterns: string[]) => Promise<IManifestDescriptor[]>;
10+
export type SetVersionRange = (range: string, ...patterns: string[]) => Promise<IManifestDescriptor[]>;
11+
export type SetVersionsToNewestMismatch = (...patterns: string[]) => Promise<IManifestDescriptor[]>;
1212

1313
const unwrap = (descriptors: IManifestDescriptor[]) => descriptors.map((descriptor) => descriptor.data);
1414

1515
const writeDescriptors = (descriptors: IManifestDescriptor[]): Promise<IManifestDescriptor[]> =>
1616
Promise.all(descriptors.map((descriptor) => writeJson(descriptor.path, descriptor.data))).then(() => descriptors);
1717

18-
export const format: Format = (pattern) =>
19-
getManifests(pattern)
18+
export const format: Format = (...patterns) =>
19+
getManifests(...patterns)
2020
.then((descriptors) => {
2121
const data = unwrap(descriptors);
2222
const nextData = manifestData.format(data);
@@ -27,32 +27,32 @@ export const format: Format = (pattern) =>
2727
})
2828
.then(writeDescriptors);
2929

30-
export const getMismatchedVersions: GetMismatchedVersions = (pattern) =>
31-
getManifests(pattern)
30+
export const getMismatchedVersions: GetMismatchedVersions = (...patterns) =>
31+
getManifests(...patterns)
3232
.then(unwrap)
3333
.then(manifestData.getMismatchedVersions);
3434

35-
export const getVersions: GetVersions = (pattern) =>
36-
getManifests(pattern)
35+
export const getVersions: GetVersions = (...patterns) =>
36+
getManifests(...patterns)
3737
.then(unwrap)
3838
.then(manifestData.getVersions);
3939

40-
export const setVersion: SetVersion = (name, version, pattern) =>
41-
getManifests(pattern).then((descriptors) => {
40+
export const setVersion: SetVersion = (name, version, ...patterns) =>
41+
getManifests(...patterns).then((descriptors) => {
4242
manifestData.setVersion(name, version, unwrap(descriptors));
4343
return descriptors;
4444
});
4545

46-
export const setVersionRange: SetVersionRange = (range, pattern) =>
47-
getManifests(pattern)
46+
export const setVersionRange: SetVersionRange = (range, ...patterns) =>
47+
getManifests(...patterns)
4848
.then((descriptors) => {
4949
manifestData.setVersionRange(range, unwrap(descriptors));
5050
return descriptors;
5151
})
5252
.then(writeDescriptors);
5353

54-
export const setVersionsToNewestMismatch: SetVersionsToNewestMismatch = (pattern) =>
55-
getManifests(pattern)
54+
export const setVersionsToNewestMismatch: SetVersionsToNewestMismatch = (...patterns) =>
55+
getManifests(...patterns)
5656
.then((descriptors) => {
5757
const data = unwrap(descriptors);
5858
const nextData = manifestData.setVersionsToNewestMismatch(data);

src/manifests/manifest-data/format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as _ from 'lodash';
22
import { SORT_AZ, SORT_FIRST } from '../../constants';
3-
import { IDictionary, IManifest } from '../../typings';
3+
import { IManifest } from '../../typings';
44

55
export type Format = (manifests: IManifest[]) => IManifest[];
66
export type ManifestMapper = (manifest: IManifest) => IManifest;

src/version.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export const getVersionRange: GetVersionRange = (version) => version.split(/[0-9
1212

1313
export const isValid = (version: string) => semver.valid(version) !== null;
1414

15-
export const sortBySemver: SortBySemver = (versions: string[]) => {
16-
return versions
15+
export const sortBySemver: SortBySemver = (versions: string[]) =>
16+
versions
1717
.concat()
1818
.sort()
1919
.sort((a: string, b: string) => {
@@ -52,6 +52,5 @@ export const sortBySemver: SortBySemver = (versions: string[]) => {
5252
}
5353
return SAME;
5454
});
55-
};
5655

5756
export const getNewest: GetNewest = (versions: string[]) => _(sortBySemver(versions)).last();

0 commit comments

Comments
 (0)