UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

51 lines (49 loc) • 1.79 kB
import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { parse, stringify } from "yaml"; //#region src/workspace-catalog.ts const WORKSPACE_YAML = "pnpm-workspace.yaml"; function catalogFromPackageJson(pkgJson) { const catalog = pkgJson.workspaces?.catalog; return catalog && typeof catalog === "object" ? { ...catalog } : {}; } function readWorkspaceCatalog(root) { const yamlPath = join(root, WORKSPACE_YAML); if (existsSync(yamlPath)) { const catalog = parse(readFileSync(yamlPath, "utf8"))?.catalog; return catalog && typeof catalog === "object" ? { ...catalog } : {}; } const pkgPath = join(root, "package.json"); if (existsSync(pkgPath)) try { return catalogFromPackageJson(JSON.parse(readFileSync(pkgPath, "utf8"))); } catch { return {}; } return {}; } function writeWorkspaceCatalog(root, catalog) { const yamlPath = join(root, WORKSPACE_YAML); const pkgPath = join(root, "package.json"); const usesYaml = existsSync(yamlPath); const sorted = Object.fromEntries(Object.entries(catalog).sort(([a], [b]) => a.localeCompare(b))); if (usesYaml) { const parsed = parse(readFileSync(yamlPath, "utf8")); const doc = parsed && typeof parsed === "object" ? parsed : {}; doc.catalog = sorted; const body = stringify(doc, { lineWidth: 0 }).trimEnd(); writeFileSync(yamlPath, `${body}\n`); return; } let pkg = {}; if (existsSync(pkgPath)) try { pkg = JSON.parse(readFileSync(pkgPath, "utf8")); } catch { pkg = {}; } if (!pkg.workspaces) pkg.workspaces = {}; pkg.workspaces.catalog = sorted; writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`); } //#endregion export { catalogFromPackageJson, readWorkspaceCatalog, writeWorkspaceCatalog }; //# sourceMappingURL=workspace-catalog.mjs.map