Skip to content

Commit

Permalink
style(core): set line length to 80
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Oct 28, 2018
1 parent 22b5a2a commit 75e3de9
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"arrowParens": "always",
"printWidth": 120,
"printWidth": 80,
"singleQuote": true
}
9 changes: 7 additions & 2 deletions src/bin-fix-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import { setVersionsToNewestMismatch } from './manifests';

const collect = (value: string, values: string[] = []) => values.concat(value);

program.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect).parse(process.argv);
program
.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect)
.parse(process.argv);

const sources: string[] = program.source && program.source.length ? program.source : OPTION_SOURCES.default;
const sources: string[] =
program.source && program.source.length
? program.source
: OPTION_SOURCES.default;

setVersionsToNewestMismatch(...sources).then((descriptors) => {
_.each(descriptors, (descriptor) => {
Expand Down
9 changes: 7 additions & 2 deletions src/bin-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import { format } from './manifests';

const collect = (value: string, values: string[] = []) => values.concat(value);

program.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect).parse(process.argv);
program
.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect)
.parse(process.argv);

const sources: string[] = program.source && program.source.length ? program.source : OPTION_SOURCES.default;
const sources: string[] =
program.source && program.source.length
? program.source
: OPTION_SOURCES.default;

format(...sources).then((descriptors) => {
_.each(descriptors, (descriptor) => {
Expand Down
9 changes: 7 additions & 2 deletions src/bin-list-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import { getMismatchedVersions } from './manifests';

const collect = (value: string, values: string[] = []) => values.concat(value);

program.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect).parse(process.argv);
program
.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect)
.parse(process.argv);

const sources: string[] = program.source && program.source.length ? program.source : OPTION_SOURCES.default;
const sources: string[] =
program.source && program.source.length
? program.source
: OPTION_SOURCES.default;

getMismatchedVersions(...sources).then((versionByName) => {
_.each(versionByName, (versions, name) => {
Expand Down
9 changes: 7 additions & 2 deletions src/bin-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import { getVersions } from './manifests';

const collect = (value: string, values: string[] = []) => values.concat(value);

program.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect).parse(process.argv);
program
.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect)
.parse(process.argv);

const sources: string[] = program.source && program.source.length ? program.source : OPTION_SOURCES.default;
const sources: string[] =
program.source && program.source.length
? program.source
: OPTION_SOURCES.default;

getVersions(...sources).then((versionByName) => {
_.each(versionByName, (versions, name) => {
Expand Down
5 changes: 4 additions & 1 deletion src/bin-set-semver-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ program
.parse(process.argv);

const semverRange: string = program.semverRange || OPTION_SEMVER_RANGE.default;
const sources: string[] = program.source && program.source.length ? program.source : OPTION_SOURCES.default;
const sources: string[] =
program.source && program.source.length
? program.source
: OPTION_SOURCES.default;

setVersionRange(semverRange, ...sources).then((descriptors) => {
_.each(descriptors, (descriptor) => {
Expand Down
9 changes: 8 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/usr/bin/env node

import program = require('commander');
import { FIX_MISMATCHES, FORMAT, LIST, LIST_MISMATCHES, SET_SEMVER_RANGES, VERSION } from './constants';
import {
FIX_MISMATCHES,
FORMAT,
LIST,
LIST_MISMATCHES,
SET_SEMVER_RANGES,
VERSION
} from './constants';

program
.version(VERSION)
Expand Down
12 changes: 9 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { IManifestKey } from './typings';

export const DEPENDENCY_TYPES: IManifestKey[] = ['dependencies', 'devDependencies', 'peerDependencies'];
export const DEPENDENCY_TYPES: IManifestKey[] = [
'dependencies',
'devDependencies',
'peerDependencies'
];
export const SORT_AZ = [
'contributors',
'dependencies',
Expand Down Expand Up @@ -43,7 +47,8 @@ const ALL_PATTERNS = [MONOREPO_PATTERN, PACKAGES_PATTERN];

export const FIX_MISMATCHES = {
command: 'fix-mismatches',
description: 'set dependencies used with different versions to the same version'
description:
'set dependencies used with different versions to the same version'
};

export const FORMAT = {
Expand All @@ -58,7 +63,8 @@ export const LIST = {

export const LIST_MISMATCHES = {
command: 'list-mismatches',
description: 'list every dependency used with different versions in your packages'
description:
'list every dependency used with different versions in your packages'
};

export const SET_SEMVER_RANGES = {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/write-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ afterEach(() => {
});

it('rejects with error thrown upstream', async () => {
jest.doMock('fs', () => ({ writeFile: jest.fn((path, data, options, cb) => cb(new Error('wat?'))) }));
jest.doMock('fs', () => ({
writeFile: jest.fn((path, data, options, cb) => cb(new Error('wat?')))
}));
const { writeJson } = require('./write-json');
try {
await writeJson('/Usr/you/some.json', { some: 'data' });
Expand All @@ -13,7 +15,9 @@ it('rejects with error thrown upstream', async () => {
});

it('resolves when file was written successfully', async () => {
jest.doMock('fs', () => ({ writeFile: jest.fn((path, data, options, cb) => cb(null)) }));
jest.doMock('fs', () => ({
writeFile: jest.fn((path, data, options, cb) => cb(null))
}));
const { writeJson } = require('./write-json');
const result = await writeJson('/Usr/you/some.json', { some: 'data' });
expect(result).toBeUndefined();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/write-json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writeFile } from 'fs';

export const formatJson = (contents: object) => `${JSON.stringify(contents, null, 2)}\n`;
export const formatJson = (contents: object) =>
`${JSON.stringify(contents, null, 2)}\n`;

export const writeJson = (location: string, contents: object): Promise<void> =>
new Promise((resolve, reject) => {
Expand Down
14 changes: 10 additions & 4 deletions src/manifests/get-manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import { manifestData } from './manifest-data';

type GetDescriptor = (path: string) => Promise<IFileDescriptor>;
type GetDescriptors = (paths: string[]) => Promise<IFileDescriptor[]>;
type FilterDescriptors = (descriptors: IFileDescriptor[]) => IManifestDescriptor[];
type FilterDescriptors = (
descriptors: IFileDescriptor[]
) => IManifestDescriptor[];

const { isManifest } = manifestData;

const getDescriptor: GetDescriptor = (path) => readJson(path).then((data) => ({ data, path }));
const getDescriptors: GetDescriptors = (paths) => Promise.all(paths.map(getDescriptor));
const getDescriptor: GetDescriptor = (path) =>
readJson(path).then((data) => ({ data, path }));
const getDescriptors: GetDescriptors = (paths) =>
Promise.all(paths.map(getDescriptor));
const filterDescriptors: FilterDescriptors = (descriptors) =>
descriptors
.filter((descriptor) => isManifest(descriptor.data))
.map((descriptor) => descriptor as IManifestDescriptor);

export const getManifests = (...patterns: string[]): Promise<IManifestDescriptor[]> =>
export const getManifests = (
...patterns: string[]
): Promise<IManifestDescriptor[]> =>
globby(patterns, { absolute: true })
.then(getDescriptors)
.then(filterDescriptors);
78 changes: 63 additions & 15 deletions src/manifests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { readFileSync } from 'fs';
import _ = require('lodash');
import mock = require('mock-fs');
import { createFile, createManifest, createMockDescriptor, createMockFs } from '../../test/helpers';
import {
createFile,
createManifest,
createMockDescriptor,
createMockFs
} from '../../test/helpers';
import { IManifest, IManifestDescriptor } from '../typings';
import {
format,
Expand All @@ -27,17 +32,36 @@ beforeEach(() => {
{ gulp: '3.9.1' }
),
...createMockFs('bar', { chalk: '1.0.0' }, { jest: '22.1.4' }),
...createMockFs('baz', null, { npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' }, { gulp: '*' })
...createMockFs(
'baz',
null,
{ npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' },
{ gulp: '*' }
)
});
});

describe('format', () => {
it('sorts and shortens properties according to a convention', async () => {
const result = await format(pattern);
const getByName = (name) => Object.keys(_.find(result, (v) => v.data.name === name).data);
expect(getByName('foo')).toEqual(['name', 'dependencies', 'devDependencies', 'peerDependencies']);
expect(getByName('bar')).toEqual(['name', 'dependencies', 'devDependencies']);
expect(getByName('baz')).toEqual(['name', 'devDependencies', 'peerDependencies']);
const getByName = (name) =>
Object.keys(_.find(result, (v) => v.data.name === name).data);
expect(getByName('foo')).toEqual([
'name',
'dependencies',
'devDependencies',
'peerDependencies'
]);
expect(getByName('bar')).toEqual([
'name',
'dependencies',
'devDependencies'
]);
expect(getByName('baz')).toEqual([
'name',
'devDependencies',
'peerDependencies'
]);
});
});

Expand Down Expand Up @@ -79,7 +103,12 @@ describe('setVersion', () => {
{ gulp: '3.9.1' }
),
createMockDescriptor('bar', { chalk: '1.0.0' }, { jest: '25.0.0' }),
createMockDescriptor('baz', null, { npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' }, { gulp: '*' })
createMockDescriptor(
'baz',
null,
{ npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' },
{ gulp: '*' }
)
])
);
});
Expand All @@ -97,7 +126,12 @@ describe('setVersionRange', () => {
{ gulp: '^3.9.1' }
),
createMockDescriptor('bar', { chalk: '^1.0.0' }, { jest: '^22.1.4' }),
createMockDescriptor('baz', null, { npm: 'https://github.com/npm/npm.git', prettier: '^1.10.2' }, { gulp: '*' })
createMockDescriptor(
'baz',
null,
{ npm: 'https://github.com/npm/npm.git', prettier: '^1.10.2' },
{ gulp: '*' }
)
])
);
});
Expand All @@ -115,25 +149,39 @@ describe('setVersionsToNewestMismatch', () => {
{ gulp: '*' }
),
createMockDescriptor('bar', { chalk: '2.3.0' }, { jest: '22.1.4' }),
createMockDescriptor('baz', null, { npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' }, { gulp: '*' })
createMockDescriptor(
'baz',
null,
{ npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' },
{ gulp: '*' }
)
])
);
});
it('rewrites the updated manifests with the correct data', async () => {
await setVersionsToNewestMismatch(pattern);
expect(readFileSync('/Users/you/Dev/monorepo/packages/foo/package.json', 'utf8')).toEqual(
expect(
readFileSync('/Users/you/Dev/monorepo/packages/foo/package.json', 'utf8')
).toEqual(
createFile(
'foo',
{ chalk: '2.3.0', commander: '2.13.0' },
{ jest: '22.1.4', prettier: '1.10.2', rimraf: '2.6.2' },
{ gulp: '*' }
)
);
expect(readFileSync('/Users/you/Dev/monorepo/packages/bar/package.json', 'utf8')).toEqual(
createFile('bar', { chalk: '2.3.0' }, { jest: '22.1.4' })
);
expect(readFileSync('/Users/you/Dev/monorepo/packages/baz/package.json', 'utf8')).toEqual(
createFile('baz', null, { npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' }, { gulp: '*' })
expect(
readFileSync('/Users/you/Dev/monorepo/packages/bar/package.json', 'utf8')
).toEqual(createFile('bar', { chalk: '2.3.0' }, { jest: '22.1.4' }));
expect(
readFileSync('/Users/you/Dev/monorepo/packages/baz/package.json', 'utf8')
).toEqual(
createFile(
'baz',
null,
{ npm: 'https://github.com/npm/npm.git', prettier: '1.10.2' },
{ gulp: '*' }
)
);
});
});
38 changes: 29 additions & 9 deletions src/manifests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ import { getManifests } from './get-manifests';
import { manifestData } from './manifest-data';

export type Format = (...sources: string[]) => Promise<IManifestDescriptor[]>;
export type GetMismatchedVersions = (...sources: string[]) => Promise<IDictionary<string[]>>;
export type GetVersions = (...sources: string[]) => Promise<IDictionary<string[]>>;
export type SetVersion = (name: string, version: string, ...sources: string[]) => Promise<IManifestDescriptor[]>;
export type SetVersionRange = (range: string, ...sources: string[]) => Promise<IManifestDescriptor[]>;
export type SetVersionsToNewestMismatch = (...sources: string[]) => Promise<IManifestDescriptor[]>;
export type GetMismatchedVersions = (
...sources: string[]
) => Promise<IDictionary<string[]>>;
export type GetVersions = (
...sources: string[]
) => Promise<IDictionary<string[]>>;
export type SetVersion = (
name: string,
version: string,
...sources: string[]
) => Promise<IManifestDescriptor[]>;
export type SetVersionRange = (
range: string,
...sources: string[]
) => Promise<IManifestDescriptor[]>;
export type SetVersionsToNewestMismatch = (
...sources: string[]
) => Promise<IManifestDescriptor[]>;

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

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

export const format: Format = (...sources) =>
getManifests(...sources)
Expand Down Expand Up @@ -51,7 +69,9 @@ export const setVersionRange: SetVersionRange = (range, ...sources) =>
})
.then(writeDescriptors);

export const setVersionsToNewestMismatch: SetVersionsToNewestMismatch = (...sources) =>
export const setVersionsToNewestMismatch: SetVersionsToNewestMismatch = (
...sources
) =>
getManifests(...sources)
.then((descriptors) => {
const data = unwrap(descriptors);
Expand Down
Loading

0 comments on commit 75e3de9

Please sign in to comment.