Skip to content

Commit

Permalink
Merge branch 'master' into format-renovate-config
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Jun 24, 2020
2 parents 86ed9de + 92729fd commit 5ad9e44
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-apples-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**configure, init:** Format `package.json` consistently
21 changes: 21 additions & 0 deletions src/cli/configure/processing/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ describe('formatObject', () => {
}
"
`));

it('handles ordinary JSON array formatting', () =>
expect(formatObject({ files: ['1', '2', '3'] })).toMatchInlineSnapshot(`
"{
\\"files\\": [\\"1\\", \\"2\\", \\"3\\"]
}
"
`));

it('handles special-cased package.json array formatting', () =>
expect(formatObject({ files: ['1', '2', '3'] }, 'package.json'))
.toMatchInlineSnapshot(`
"{
\\"files\\": [
\\"1\\",
\\"2\\",
\\"3\\"
]
}
"
`));
});

describe('parseObject', () => {
Expand Down
10 changes: 8 additions & 2 deletions src/cli/configure/processing/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import prettier from 'prettier';

import { isObject } from '../../../utils/validation';

export const formatObject = (data: Record<PropertyKey, unknown>) => {
export const formatObject = (
data: Record<PropertyKey, unknown>,
filepath?: string,
) => {
const sortedData = Object.fromEntries(
Object.entries(data).sort(([keyA], [keyB]) =>
String(keyA).localeCompare(String(keyB)),
Expand All @@ -11,7 +14,10 @@ export const formatObject = (data: Record<PropertyKey, unknown>) => {

const output = JSON.stringify(sortedData, null, 2);

return prettier.format(output, { parser: 'json' });
return prettier.format(
output,
typeof filepath === 'undefined' ? { parser: 'json' } : { filepath },
);
};

export const parseObject = (
Expand Down
5 changes: 4 additions & 1 deletion src/cli/configure/processing/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ describe('withPackage', () => {
\\"d\\": \\"4\\",
\\"e\\": \\"5\\"
},
\\"files\\": [\\"b\\", \\"a\\"],
\\"files\\": [
\\"b\\",
\\"a\\"
],
\\"scripts\\": {
\\"prebuild\\": \\"rm -rf system32\\",
\\"build\\": \\"npm install freebsd\\"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/configure/processing/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const formatPackage = (data: PackageJson) => {
delete data.version;
}

return formatObject(data);
return formatObject(data, 'package.json');
};

export const parsePackage = (
Expand Down

0 comments on commit 5ad9e44

Please sign in to comment.