Skip to content

Commit

Permalink
Sort dependencies (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Jun 14, 2020
1 parent b85b4b3 commit 0c1e129
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-wombats-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**configure, init:** Sort dependencies
30 changes: 30 additions & 0 deletions src/cli/configure/processing/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ describe('withPackage', () => {
"
`));

it('sorts dependencies', () =>
expect(
withPackage((data) => data)(
JSON.stringify({
devDependencies: {
c: '3',
e: '5',
d: '4',
},
dependencies: {
b: '2',
a: '1',
},
}),
),
).toMatchInlineSnapshot(`
"{
\\"dependencies\\": {
\\"a\\": \\"1\\",
\\"b\\": \\"2\\"
},
\\"devDependencies\\": {
\\"c\\": \\"3\\",
\\"d\\": \\"4\\",
\\"e\\": \\"5\\"
}
}
"
`));

it('handles bad JSON gracefully', () =>
expect(
withPackage((data) => {
Expand Down
13 changes: 13 additions & 0 deletions src/cli/configure/processing/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@ import { PackageJson } from '../types';

import { formatObject, parseObject } from './json';

const sortRecord = <T>(record: Record<string, T>): Record<string, T> =>
Object.fromEntries(
Object.entries(record).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)),
);

export const formatPackage = (data: PackageJson) => {
normalizeData(data);

if (data.dependencies) {
data.dependencies = sortRecord(data.dependencies);
}

if (data.devDependencies) {
data.devDependencies = sortRecord(data.devDependencies);
}

// normalize-package-data fields that aren't useful for applications

delete data._id;
Expand Down

0 comments on commit 0c1e129

Please sign in to comment.