@alexaegis/workspace-tools
Version:
Tools for working with javascript workspaces
125 lines (124 loc) • 4.33 kB
JavaScript
;
const fs = require("@alexaegis/fs");
const node_path = require("node:path");
const getWorkspaceRoot_function = require("./get-workspace-root.function-CMUtTfQo.cjs");
const DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE = [
"name",
"description",
"version",
"license",
"private",
{
key: "archetype",
order: ["platform", "framework", "language", "kind", "bundler", "testing"]
},
"keywords",
"author",
"homepage",
{
key: "repository",
order: ["url", "type", "directory"]
},
"bugs",
"type",
"config",
"publishConfig",
".*",
"engines",
"packageManager",
"workspaces",
{ key: "scripts", order: ["^build.*", "^lint.*", "^test.*"] },
{ key: "exports", order: [{ key: ".*", order: ["types", ".*", "default"] }] },
"bin",
"dependencies",
"peerDependencies",
"optionalDependencies",
"devDependencies"
];
const normalizeSortingPreferenceForPackageJson = (sortingPreferences) => {
return sortingPreferences.some(
(sortingPrefrence) => typeof sortingPrefrence === "string" && sortingPrefrence === "exports" || typeof sortingPrefrence === "object" && sortingPrefrence.key === "exports"
) ? sortingPreferences.map((sortingPrefrence) => {
if (typeof sortingPrefrence === "string" && sortingPrefrence === "exports") {
return {
key: "exports",
order: [{ key: ".*", order: ["types", ".*", "default"] }]
};
} else if (typeof sortingPrefrence === "object" && sortingPrefrence.key === "exports") {
const order = sortingPrefrence.order.map((ordering) => {
if (typeof ordering === "string") {
return { key: ordering, order: ["types", ".*", "default"] };
} else {
const typesEntry = ordering.order.find(
(o) => typeof o === "string" ? o === "types" : o.key === "types"
);
const defaultEntry = ordering.order.find(
(o) => typeof o === "string" ? o === "default" : o.key === "default"
);
const hasSpread = ordering.order.some(
(o) => typeof o === "string" ? o === ".*" : o.key === ".*"
);
const nonSpecialEntries = ordering.order.filter(
(o) => typeof o === "string" ? o !== "types" && o !== "default" : o.key !== "types" && o.key !== "default"
);
if (!hasSpread) {
nonSpecialEntries.push(".*");
}
return {
key: ordering.key,
order: [
typesEntry ?? "types",
...nonSpecialEntries,
defaultEntry ?? "default"
]
};
}
});
if (!order.some((o) => o.key === ".*")) {
order.push({ key: ".*", order: ["types", ".*", "default"] });
}
return {
key: "exports",
order
};
} else {
return sortingPrefrence;
}
}) : [
...sortingPreferences,
{
key: "exports",
order: [{ key: ".*", order: ["types", ".*", "default"] }]
}
];
};
const createJsonSortingPreferenceNormalizer = async (fileName, rawOptions) => {
const options = fs.normalizeCwdOption(rawOptions);
const workspaceRoot = getWorkspaceRoot_function.getWorkspaceRoot(options);
let workspaceSortPreference;
if (workspaceRoot) {
workspaceSortPreference = await fs.readJson(
node_path.join(workspaceRoot, ".config", fileName.replace(/\.json$/, ".sort.json"))
);
}
return (sortingPreference) => {
let sortingOrder = sortingPreference ?? workspaceSortPreference?.sort;
if (fileName === getWorkspaceRoot_function.PACKAGE_JSON_NAME) {
if (sortingPreference) {
sortingOrder = normalizeSortingPreferenceForPackageJson(sortingPreference);
} else if (workspaceSortPreference?.sort) {
sortingOrder = normalizeSortingPreferenceForPackageJson(
workspaceSortPreference.sort
);
} else {
sortingOrder = normalizeSortingPreferenceForPackageJson(
DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE
);
}
}
return sortingOrder ?? [];
};
};
exports.DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE = DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE;
exports.createJsonSortingPreferenceNormalizer = createJsonSortingPreferenceNormalizer;
exports.normalizeSortingPreferenceForPackageJson = normalizeSortingPreferenceForPackageJson;