Skip to content

Commit

Permalink
fix(format): write files if only whitespace changes
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
JamieMason committed Aug 1, 2021
1 parent a61d384 commit f38ea40
Show file tree
Hide file tree
Showing 14 changed files with 29,541 additions and 69 deletions.
53 changes: 53 additions & 0 deletions src/commands/__snapshots__/fix-mismatches.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Array [
},
},
"filePath": "/a/package.json",
"json": "{
\\"dependencies\\": {
\\"foo\\": \\"link:vendor/foo-0.1.0\\"
}
}
",
},
Object {
"contents": Object {
Expand All @@ -17,6 +23,12 @@ Array [
},
},
"filePath": "/b/package.json",
"json": "{
\\"dependencies\\": {
\\"foo\\": \\"link:vendor/foo-0.2.0\\"
}
}
",
},
Object {
"contents": Object {
Expand All @@ -25,6 +37,12 @@ Array [
},
},
"filePath": "/c/package.json",
"json": "{
\\"dependencies\\": {
\\"foo\\": \\"0.3.0\\"
}
}
",
},
Object {
"contents": Object {
Expand All @@ -33,6 +51,12 @@ Array [
},
},
"filePath": "/d/package.json",
"json": "{
\\"dependencies\\": {
\\"foo\\": \\"0.2.0\\"
}
}
",
},
]
`;
Expand All @@ -46,6 +70,12 @@ Array [
},
},
"filePath": "/a/package.json",
"json": "{
\\"dependencies\\": {
\\"foo\\": \\"0.1.0\\"
}
}
",
},
Object {
"contents": Object {
Expand All @@ -54,13 +84,24 @@ Array [
},
},
"filePath": "/b/package.json",
"json": "{
\\"devDependencies\\": {
\\"foo\\": \\"0.2.0\\"
}
}
",
},
Object {
"contents": Object {
"name": "foo",
"version": "0.0.1",
},
"filePath": "/foo/package.json",
"json": "{
\\"name\\": \\"foo\\",
\\"version\\": \\"0.0.1\\"
}
",
},
]
`;
Expand All @@ -74,6 +115,12 @@ Array [
},
},
"filePath": "/a/package.json",
"json": "{
\\"dependencies\\": {
\\"foo\\": \\"0.1.0\\"
}
}
",
},
Object {
"contents": Object {
Expand All @@ -82,6 +129,12 @@ Array [
},
},
"filePath": "/b/package.json",
"json": "{
\\"devDependencies\\": {
\\"foo\\": \\"0.2.0\\"
}
}
",
},
]
`;
3 changes: 2 additions & 1 deletion src/commands/format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { withJson } from '../../test/mock';
import { DEFAULT_CONFIG } from '../constants';
import { format } from './format';
import { Source, SourceWrapper } from './lib/get-wrappers';

const createWrapper = (contents: Source): SourceWrapper => ({ contents, filePath: '' });
const createWrapper = (contents: Source): SourceWrapper => withJson({ contents, filePath: '' });

describe('format', () => {
it('sorts Array properties alphabetically by value', () => {
Expand Down
53 changes: 29 additions & 24 deletions src/commands/lib/get-wrappers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { removeSync } from 'fs-extra';
import { getWrappers, SourceWrapper } from './get-wrappers';
import mock = require('mock-fs');
import { toJson, withJson } from '../../../test/mock';

describe('getWrappers', () => {
afterEach(() => {
Expand All @@ -13,21 +14,22 @@ describe('getWrappers', () => {

beforeEach(() => {
mock({
'package.json': JSON.stringify({ name: 'root' }),
'cli/a/package.json': JSON.stringify({ name: 'cli-a' }),
'cli/b/package.json': JSON.stringify({ name: 'cli-b' }),
'lerna.json': JSON.stringify({ packages: ['lerna/*'] }),
'lerna/a/package.json': JSON.stringify({ name: 'lerna-a' }),
'lerna/b/package.json': JSON.stringify({ name: 'lerna-b' }),
'packages/a/package.json': JSON.stringify({ name: 'packages-a' }),
'packages/b/package.json': JSON.stringify({ name: 'packages-b' }),
'package.json': toJson({ name: 'root' }),
'cli/a/package.json': toJson({ name: 'cli-a' }),
'cli/b/package.json': toJson({ name: 'cli-b' }),
'lerna.json': toJson({ packages: ['lerna/*'] }),
'lerna/a/package.json': toJson({ name: 'lerna-a' }),
'lerna/b/package.json': toJson({ name: 'lerna-b' }),
'packages/a/package.json': toJson({ name: 'packages-a' }),
'packages/b/package.json': toJson({ name: 'packages-b' }),
});
});

const getShape = (name: string, filePath: string): SourceWrapper => ({
contents: { name },
filePath: `${process.cwd()}/${filePath}`,
});
const getShape = (name: string, filePath: string): SourceWrapper =>
withJson({
contents: { name },
filePath: `${process.cwd()}/${filePath}`,
});

it('prefers CLI', () => {
const program = { source: ['cli/*/package.json'] };
Expand Down Expand Up @@ -64,16 +66,16 @@ describe('getWrappers', () => {
describe('when configuration is an array', () => {
beforeEach(() => {
mock({
'package.json': JSON.stringify({ workspaces: ['as-array/*'] }),
'as-array/a/package.json': JSON.stringify({ name: 'packages-a' }),
'as-array/b/package.json': JSON.stringify({ name: 'packages-b' }),
'package.json': toJson({ workspaces: ['as-array/*'] }),
'as-array/a/package.json': toJson({ name: 'packages-a' }),
'as-array/b/package.json': toJson({ name: 'packages-b' }),
});
});

it('should resolve correctly', () => {
const program = { source: [] };
expect(getWrappers(program)).toEqual([
{ contents: { workspaces: ['as-array/*'] }, filePath: `${process.cwd()}/package.json` },
withJson({ contents: { workspaces: ['as-array/*'] }, filePath: `${process.cwd()}/package.json` }),
getShape('packages-a', 'as-array/a/package.json'),
getShape('packages-b', 'as-array/b/package.json'),
]);
Expand All @@ -83,16 +85,19 @@ describe('getWrappers', () => {
describe('when configuration is an object', () => {
beforeEach(() => {
mock({
'package.json': JSON.stringify({ workspaces: { packages: ['as-object/*'] } }),
'as-object/a/package.json': JSON.stringify({ name: 'packages-a' }),
'as-object/b/package.json': JSON.stringify({ name: 'packages-b' }),
'package.json': toJson({ workspaces: { packages: ['as-object/*'] } }),
'as-object/a/package.json': toJson({ name: 'packages-a' }),
'as-object/b/package.json': toJson({ name: 'packages-b' }),
});
});

it('should resolve correctly', () => {
const program = { source: [] };
expect(getWrappers(program)).toEqual([
{ contents: { workspaces: { packages: ['as-object/*'] } }, filePath: `${process.cwd()}/package.json` },
withJson({
contents: { workspaces: { packages: ['as-object/*'] } },
filePath: `${process.cwd()}/package.json`,
}),
getShape('packages-a', 'as-object/a/package.json'),
getShape('packages-b', 'as-object/b/package.json'),
]);
Expand All @@ -107,17 +112,17 @@ describe('getWrappers', () => {

beforeEach(() => {
mock({
'package.json': JSON.stringify({ name: 'root' }),
'package.json': toJson({ name: 'root' }),
'pnpm-workspace.yaml': ['packages:', ' - "./*"'].join('\n'),
'a/package.json': JSON.stringify({ name: 'package-a' }),
'b/package.json': JSON.stringify({ name: 'package-b' }),
'a/package.json': toJson({ name: 'package-a' }),
'b/package.json': toJson({ name: 'package-b' }),
});
});

it('should resolve correctly', () => {
const program = { source: [] };
expect(getWrappers(program)).toEqual([
{ contents: { name: 'root' }, filePath: `${process.cwd()}/package.json` },
withJson({ contents: { name: 'root' }, filePath: `${process.cwd()}/package.json` }),
getShape('package-a', 'a/package.json'),
getShape('package-b', 'b/package.json'),
]);
Expand Down
16 changes: 13 additions & 3 deletions src/commands/lib/get-wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArrayOfStrings } from 'expect-more';
import { readJsonSync } from 'fs-extra';
import { readFileSync, readJsonSync } from 'fs-extra';
import { sync } from 'glob';
import { join, resolve } from 'path';
import { sync as readYamlFileSync } from 'read-yaml-file';
Expand All @@ -21,14 +21,17 @@ export interface Source {
resolutions?: { [key: string]: string };
scripts?: { [key: string]: string };
version?: string;
[otherProps: string]: string | string[] | { [key: string]: string } | undefined;
workspaces?: string[] | { [key: string]: string[] };
[otherProps: string]: string | string[] | { [key: string]: string | string[] } | undefined;
}

export interface SourceWrapper {
/** the absolute path on disk to this package.json file */
filePath: string;
/** the parsed JSON contents of this package.json file */
contents: Source;
/** the raw file contents of this package.json file */
json: string;
}

const getPatternsFromJson = (
Expand Down Expand Up @@ -64,7 +67,14 @@ const getPnpmPatterns = (): string[] | null => {
const getDefaultPatterns = (): string[] => ALL_PATTERNS;
const resolvePattern = (pattern: string): string[] => sync(pattern, { absolute: true });
const reduceFlatArray = (all: string[], next: string[]): string[] => all.concat(next);
const createWrapper = (filePath: string): SourceWrapper => ({ contents: readJsonSync(filePath), filePath });
const createWrapper = (filePath: string): SourceWrapper => {
const json = readFileSync(filePath, { encoding: 'utf8' });
return {
contents: JSON.parse(json),
filePath,
json,
};
};

export const getWrappers = (program: Options): SourceWrapper[] =>
(getCliPatterns(program) || getYarnPatterns() || getPnpmPatterns() || getLernaPatterns() || getDefaultPatterns())
Expand Down

0 comments on commit f38ea40

Please sign in to comment.