everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
120 lines (118 loc) • 5.77 kB
JavaScript
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_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_errors = require('../../utils/errors.cjs');
const require_upgrade_packages = require('./upgrade-packages.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 platform-pin edit on the authored `bos.app.ts` source: rewrites
* only the `platform: "…"` line (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 stampPlatformPin(source, version) {
const platformLineRe = /^([ \t]*)(platform|"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(platformLineRe);
if (match) {
lines[i] = `${match[1]}platform: "${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]}platform: "${version}",`);
return lines.join("\n");
}
}
throw new Error(`could not stamp the platform pin surgically — no platform or extends property found in the authored config; set platform: "${version}" in bos.app.ts manually and re-run`);
}
/**
* Stamp the resolved framework version into the authored config's `platform`
* pin (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. The authored `bos.app.ts` is edited
* surgically (the pin line only) — never re-serialized.
*/
async function applyPlatformPin(projectDir, parentSource) {
const version = parentSource.platform.manifest.frameworkVersion;
const config = await require_load.readAuthoredConfigInput(projectDir);
if (!config) return void 0;
if (config.platform === version) 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, stampPlatformPin(source, version));
return "bos.app.ts";
}
config.platform = 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 platformPinned = await require_timing.timePhase(timings, "stamp platform pin", () => applyPlatformPin(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
}));
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));
const migratedFiles = [...platformPinned ? [platformPinned] : [], ...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,
timings,
changelogUrl
};
}
//#endregion
exports.applyPlatformPin = applyPlatformPin;
exports.describeMigrationFailure = describeMigrationFailure;
exports.runMigrationPhase = runMigrationPhase;
exports.stampPlatformPin = stampPlatformPin;
//# sourceMappingURL=upgrade-migration.cjs.map