everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
1 lines • 15.6 kB
Source Map (JSON)
{"version":3,"file":"upgrade.mjs","names":[],"sources":["../../../src/services/scaffold/upgrade.ts"],"sourcesContent":["import { spawnSync } from \"node:child_process\";\nimport { existsSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport process from \"node:process\";\nimport type { PhaseTiming, UpgradeOptions, UpgradeResult } from \"../../contract\";\nimport { readJsonFile } from \"../../utils/json-file\";\nimport { timePhase } from \"../../utils/timing\";\nimport { readWorkspaceCatalog } from \"../workspace/catalog\";\nimport { runPnpmInstallForUpgrade } from \"./init\";\nimport { syncTemplate } from \"./sync\";\nimport { describeMigrationFailure, runMigrationPhase } from \"./upgrade-migration\";\nimport {\n diffCatalogEntries,\n ensureAuthCoreCatalogRef,\n FRAMEWORK_PACKAGES,\n findWorkspacePackageJsons,\n packageFileNeedsCatalogRefs,\n packageObjectNeedsAuthCoreCatalogRef,\n packageObjectNeedsCatalogRefs,\n readCurrentPackageSpecifier,\n readRootPackageJson,\n syncRootCatalogWithParent,\n updatePackageFileAuthCoreRef,\n updatePackageFileCatalogRefs,\n workspaceDirOf,\n} from \"./upgrade-packages\";\nimport {\n buildChangelogUrl,\n loadParentPluginOptions,\n readExtendedRootSource,\n} from \"./upgrade-source\";\n\nexport * from \"./upgrade-migration\";\nexport * from \"./upgrade-packages\";\nexport * from \"./upgrade-source\";\n\nconst UPSTREAM_ISSUE_URL = \"https://github.com/NEARBuilders/everything-dev/issues\";\n\nexport async function upgradeTemplate(\n projectDir: string,\n options: UpgradeOptions,\n): Promise<UpgradeResult> {\n const timings: PhaseTiming[] = [];\n // `--check` is CI's drift alarm (ADR 0034 pt 6): dry-run semantics plus a\n // nonzero exit when updates are available (renderer-side).\n if (options.check) options.dryRun = true;\n const pkgPath = join(workspaceDirOf(projectDir), \"package.json\");\n if (!existsSync(pkgPath)) {\n return {\n status: \"error\",\n packages: [],\n timings,\n error: \"No package.json found in current directory\",\n };\n }\n\n const parentSource = await readExtendedRootSource(projectDir);\n const sourceRootCatalog = parentSource.catalog;\n const inheritedCatalogPackageNames = Object.keys(sourceRootCatalog);\n const currentCatalogEntries = readWorkspaceCatalog(workspaceDirOf(projectDir));\n const workspacePkgPaths = await findWorkspacePackageJsons(projectDir);\n const hasCatalogRefRewrites =\n inheritedCatalogPackageNames.length > 0 &&\n (packageObjectNeedsCatalogRefs(readRootPackageJson(projectDir), inheritedCatalogPackageNames) ||\n workspacePkgPaths.some((pkgPath) =>\n packageFileNeedsCatalogRefs(pkgPath, inheritedCatalogPackageNames),\n ));\n const hasAuthCoreRefRewrites =\n packageObjectNeedsAuthCoreCatalogRef(readRootPackageJson(projectDir)) ||\n workspacePkgPaths.some((pkgPath) =>\n packageObjectNeedsAuthCoreCatalogRef(readJsonFile<Record<string, unknown>>(pkgPath)),\n );\n\n const { packages, catalogVersionUpdates } = await timePhase(\n timings,\n \"check package versions\",\n async () => {\n const nextPackages: UpgradeResult[\"packages\"] = [];\n\n for (const name of FRAMEWORK_PACKAGES) {\n const current = readCurrentPackageSpecifier(projectDir, name);\n const target = sourceRootCatalog[name] ?? current ?? \"unknown\";\n\n nextPackages.push({ name, from: current, to: target });\n }\n\n const nextCatalogVersionUpdates = diffCatalogEntries(\n sourceRootCatalog,\n currentCatalogEntries,\n );\n\n return { packages: nextPackages, catalogVersionUpdates: nextCatalogVersionUpdates };\n },\n );\n\n const hasFrameworkUpdates = packages.some((p) => p.from !== p.to && p.from !== undefined);\n const hasCatalogUpdates = catalogVersionUpdates.some((u) => u.from !== undefined);\n const hasUpdates =\n hasFrameworkUpdates || hasCatalogUpdates || hasCatalogRefRewrites || hasAuthCoreRefRewrites;\n\n if (options.dryRun) {\n let changelogUrl: string | undefined;\n const pluginOptions = options.noSync\n ? null\n : await timePhase(timings, \"list parent plugins\", () => loadParentPluginOptions(projectDir));\n if (hasUpdates) {\n const mainPkg = packages.find((p) => p.name === \"everything-dev\");\n if (mainPkg?.from && mainPkg.from !== mainPkg.to) {\n changelogUrl = buildChangelogUrl(mainPkg.from, mainPkg.to, parentSource.repository);\n }\n }\n\n return {\n status: \"dry-run\",\n packages: [\n ...packages,\n ...catalogVersionUpdates.map((u) => ({ name: u.name, from: u.from, to: u.to })),\n ],\n availablePlugins: pluginOptions?.newPluginKeys,\n platform: {\n resolved: parentSource.platform.manifest.frameworkVersion,\n source: `bos://${parentSource.platform.source.account}/${parentSource.platform.source.gateway}`,\n fromCache: parentSource.platform.fromCache,\n },\n updatesAvailable: hasUpdates,\n timings,\n changelogUrl,\n };\n }\n\n // The version delta that drives the whole migration phase. In the re-exec'd\n // child, packages compare new-installed vs new-catalog (no delta), so the\n // pre-upgrade version must ride the `--from` flag across the boundary;\n // `--migrations-only` without `--from` is a deliberate true no-op.\n const targetMainVersion = packages.find((p) => p.name === \"everything-dev\")?.to;\n const versionDelta =\n options.migrationsOnly && !options.from\n ? false\n : options.from !== undefined && targetMainVersion !== undefined\n ? options.from !== targetMainVersion\n : hasFrameworkUpdates;\n\n if (options.migrationsOnly) {\n return await runMigrationPhase(projectDir, options, parentSource, { versionDelta });\n }\n\n // Pre-flight (ADR 0032): refuse applies of new packages against stale\n // framework-owned files. The template reconciliation must be ready to run\n // when the new packages land; existing conflicts abort before Phase 1 so a\n // resolution (`--force`, or accepting the template) can happen cleanly.\n if (versionDelta && !options.noSync && !options.force) {\n const preflight = await timePhase(timings, \"preflight template sync\", () =>\n syncTemplate(projectDir, { dryRun: true }),\n );\n if (preflight.status === \"conflicted\" || (preflight.conflicted?.length ?? 0) > 0) {\n return {\n status: \"error\",\n packages: packages,\n timings,\n error:\n `Template files conflict with the upstream release — nothing was applied. ` +\n `Review the conflicts or run \\`bos upgrade --force\\` to overwrite (current copies are backed up). ` +\n `To make a local change permanent, file an issue: ${UPSTREAM_ISSUE_URL}. ` +\n `Conflicted: ${(preflight.conflicted ?? []).join(\", \")}`,\n };\n }\n }\n\n // Phase 1: apply package updates, install, then re-exec migrations from the new package\n await timePhase(timings, \"apply package updates\", async () => {\n if (inheritedCatalogPackageNames.length > 0) {\n syncRootCatalogWithParent(projectDir, sourceRootCatalog);\n }\n\n if (inheritedCatalogPackageNames.length > 0) {\n for (const pkgPath of workspacePkgPaths) {\n updatePackageFileCatalogRefs(pkgPath, inheritedCatalogPackageNames);\n }\n }\n\n // @better-auth/core rides the catalog row its plugins need; it is not in\n // every template package.json, so it is ensured here rather than sync'd.\n const rootPkgPath = join(workspaceDirOf(projectDir), \"package.json\");\n const rootPkg = readJsonFile<Record<string, unknown>>(rootPkgPath);\n if (ensureAuthCoreCatalogRef(rootPkg)) {\n writeFileSync(rootPkgPath, `${JSON.stringify(rootPkg, null, 2)}\\n`);\n }\n for (const pkgPath of workspacePkgPaths) {\n updatePackageFileAuthCoreRef(pkgPath);\n }\n });\n\n const needsInstall = (hasUpdates || hasCatalogUpdates) && !options.noInstall;\n\n if (needsInstall) {\n await timePhase(timings, \"install dependencies\", () =>\n runPnpmInstallForUpgrade(workspaceDirOf(projectDir)),\n );\n }\n\n // If we installed new framework packages, re-exec from the new package so migrations\n // run with the latest code instead of the old cached modules.\n if (needsInstall && hasFrameworkUpdates) {\n const bosPath = join(workspaceDirOf(projectDir), \"node_modules\", \".bin\", \"bos\");\n const prevMainPkg = packages.find((p) => p.name === \"everything-dev\");\n const childArgs = [\n \"upgrade\",\n \"--migrations-only\",\n \"--no-install\",\n \"--from\",\n prevMainPkg?.from ?? \"unknown\",\n ...(options.noSync ? [\"--no-sync\"] : []),\n ...(options.force ? [\"--force\"] : []),\n \"--json\",\n ];\n\n const childResult = spawnSync(bosPath, childArgs, {\n cwd: projectDir,\n stdio: [\"inherit\", \"pipe\", \"pipe\"],\n encoding: \"utf-8\",\n env: { ...process.env, BOS_NO_BANNER: \"1\" },\n });\n\n if (childResult.status !== 0) {\n return {\n status: \"error\",\n packages: [\n ...packages,\n ...catalogVersionUpdates.map((u) => ({ name: u.name, from: u.from, to: u.to })),\n ],\n timings,\n error: `Migration phase failed: ${describeMigrationFailure(childResult.stdout, childResult.stderr)}`,\n };\n }\n\n try {\n const childUpgrade: UpgradeResult = JSON.parse(childResult.stdout);\n const mainPkg = packages.find((p) => p.name === \"everything-dev\");\n const changelogUrl =\n mainPkg?.from && mainPkg.from !== mainPkg.to\n ? buildChangelogUrl(mainPkg.from, mainPkg.to, parentSource.repository)\n : undefined;\n return {\n ...childUpgrade,\n packages: [\n ...packages,\n ...catalogVersionUpdates.map((u) => ({ name: u.name, from: u.from, to: u.to })),\n ],\n timings: [...timings, ...(childUpgrade.timings ?? [])],\n changelogUrl,\n };\n } catch {\n return {\n status: \"error\",\n packages: [\n ...packages,\n ...catalogVersionUpdates.map((u) => ({ name: u.name, from: u.from, to: u.to })),\n ],\n timings,\n error: \"Failed to parse migration phase output\",\n };\n }\n }\n\n // No framework version change: run migrations inline (same-package, no re-exec needed)\n // runMigrationPhase handles types gen internally — no separate runTypesGen call needed here.\n // ADR 0032: template sync rides the framework version delta — with no delta the\n // framework-owned files are never reconciled; the release tag they target moves\n // only through this pathway.\n\n const migrationResult = await runMigrationPhase(projectDir, options, parentSource, {\n versionDelta,\n });\n const mainPkg = packages.find((p) => p.name === \"everything-dev\");\n const changelogUrl =\n mainPkg?.from && mainPkg.from !== mainPkg.to\n ? buildChangelogUrl(mainPkg.from, mainPkg.to, parentSource.repository)\n : undefined;\n return {\n ...migrationResult,\n packages: [\n ...packages,\n ...catalogVersionUpdates.map((u) => ({ name: u.name, from: u.from, to: u.to })),\n ],\n timings: [...timings, ...(migrationResult.timings ?? [])],\n changelogUrl,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;AAoCA,MAAM,qBAAqB;AAE3B,eAAsB,gBACpB,YACA,SACwB;CACxB,MAAM,UAAyB,CAAC;CAGhC,IAAI,QAAQ,OAAO,QAAQ,SAAS;CACpC,MAAM,UAAU,KAAK,eAAe,UAAU,GAAG,cAAc;CAC/D,IAAI,CAAC,WAAW,OAAO,GACrB,OAAO;EACL,QAAQ;EACR,UA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