@alexaegis/workspace-tools
Version:
Tools for working with javascript workspaces
270 lines (269 loc) • 7.52 kB
JavaScript
import { t as getWorkspaceRoot } from "./get-workspace-root.function-DnORClbN.js";
import { join } from "node:path";
import { normalizeCwdOption, readJson } from "@alexaegis/fs";
//#region src/sort/default-package-json-order.const.ts
var 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",
{
key: "publishConfig",
order: [
"access",
{
key: "exports",
order: [{
key: ".*",
order: [
"types",
".*",
"default"
]
}]
},
".*"
]
},
".*",
"engines",
"packageManager",
"workspaces",
{
key: "scripts",
order: [
"^build.*",
"^lint.*",
"^test.*"
]
},
{
key: "exports",
order: [{
key: ".*",
order: [
"types",
".*",
"default"
]
}]
},
"bin",
"dependencies",
"peerDependencies",
"optionalDependencies",
"devDependencies"
];
//#endregion
//#region src/sort/normalize-sorting-preference-for-package-json.function.ts
/**
* The types field in the exports object has to be the first one. This
* sortingPreference normalizer ensures that.
*
* It's also recommended to have the 'default' key be the last. This is also
* ensured here.
*
* @returns a sortingPreference that will always be suited for packageJson files
*/
var normalizeExportsOrder = (sortingPrefrence, targetKey) => {
if (typeof sortingPrefrence === "string" && sortingPrefrence === targetKey) return {
key: targetKey,
order: [{
key: ".*",
order: [
"types",
".*",
"default"
]
}]
};
else if (typeof sortingPrefrence === "object" && sortingPrefrence.key === targetKey) return normalizeExportsDetailedEntry(sortingPrefrence, targetKey);
return sortingPrefrence;
};
var normalizeExportsDetailedEntry = (entry, targetKey) => {
const order = entry.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) => typeof o === "object" && o.key === ".*")) order.push({
key: ".*",
order: [
"types",
".*",
"default"
]
});
return {
key: targetKey,
order
};
};
var normalizePublishConfigOrder = (sortingPrefrence) => {
if (typeof sortingPrefrence === "string" && sortingPrefrence === "publishConfig") return {
key: "publishConfig",
order: [
"access",
{
key: "exports",
order: [{
key: ".*",
order: [
"types",
".*",
"default"
]
}]
},
".*"
]
};
else if (typeof sortingPrefrence === "object" && sortingPrefrence.key === "publishConfig") return {
key: "publishConfig",
order: sortingPrefrence.order.map((inner) => normalizeExportsOrder(inner, "exports"))
};
return sortingPrefrence;
};
var normalizeSortingPreferenceForPackageJson = (sortingPreferences) => {
return sortingPreferences.some((sortingPrefrence) => typeof sortingPrefrence === "string" && sortingPrefrence === "exports" || typeof sortingPrefrence === "object" && sortingPrefrence.key === "exports") ? sortingPreferences.map((sortingPrefrence) => {
const normalized = normalizePublishConfigOrder(sortingPrefrence);
if (normalized !== sortingPrefrence) return normalized;
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.map((p) => normalizePublishConfigOrder(p)), {
key: "exports",
order: [{
key: ".*",
order: [
"types",
".*",
"default"
]
}]
}];
};
//#endregion
//#region src/sort/normalize-json-sorting-preference.function.ts
/**
* Creates a function that can receive an optional objectKeyOrder and return it.
* It will also try to load a file from .config/${filename}.sort.json
* and treat it as the sorting preference if no custom preference is passed.
* That's why this function is async, but the normalizer itself is not.
*
* If the fileName passed is package.json it will also perform some adjustments
* to any order received to make sure the exports object will be valid.
*
* For package.json's only, when there's no keyorder is passed and there's no
* .config/package.sort.json is present, an opinionated order is returned
* specifically for package.json files.
*
*/
var createJsonSortingPreferenceNormalizer = async (fileName, rawOptions) => {
const workspaceRoot = getWorkspaceRoot(normalizeCwdOption(rawOptions));
let workspaceSortPreference;
if (workspaceRoot) workspaceSortPreference = await readJson(join(workspaceRoot, ".config", fileName.replace(/\.json$/, ".sort.json")));
return (sortingPreference) => {
let sortingOrder = sortingPreference ?? workspaceSortPreference?.sort;
if (fileName === "package.json") if (sortingPreference) sortingOrder = normalizeSortingPreferenceForPackageJson(sortingPreference);
else if (workspaceSortPreference?.sort) sortingOrder = normalizeSortingPreferenceForPackageJson(workspaceSortPreference.sort);
else sortingOrder = normalizeSortingPreferenceForPackageJson(DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE);
return sortingOrder ?? [];
};
};
//#endregion
export { normalizeSortingPreferenceForPackageJson as n, DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE as r, createJsonSortingPreferenceNormalizer as t };
//# sourceMappingURL=sort-CAYg0bDG.js.map