UNPKG

everything-dev

Version:

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

249 lines (247 loc) • 9.9 kB
const require_load = require('../resolution/load.cjs'); const require_bos_ref = require('../../utils/bos-ref.cjs'); const require_catalog = require('../workspace/catalog.cjs'); const require_workspace = require('../workspace/workspace.cjs'); const require_framework_version = require('../framework-version.cjs'); const require_init = require('./init.cjs'); const require_json_file = require('../../utils/json-file.cjs'); let node_fs = require("node:fs"); let node_path = require("node:path"); let glob = require("glob"); //#region src/services/scaffold/upgrade-packages.ts const FRAMEWORK_PACKAGES = ["everything-dev", "every-plugin"]; const AUTH_CORE_PACKAGE = "@better-auth/core"; const AUTH_CORE_TRIGGER_PACKAGES = [ "better-auth", "better-near-auth", "@better-auth/api-key", "@better-auth/passkey" ]; function workspaceDirOf(projectDir) { return require_workspace.findWorkspaceRoot(projectDir)?.dir ?? projectDir; } function readRootPackageJson(projectDir) { return require_json_file.readJsonFile((0, node_path.join)(workspaceDirOf(projectDir), "package.json")); } function readRootCatalogEntry(projectDir, packageName) { return require_catalog.readWorkspaceCatalog(workspaceDirOf(projectDir))[packageName]; } /** * Chain-catalog vs local-catalog diff for the drift alarm (ADR 0034 pt 6): * entries only count when the local catalog actually pins a different version * (`from !== undefined`) — a purely-additive chain entry the child does not * consume is not drift. Reading the local catalog is `readWorkspaceCatalog` * (pnpm-workspace.yaml first, package.json fallback) — the v1 package.json-only * read made every entry read as new on v2 children, a permanent false positive. */ function diffCatalogEntries(sourceRootCatalog, currentCatalogEntries) { const updates = []; for (const [name, targetVersion] of Object.entries(sourceRootCatalog)) { if (FRAMEWORK_PACKAGES.includes(name)) continue; const currentVersion = currentCatalogEntries[name]; if (currentVersion === targetVersion) continue; updates.push({ name, from: currentVersion, to: targetVersion }); } return updates; } function readCurrentPackageSpecifier(projectDir, packageName) { const pkg = readRootPackageJson(projectDir); for (const fieldName of [ "dependencies", "devDependencies", "peerDependencies" ]) { const value = pkg[fieldName]?.[packageName]; if (!value) continue; if (value === "catalog:") return readRootCatalogEntry(projectDir, packageName) ?? readInstalledVersion(projectDir, packageName); if (value.startsWith("workspace:") || value.startsWith("file:") || value.startsWith("link:")) return readInstalledVersion(projectDir, packageName); return value; } return readRootCatalogEntry(projectDir, packageName) ?? readInstalledVersion(projectDir, packageName); } function setCatalogRefs(field, packageNames) { if (!field) return false; let modified = false; for (const packageName of packageNames) if (setCatalogRef(field, packageName)) modified = true; return modified; } function syncPackageObjectCatalogRefs(pkg, packageNames) { let modified = false; for (const fieldName of [ "dependencies", "devDependencies", "peerDependencies" ]) { const field = pkg[fieldName]; if (setCatalogRefs(field, packageNames)) modified = true; } return modified; } function findPackageFieldWithAnyDependency(pkg, packageNames) { for (const fieldName of [ "dependencies", "devDependencies", "peerDependencies" ]) { const field = pkg[fieldName]; if (!field) continue; if (packageNames.some((packageName) => typeof field[packageName] === "string")) return field; } } function packageObjectNeedsAuthCoreCatalogRef(pkg) { if (!findPackageFieldWithAnyDependency(pkg, AUTH_CORE_TRIGGER_PACKAGES)) return false; for (const fieldName of [ "dependencies", "devDependencies", "peerDependencies" ]) if (pkg[fieldName]?.["@better-auth/core"]) return false; return true; } function ensureAuthCoreCatalogRef(pkg) { if (!packageObjectNeedsAuthCoreCatalogRef(pkg)) return false; const targetField = findPackageFieldWithAnyDependency(pkg, AUTH_CORE_TRIGGER_PACKAGES); if (!targetField) return false; targetField[AUTH_CORE_PACKAGE] = "catalog:"; return true; } function packageObjectNeedsCatalogRefs(pkg, packageNames) { for (const fieldName of [ "dependencies", "devDependencies", "peerDependencies" ]) { const field = pkg[fieldName]; if (!field) continue; for (const packageName of packageNames) { const value = field[packageName]; if (!value) continue; if (value !== "catalog:" && !value.startsWith("file:") && !value.startsWith("link:")) return true; } } return false; } function packageFileNeedsCatalogRefs(filePath, packageNames) { return packageObjectNeedsCatalogRefs(require_json_file.readJsonFile(filePath), packageNames); } function updatePackageFileCatalogRefs(filePath, packageNames) { const pkg = require_json_file.readJsonFile(filePath); const modified = syncPackageObjectCatalogRefs(pkg, packageNames); if (modified) (0, node_fs.writeFileSync)(filePath, `${JSON.stringify(pkg, null, 2)}\n`); return modified; } function updatePackageFileAuthCoreRef(filePath) { const pkg = require_json_file.readJsonFile(filePath); const modified = ensureAuthCoreCatalogRef(pkg); if (modified) (0, node_fs.writeFileSync)(filePath, `${JSON.stringify(pkg, null, 2)}\n`); return modified; } function syncRootCatalogWithParent(projectDir, parentCatalog) { const workspaceDir = workspaceDirOf(projectDir); const pkgPath = (0, node_path.join)(workspaceDir, "package.json"); const pkg = require_json_file.readJsonFile(pkgPath); const modified = syncPackageObjectCatalogRefs(pkg, Object.keys(parentCatalog)); let catalogChanged = false; const catalog = require_catalog.readWorkspaceCatalog(workspaceDir); for (const [packageName, version] of Object.entries(parentCatalog)) if (catalog[packageName] !== version) { catalog[packageName] = version; catalogChanged = true; } if (catalogChanged) require_catalog.writeWorkspaceCatalog(workspaceDir, catalog); if (modified) (0, node_fs.writeFileSync)(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`); return modified || catalogChanged; } function readInstalledVersion(projectDir, packageName) { return require_framework_version.readInstalledFrameworkVersion(projectDir, packageName); } function setCatalogRef(field, packageName) { if (!field || !(packageName in field)) return false; if (field[packageName] === "catalog:" || field[packageName].startsWith("file:")) return false; field[packageName] = "catalog:"; return true; } async function findWorkspacePackageJsons(projectDir) { const root = require_workspace.findWorkspaceRoot(projectDir); if (!root) return []; const pkgPaths = []; for (const member of root.members) { const pkgPath = (0, node_path.join)(member.dir, "package.json"); if ((0, node_fs.existsSync)(pkgPath) && (0, node_fs.statSync)(pkgPath).isFile()) pkgPaths.push(pkgPath); } return [...new Set(pkgPaths)]; } async function migrateChildRootPackageJson(projectDir) { const config = await require_load.readAuthoredConfigInput(projectDir); const pkgPath = (0, node_path.join)(workspaceDirOf(projectDir), "package.json"); if (!config || !(0, node_fs.existsSync)(pkgPath)) return false; if (!require_bos_ref.getExtendsRef(config)?.startsWith("bos://")) return false; const pkg = require_json_file.readJsonFile(pkgPath); let changed = false; if (pkg.private !== true) { pkg.private = true; changed = true; } if (pkg.type !== "module") { pkg.type = "module"; changed = true; } if ("module" in pkg) { delete pkg.module; changed = true; } const pluginPackageJsons = await (0, glob.glob)("plugins/*/package.json", { cwd: projectDir, nodir: true, dot: false, absolute: false }); const childScripts = require_init.buildChildRootScripts({ ui: (0, node_fs.existsSync)((0, node_path.join)(projectDir, "ui", "package.json")), api: (0, node_fs.existsSync)((0, node_path.join)(projectDir, "api", "package.json")), host: (0, node_fs.existsSync)((0, node_path.join)(projectDir, "host", "package.json")), plugins: pluginPackageJsons.length > 0 }); if (!pkg.scripts || typeof pkg.scripts !== "object") { pkg.scripts = {}; changed = true; } const scripts = pkg.scripts; for (const [key, value] of Object.entries(childScripts)) if (scripts[key] !== value) { scripts[key] = value; changed = true; } for (const obsoleteScript of [ ...require_init.getParentOnlyScriptKeys(projectDir), "sync-catalog", "init" ]) if (obsoleteScript in scripts) { delete scripts[obsoleteScript]; changed = true; } if (changed) (0, node_fs.writeFileSync)(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`); return changed; } //#endregion exports.AUTH_CORE_PACKAGE = AUTH_CORE_PACKAGE; exports.AUTH_CORE_TRIGGER_PACKAGES = AUTH_CORE_TRIGGER_PACKAGES; exports.FRAMEWORK_PACKAGES = FRAMEWORK_PACKAGES; exports.diffCatalogEntries = diffCatalogEntries; exports.ensureAuthCoreCatalogRef = ensureAuthCoreCatalogRef; exports.findWorkspacePackageJsons = findWorkspacePackageJsons; exports.migrateChildRootPackageJson = migrateChildRootPackageJson; exports.packageFileNeedsCatalogRefs = packageFileNeedsCatalogRefs; exports.packageObjectNeedsAuthCoreCatalogRef = packageObjectNeedsAuthCoreCatalogRef; exports.packageObjectNeedsCatalogRefs = packageObjectNeedsCatalogRefs; exports.readCurrentPackageSpecifier = readCurrentPackageSpecifier; exports.readInstalledVersion = readInstalledVersion; exports.readRootCatalogEntry = readRootCatalogEntry; exports.readRootPackageJson = readRootPackageJson; exports.setCatalogRef = setCatalogRef; exports.syncRootCatalogWithParent = syncRootCatalogWithParent; exports.updatePackageFileAuthCoreRef = updatePackageFileAuthCoreRef; exports.updatePackageFileCatalogRefs = updatePackageFileCatalogRefs; exports.workspaceDirOf = workspaceDirOf; //# sourceMappingURL=upgrade-packages.cjs.map