UNPKG

@alexaegis/workspace-tools

Version:

Tools for working with javascript workspaces

126 lines (125 loc) 4.17 kB
import { normalizeCwdOption, readJson } from "@alexaegis/fs"; import { join } from "node:path"; import { g as getWorkspaceRoot, P as PACKAGE_JSON_NAME } from "./get-workspace-root.function-BIw0sIyQ.js"; 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 = normalizeCwdOption(rawOptions); const workspaceRoot = getWorkspaceRoot(options); 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_NAME) { if (sortingPreference) { sortingOrder = normalizeSortingPreferenceForPackageJson(sortingPreference); } else if (workspaceSortPreference?.sort) { sortingOrder = normalizeSortingPreferenceForPackageJson( workspaceSortPreference.sort ); } else { sortingOrder = normalizeSortingPreferenceForPackageJson( DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE ); } } return sortingOrder ?? []; }; }; export { DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE as D, createJsonSortingPreferenceNormalizer as c, normalizeSortingPreferenceForPackageJson as n };