From f20bd686f0025bdd45abe36bc501070049a29106 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 14 Nov 2023 16:16:35 -0500 Subject: [PATCH] fix: ensure package.json entries are in a reasonable order (#351) --- lib/package_json.test.ts | 70 ++++++++++++++++++++++------------------ lib/package_json.ts | 46 +++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 32 deletions(-) diff --git a/lib/package_json.test.ts b/lib/package_json.test.ts index b45a5d4..d8a6b81 100644 --- a/lib/package_json.test.ts +++ b/lib/package_json.test.ts @@ -468,38 +468,46 @@ Deno.test("peer dependencies", () => { }, }; - assertEquals(getPackageJson(props), { - name: "package", - version: "0.1.0", - main: "./script/mod.js", - module: "./esm/mod.js", - types: "./types/mod.d.ts", - dependencies: { - dep: "^1.0.0", - }, - peerDependencies: { - peerDep: "^2.0.0", - }, - devDependencies: { - "@types/node": versions.nodeTypes, - "picocolors": versions.picocolors, - "test-dep": "0.1.0", - "@deno/shim-deno": "~0.1.0", - }, - exports: { - ".": { - import: { - types: "./types/mod.d.ts", - default: "./esm/mod.js", + // the stringify ensures that the order looks ok as well + assertEquals( + JSON.stringify(getPackageJson(props), null, 2), + JSON.stringify( + { + name: "package", + version: "0.1.0", + main: "./script/mod.js", + module: "./esm/mod.js", + types: "./types/mod.d.ts", + exports: { + ".": { + import: { + types: "./types/mod.d.ts", + default: "./esm/mod.js", + }, + require: { + types: "./types/mod.d.ts", + default: "./script/mod.js", + }, + }, }, - require: { - types: "./types/mod.d.ts", - default: "./script/mod.js", + scripts: { + test: "node test_runner.js", + }, + dependencies: { + dep: "^1.0.0", + }, + peerDependencies: { + peerDep: "^2.0.0", + }, + devDependencies: { + "@types/node": versions.nodeTypes, + "picocolors": versions.picocolors, + "test-dep": "0.1.0", + "@deno/shim-deno": "~0.1.0", }, }, - }, - scripts: { - test: "node test_runner.js", - }, - }); + null, + 2, + ), + ); }); diff --git a/lib/package_json.ts b/lib/package_json.ts index 2839c23..8545e6a 100644 --- a/lib/package_json.ts +++ b/lib/package_json.ts @@ -107,7 +107,7 @@ export function getPackageJson({ } : {}; - return { + const final: Record = { ...mainExport, ...binaryExport, ...packageJsonObj, @@ -151,6 +151,7 @@ export function getPackageJson({ devDependencies, }), }; + return sortObject(final); function shouldIncludeTypesNode() { if (Object.keys(dependencies).includes("@types/node")) { @@ -183,3 +184,46 @@ export function getPackageJson({ return obj; } } + +function sortObject(obj: Record) { + const highPrecedence = [ + "name", + "version", + "description", + "keywords", + "author", + "homepage", + "repository", + "license", + "bugs", + "main", + "module", + "types", + "typings", + "exports", + "scripts", + ]; + const lowPrecedence = ["dependencies", "peerDependencies", "devDependencies"]; + const sortedObj: Record = {}; + const finalEntries: Record = {}; + for (const key of highPrecedence) { + if (key in obj) { + sortedObj[key] = obj[key]; + delete obj[key]; + } + } + for (const key of lowPrecedence) { + if (key in obj) { + finalEntries[key] = obj[key]; + delete obj[key]; + } + } + for (const key of Object.keys(obj)) { + sortedObj[key] = obj[key]; + } + for (const [key, value] of Object.entries(finalEntries)) { + sortedObj[key] = value; + } + + return sortedObj; +}