everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
74 lines (72 loc) • 3.29 kB
JavaScript
const require_infra = require('../cli/infra.cjs');
const require_types = require('./types.cjs');
let node_fs = require("node:fs");
let node_path = require("node:path");
let effect = require("effect");
//#region src/infra/materializer.ts
var InfraMaterializer = class extends effect.Context.Service()("InfraMaterializer") {};
function ensureDir(filePath) {
(0, node_fs.mkdirSync)((0, node_path.dirname)(filePath), { recursive: true });
}
function writeIfChanged(filePath, content) {
if ((0, node_fs.existsSync)(filePath) && (0, node_fs.readFileSync)(filePath, "utf-8") === content) return;
ensureDir(filePath);
(0, node_fs.writeFileSync)(filePath, content);
}
function writeContentEffect(filePath, content, phase) {
return effect.Effect.try({
try: () => writeIfChanged(filePath, content),
catch: (cause) => new require_types.InfraError({
phase,
message: `failed to write ${filePath}`,
cause
})
});
}
const makeMaterializer = () => ({
materializeTemplate: (configDir, runtimeConfig) => effect.Effect.gen(function* () {
const groups = require_infra.getSecretGroups(runtimeConfig);
yield* writeContentEffect((0, node_path.join)(configDir, ".env.example"), require_infra.renderEnvFile(groups, { forExample: true }), "materialize-env");
}),
materializeTestInfra: (configDir, runtimeConfig) => effect.Effect.gen(function* () {
const groups = require_infra.getSecretGroups(runtimeConfig);
yield* writeContentEffect((0, node_path.join)(configDir, ".env.test"), require_infra.renderEnvTestFile(groups), "materialize-env");
})
});
const InfraMaterializerLive = effect.Layer.succeed(InfraMaterializer, makeMaterializer());
/**
* Single source of truth for "should we persist port state?".
* Lives next to the only thing that still persists dev ports (the planner).
*/
function shouldPersistPortState() {
return process.env.BOS_NO_PERSIST_PORTS !== "1" && process.env.BOS_TEST !== "1" && process.env.NODE_ENV !== "test";
}
/**
* True when the run is ephemeral (regression, CI, local test).
* In ephemeral mode we must not rewrite `.env.example` because it's a
* committed reference template — every regression run was previously
* baking the resolved regression port into it, polluting the file.
*/
function isEphemeralRun() {
return process.env.BOS_NO_PERSIST_PORTS === "1" || process.env.BOS_TEST === "1" || process.env.NODE_ENV === "test";
}
/**
* Orchestration helper: materializes the env templates in the canonical
* order. docker-compose.yml is a static committed file — nothing here
* generates or rewrites it.
*/
async function materializeViaLayer(configDir, runtimeConfig, options = {}) {
const ephemeral = options.ephemeral ?? isEphemeralRun();
await effect.Effect.runPromise(effect.Effect.gen(function* () {
const m = yield* InfraMaterializer;
if (!ephemeral) yield* m.materializeTemplate(configDir, runtimeConfig);
yield* m.materializeTestInfra(configDir, runtimeConfig);
}).pipe(effect.Effect.provide(InfraMaterializerLive)));
}
//#endregion
exports.InfraMaterializer = InfraMaterializer;
exports.InfraMaterializerLive = InfraMaterializerLive;
exports.isEphemeralRun = isEphemeralRun;
exports.materializeViaLayer = materializeViaLayer;
exports.shouldPersistPortState = shouldPersistPortState;
//# sourceMappingURL=materializer.cjs.map