Skip to content

Commit 4793f1f

Browse files
committed
feat(core): add fix-mismatches command
1 parent 0e8eca9 commit 4793f1f

File tree

7 files changed

+79
-19
lines changed

7 files changed

+79
-19
lines changed

src/bin-fix-mismatches.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env node
2+
3+
import chalk from 'chalk';
4+
import * as program from 'commander';
5+
import * as _ from 'lodash';
6+
import { relative } from 'path';
7+
import { DEFAULT_PATTERN, OPTION_PACKAGES } from './constants';
8+
import { setVersionsToNewestMismatch } from './manifests';
9+
10+
program.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description).parse(process.argv);
11+
12+
const { packages = DEFAULT_PATTERN } = program;
13+
14+
setVersionsToNewestMismatch(packages).then((descriptors) => {
15+
_.each(descriptors, (descriptor) => {
16+
console.log(chalk.blue(`./${relative('.', descriptor.path)}`));
17+
});
18+
});

src/bin.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env node
22

33
import * as program from 'commander';
4-
import { COMMAND_LIST, COMMAND_LIST_MISMATCHES } from './constants';
4+
import { FIX_MISMATCHES, LIST, LIST_MISMATCHES } from './constants';
55

66
program
77
.version('TODO')
8-
.command(COMMAND_LIST.name, COMMAND_LIST.description, {
9-
isDefault: true
10-
})
11-
.command(COMMAND_LIST_MISMATCHES.name, COMMAND_LIST_MISMATCHES.description);
8+
.command(FIX_MISMATCHES.name, FIX_MISMATCHES.description)
9+
.command(LIST.name, LIST.description, { isDefault: true })
10+
.command(LIST_MISMATCHES.name, LIST_MISMATCHES.description);
1211

1312
program.parse(process.argv);

src/constants.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { IManifestKey } from './typings';
22

3-
export const COMMAND_LIST = {
3+
export const FIX_MISMATCHES = {
4+
description: 'set dependencies used with different versions to the same version',
5+
name: 'fix-mismatches'
6+
};
7+
export const LIST = {
48
description: 'list every dependency used in your packages',
59
name: 'list'
610
};
7-
export const COMMAND_LIST_MISMATCHES = {
11+
export const LIST_MISMATCHES = {
812
description: 'list every dependency used with different versions in your packages',
913
name: 'list-mismatches'
1014
};

src/lib/write-json.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { writeFile } from 'fs';
22

3-
export const writeJson = (location: string, contents: string): Promise<void> =>
3+
export const formatJson = (contents: object) => `${JSON.stringify(contents, null, 2)}\n`;
4+
5+
export const writeJson = (location: string, contents: object): Promise<void> =>
46
new Promise((resolve, reject) => {
5-
const json = `${JSON.stringify(contents, null, 2)}\n`;
6-
writeFile(location, json, { encoding: 'utf8' }, (err) => {
7+
writeFile(location, formatJson(contents), { encoding: 'utf8' }, (err) => {
78
err ? reject(err) : resolve();
89
});
910
});

src/manifests/index.spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { readFileSync } from 'fs';
12
import * as mock from 'mock-fs';
2-
import { createManifest, createMockDescriptor, createMockFs } from '../../test/helpers';
3-
import { IManifest } from '../typings';
3+
import { createFile, createManifest, createMockDescriptor, createMockFs } from '../../test/helpers';
4+
import { IManifest, IManifestDescriptor } from '../typings';
45
import { getMismatchedVersions, getVersions, setVersion, setVersionRange, setVersionsToNewestMismatch } from './index';
56

67
const pattern = '/Users/you/Dev/monorepo/packages/*/package.json';
@@ -85,7 +86,7 @@ describe('setVersionRange', () => {
8586
});
8687

8788
describe('setVersionsToNewestMismatch', () => {
88-
it('sets the version of dependencies with different versions to the newest of those versions found', async () => {
89+
it('sets all dependencies used with different versions to the newest of those versions', async () => {
8990
const result = await setVersionsToNewestMismatch(pattern);
9091
expect(result).toEqual(
9192
expect.arrayContaining([
@@ -100,4 +101,21 @@ describe('setVersionsToNewestMismatch', () => {
100101
])
101102
);
102103
});
104+
it('rewrites the updated manifests with the correct data', async () => {
105+
await setVersionsToNewestMismatch(pattern);
106+
expect(readFileSync('/Users/you/Dev/monorepo/packages/foo/package.json', 'utf8')).toEqual(
107+
createFile(
108+
'foo',
109+
{ chalk: '2.3.0', commander: '2.13.0' },
110+
{ jest: '22.1.4', prettier: '1.10.2', rimraf: '2.6.2' },
111+
{ gulp: '*' }
112+
)
113+
);
114+
expect(readFileSync('/Users/you/Dev/monorepo/packages/bar/package.json', 'utf8')).toEqual(
115+
createFile('bar', { chalk: '2.3.0' }, { jest: '22.1.4' })
116+
);
117+
expect(readFileSync('/Users/you/Dev/monorepo/packages/baz/package.json', 'utf8')).toEqual(
118+
createFile('baz', {}, { npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' }, { gulp: '*' })
119+
);
120+
});
103121
});

src/manifests/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DEFAULT_PATTERN } from '../constants';
2+
import { writeJson } from '../lib/write-json';
23
import { IDictionary, IManifest, IManifestDescriptor } from '../typings';
34
import { getManifests } from './get-manifests';
45
import { manifestData } from './manifest-data';
@@ -34,7 +35,15 @@ export const setVersionRange: SetVersionRange = (range, pattern = DEFAULT_PATTER
3435
});
3536

3637
export const setVersionsToNewestMismatch: SetVersionsToNewestMismatch = (pattern = DEFAULT_PATTERN) =>
37-
getManifests(pattern).then((descriptors) => {
38-
manifestData.setVersionsToNewestMismatch(unwrap(descriptors));
39-
return descriptors;
40-
});
38+
getManifests(pattern)
39+
.then((descriptors) => {
40+
const data = unwrap(descriptors);
41+
const nextData = manifestData.setVersionsToNewestMismatch(data);
42+
return descriptors.map((descriptor, i) => ({
43+
data: nextData[i],
44+
path: descriptor.path
45+
}));
46+
})
47+
.then((descriptors) =>
48+
Promise.all(descriptors.map((descriptor) => writeJson(descriptor.path, descriptor.data))).then(() => descriptors)
49+
);

test/helpers.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import { formatJson } from '../src/lib/write-json';
12
import { IDictionary, IManifest, IManifestDescriptor } from '../src/typings';
23

4+
export const createFile = (
5+
name: string,
6+
dependencies: IManifest['dependencies'] = {},
7+
devDependencies: IManifest['devDependencies'] = {},
8+
peerDependencies: IManifest['peerDependencies'] = {}
9+
) => formatJson(createManifest(name, dependencies, devDependencies, peerDependencies));
10+
311
export const createMockFs = (
412
name: string,
513
dependencies: IManifest['dependencies'] = {},
614
devDependencies: IManifest['devDependencies'] = {},
715
peerDependencies: IManifest['peerDependencies'] = {}
816
): { [path: string]: string } => ({
9-
[`/Users/you/Dev/monorepo/packages/${name}/package.json`]: JSON.stringify(
10-
createManifest(name, dependencies, devDependencies, peerDependencies)
17+
[`/Users/you/Dev/monorepo/packages/${name}/package.json`]: createFile(
18+
name,
19+
dependencies,
20+
devDependencies,
21+
peerDependencies
1122
)
1223
});
1324

0 commit comments

Comments
 (0)