Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure package.json entries are in a reasonable order #351

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading