/** * The env template of the release being installed (crosscode.env-template/1, * apps//env.template.json at its tag), read from git like the release notes. The Hub * refuses to make a job when a required server or defaults value is missing (AGENT-1.0-PLAN ยง5). * Releases from before templates (events@2.2.0) use the copy kept here, the same one the * crosscode script falls back to. */ const { agent } = require('@crosscode/contracts'); const { getSettings, releaseRepoToken } = require('./settings'); const { PLATFORM_REPO, parseRepoUrl, gitGet } = require('./releaseSync'); const FALLBACK = { events: require('./envTemplates/events.json'), accounts: require('./envTemplates/accounts.json') }; const cache = new Map(); async function envTemplate(product, version) { const key = `${product}@${version}`; if (cache.has(key)) return cache.get(key); let template = null; try { const repo = parseRepoUrl(PLATFORM_REPO); const token = await releaseRepoToken().catch(() => ''); const text = await gitGet(`${repo.api}/raw/${encodeURIComponent(`apps/${product}`)}/env.template.json?ref=${encodeURIComponent(key)}`, token, { raw: true }); if (text) { const parsed = agent.envTemplateSchema.safeParse(JSON.parse(text)); if (parsed.success) template = parsed.data; } } catch (e) { console.warn(`[env template] ${key}: ${e?.message || e}`); } template = template || (FALLBACK[product] ? agent.envTemplateSchema.parse(FALLBACK[product]) : null); if (template) cache.set(key, template); return template; } /** What's missing for a release on a server: { server: [...], defaults: [...] }. */ async function missingFor(product, version, serverValues, defaults) { const template = await envTemplate(product, version); if (!template) return { server: [], defaults: [] }; return { server: agent.missingRequired(template, 'server', serverValues), defaults: agent.missingRequired(template, 'defaults', defaults) }; } /** * Values the apps can't start with, as sentences (e.g. TRUST_PROXY=TRUE, which crashed the Events * backend after an install's build). Saving a configuration checks them too, but configurations * saved before Hub 2.3.3 weren't checked. */ function invalidFor(serverValues, defaults) { return [...Object.values(agent.invalidValues(serverValues || {})), ...Object.values(agent.invalidValues(defaults || {}))]; } module.exports = { envTemplate, missingFor, invalidFor, _cache: cache, getSettings };