UNPKG

everything-dev

Version:

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

143 lines (141 loc) • 7.38 kB
const require_load = require("../resolution/load.cjs"); const require_services_resolution_session = require("../resolution/session.cjs"); const require_timing = require("../../utils/timing.cjs"); const require_errors = require("../../utils/errors.cjs"); const require_framework_version = require("../framework-version.cjs"); const require_shared_deps = require("../workspace/shared-deps.cjs"); const require_save_config = require("../../utils/save-config.cjs"); const require_init = require("./init.cjs"); const require_sync = require("./sync.cjs"); const require_upgrade_packages = require("./upgrade-packages.cjs"); const require_upgrade_realign = require("./upgrade-realign.cjs"); const require_upgrade_source = require("./upgrade-source.cjs"); let node_fs = require("node:fs"); let node_path = require("node:path"); //#region src/services/scaffold/upgrade-migration.ts /** * Surgical authored-release edit on the authored config's source: rewrites * only the `release:` line (or a legacy `platform:` line — the codemod form), * or inserts one after `extends`, leaving every other byte — formatting, * comments, attachments — untouched. Throws with the version to set by hand * when no safe anchor exists; the authored file is never re-serialized. */ function stampRelease(source, version) { const releaseLineRe = /^([ \t]*)(release|platform|"release"|"platform")[ \t]*:[ \t]*"([^"]*)"(,?)[ \t]*$/; const extendsLineRe = /^([ \t]*)(extends|"extends")[ \t]*:[ \t]*("[^"]*")(,?)[ \t]*$/; const lines = source.split("\n"); for (let i = 0; i < lines.length; i++) { const match = lines[i].match(releaseLineRe); if (match) { lines[i] = `${match[1]}release: "${version}"${match[4]}`; return lines.join("\n"); } } for (let i = 0; i < lines.length; i++) { const match = lines[i].match(extendsLineRe); if (match) { lines[i] = `${match[1]}${match[2]}: ${match[3]},`; lines.splice(i + 1, 0, `${match[1]}release: "${version}",`); return lines.join("\n"); } } throw new Error(`could not stamp the authored release surgically — no release or extends property found in the authored config; set release: "${version}" in bos.app.ts manually and re-run`); } /** * Stamp the resolved framework version into the authored config's `release` * field (ADR 0034). Runs at the top of the migration phase on both the inline * and re-exec'd paths, so everyday resolution (sync, init, runtime) always * freezes to the release this upgrade just landed from — whether or not the * package versions actually moved. A legacy `platform:` line is the codemod * form: the same stamp rewrites it to `release:`. The authored `bos.app.ts` * is edited surgically (the field line only) — never re-serialized. */ async function applyRelease(projectDir, parentSource) { const version = parentSource.platform.manifest.frameworkVersion; const config = await require_load.readAuthoredConfigInput(projectDir); if (!config) return void 0; if (config.release === version && config.platform === void 0) return void 0; const appConfigPath = (0, node_path.join)(projectDir, "bos.app.ts"); if ((0, node_fs.existsSync)(appConfigPath)) { const source = (0, node_fs.readFileSync)(appConfigPath, "utf-8"); (0, node_fs.writeFileSync)(appConfigPath, stampRelease(source, version)); return "bos.app.ts"; } delete config.platform; config.release = version; await require_save_config.saveBosConfig(projectDir, config); return "bos.config.json"; } /** * The re-exec'd migration child runs `--json`, so its stdout is the structured * result — a failed run carries the real failure in its `error` field, which * raw stderr/stdout concatenation hides behind `[CLI] [object Object]`. */ function describeMigrationFailure(stdout, stderr) { if (stdout) try { const parsed = JSON.parse(stdout); if (parsed && typeof parsed === "object" && parsed.error != null) return require_errors.formatUnknownError(parsed.error); } catch {} const raw = (stderr || stdout || "").trim(); return raw.length > 0 ? raw : "Unknown error"; } async function runMigrationPhase(projectDir, options, parentSource, scope) { const timings = []; const legacyCache = (0, node_path.join)(projectDir, ".bos", "platform-manifest.json"); if ((0, node_fs.existsSync)(legacyCache)) (0, node_fs.rmSync)(legacyCache, { force: true }); const drift = require_upgrade_packages.installDriftError(Object.fromEntries(require_upgrade_packages.FRAMEWORK_PACKAGES.map((name) => [name, require_framework_version.readNodeModulesVersion(projectDir, name)])), Object.fromEntries(require_upgrade_packages.FRAMEWORK_PACKAGES.map((name) => [name, parentSource.catalog[name]]))); if (drift) throw new Error(drift); const releaseStamped = await require_timing.timePhase(timings, "stamp release", () => applyRelease(projectDir, parentSource)); const migratedRootPackageJson = await require_timing.timePhase(timings, "migrate root package", () => require_upgrade_packages.migrateChildRootPackageJson(projectDir)); let syncResult; if (scope.versionDelta && !options.noSync) { syncResult = await require_timing.timePhase(timings, "sync template", () => require_sync.syncTemplate(projectDir, { dryRun: false, noInstall: true, force: options.force, resolvedRelease: parentSource.platform })); if (syncResult?.status === "conflicted") throw new Error(syncResult.error ?? "Template sync hit unresolvable local conflicts"); } await require_timing.timePhase(timings, "sync shared deps", async () => { const configSession = await require_services_resolution_session.openResolution({ cwd: projectDir }); if (!configSession?.config) throw new Error(`${require_load.MISSING_CONFIG_MESSAGE} in current directory`); return require_shared_deps.syncResolvedSharedDeps({ configDir: projectDir, hostMode: "local", bosConfig: configSession.config }); }); if (!options.noInstall) await require_timing.timePhase(timings, "generate types", () => require_init.runTypesGen(projectDir)); let realignment; if (scope.versionDelta && !options.noRealign) realignment = await require_timing.timePhase(timings, "realign owned slots", async () => { const session = await require_services_resolution_session.openResolution({ cwd: projectDir }); if (!session?.config) return void 0; const { keys, lines } = await require_upgrade_realign.detectOwnedDivergentKeys(session); if (keys.length === 0) return { keys: [], realigned: [], failures: [] }; for (const line of lines) if (line.kind === "owned") console.error(` [realign] ${line.message}`); return await require_upgrade_realign.realignOwnedRemotes(projectDir, keys, console.error); }); const migratedFiles = [...releaseStamped ? [releaseStamped] : [], ...migratedRootPackageJson ? ["package.json"] : []]; const mainPkg = parentSource.catalog["everything-dev"]; const changelogUrl = mainPkg ? require_upgrade_source.buildChangelogUrl(void 0, mainPkg, parentSource.repository) : void 0; return { status: "upgraded", packages: [], sync: syncResult, migrated: migratedFiles.length > 0 ? migratedFiles : void 0, ...realignment ? { realignment } : {}, timings, changelogUrl }; } //#endregion exports.applyRelease = applyRelease; exports.describeMigrationFailure = describeMigrationFailure; exports.runMigrationPhase = runMigrationPhase; exports.stampRelease = stampRelease; //# sourceMappingURL=upgrade-migration.cjs.map