diff --git a/.changeset/rare-apples-know.md b/.changeset/rare-apples-know.md new file mode 100644 index 000000000..170181730 --- /dev/null +++ b/.changeset/rare-apples-know.md @@ -0,0 +1,5 @@ +--- +'skuba': patch +--- + +**configure, init:** Format `package.json` consistently diff --git a/src/cli/configure/processing/json.test.ts b/src/cli/configure/processing/json.test.ts index ea802b8a3..2eaf93d43 100644 --- a/src/cli/configure/processing/json.test.ts +++ b/src/cli/configure/processing/json.test.ts @@ -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', () => { diff --git a/src/cli/configure/processing/json.ts b/src/cli/configure/processing/json.ts index f19bd077a..eb5169ef7 100644 --- a/src/cli/configure/processing/json.ts +++ b/src/cli/configure/processing/json.ts @@ -2,7 +2,10 @@ import prettier from 'prettier'; import { isObject } from '../../../utils/validation'; -export const formatObject = (data: Record) => { +export const formatObject = ( + data: Record, + filepath?: string, +) => { const sortedData = Object.fromEntries( Object.entries(data).sort(([keyA], [keyB]) => String(keyA).localeCompare(String(keyB)), @@ -11,7 +14,10 @@ export const formatObject = (data: Record) => { 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 = ( diff --git a/src/cli/configure/processing/package.test.ts b/src/cli/configure/processing/package.test.ts index 3ef6c3251..eaa754c2c 100644 --- a/src/cli/configure/processing/package.test.ts +++ b/src/cli/configure/processing/package.test.ts @@ -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\\" diff --git a/src/cli/configure/processing/package.ts b/src/cli/configure/processing/package.ts index 9499b9054..316cbc0a6 100644 --- a/src/cli/configure/processing/package.ts +++ b/src/cli/configure/processing/package.ts @@ -35,7 +35,7 @@ export const formatPackage = (data: PackageJson) => { delete data.version; } - return formatObject(data); + return formatObject(data, 'package.json'); }; export const parsePackage = (