Skip to content

Commit

Permalink
fix: ensure package.json entries are in a reasonable order (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 14, 2023
1 parent e28b64a commit f20bd68
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 32 deletions.
70 changes: 39 additions & 31 deletions lib/package_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
});
46 changes: 45 additions & 1 deletion lib/package_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function getPackageJson({
}
: {};

return {
const final: Record<string, unknown> = {
...mainExport,
...binaryExport,
...packageJsonObj,
Expand Down Expand Up @@ -151,6 +151,7 @@ export function getPackageJson({
devDependencies,
}),
};
return sortObject(final);

function shouldIncludeTypesNode() {
if (Object.keys(dependencies).includes("@types/node")) {
Expand Down Expand Up @@ -183,3 +184,46 @@ export function getPackageJson({
return obj;
}
}

function sortObject(obj: Record<string, unknown>) {
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<string, unknown> = {};
const finalEntries: Record<string, unknown> = {};
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;
}

0 comments on commit f20bd68

Please sign in to comment.