d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
import { dirname, resolve } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { compile } from 'json-schema-to-typescript'
|
|
import { formatGeneratedSource } from './format-generated-source'
|
|
|
|
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))
|
|
const ROOT = resolve(SCRIPT_DIR, '..')
|
|
const DEFAULT_CONTRACT_PATH = resolve(
|
|
ROOT,
|
|
'../copilot/copilot/contracts/mothership-stream-v1.schema.json'
|
|
)
|
|
const OUTPUT_PATH = resolve(ROOT, 'apps/sim/lib/copilot/generated/mothership-stream-v1.ts')
|
|
const RUNTIME_SCHEMA_OUTPUT_PATH = resolve(
|
|
ROOT,
|
|
'apps/sim/lib/copilot/generated/mothership-stream-v1-schema.ts'
|
|
)
|
|
|
|
function generateRuntimeConstants(schema: Record<string, unknown>, existingTypes: string): string {
|
|
const defs = (schema.$defs ?? schema.definitions ?? {}) as Record<string, unknown>
|
|
const lines: string[] = []
|
|
|
|
for (const [name, def] of Object.entries(defs)) {
|
|
if (!def || typeof def !== 'object') continue
|
|
const defObj = def as Record<string, unknown>
|
|
const enumValues = defObj.enum
|
|
if (!Array.isArray(enumValues) || enumValues.length === 0) continue
|
|
if (!enumValues.every((v) => typeof v === 'string')) continue
|
|
|
|
const typeAlias = (enumValues as string[]).map((v) => JSON.stringify(v)).join(' | ')
|
|
const entries = (enumValues as string[])
|
|
.map((v) => ` ${JSON.stringify(v)}: ${JSON.stringify(v)}`)
|
|
.join(',\n')
|
|
|
|
if (!existingTypes.includes(`export type ${name} =`)) {
|
|
lines.push(`export type ${name} = ${typeAlias}\n`)
|
|
}
|
|
|
|
lines.push(`export const ${name} = {\n${entries},\n} as const;\n`)
|
|
}
|
|
|
|
return lines.join('\n')
|
|
}
|
|
|
|
function renderRuntimeSchemaModule(schema: unknown): string {
|
|
return [
|
|
'// AUTO-GENERATED FILE. DO NOT EDIT.',
|
|
'// Generated from copilot/contracts/mothership-stream-v1.schema.json',
|
|
'//',
|
|
'',
|
|
'export type JsonSchema = unknown',
|
|
'',
|
|
`export const MOTHERSHIP_STREAM_V1_SCHEMA: JsonSchema = ${JSON.stringify(schema, null, 2)}`,
|
|
'',
|
|
].join('\n')
|
|
}
|
|
|
|
async function main() {
|
|
const checkOnly = process.argv.includes('--check')
|
|
const inputPathArg = process.argv.find((arg) => arg.startsWith('--input='))
|
|
const inputPath = inputPathArg
|
|
? resolve(ROOT, inputPathArg.slice('--input='.length))
|
|
: DEFAULT_CONTRACT_PATH
|
|
|
|
const raw = await readFile(inputPath, 'utf8')
|
|
const schema = JSON.parse(raw)
|
|
const types = await compile(schema, 'MothershipStreamV1EventEnvelope', {
|
|
bannerComment: '// AUTO-GENERATED FILE. DO NOT EDIT.\n//',
|
|
unreachableDefinitions: true,
|
|
additionalProperties: false,
|
|
})
|
|
|
|
const constants = generateRuntimeConstants(schema, types)
|
|
const rendered = formatGeneratedSource(
|
|
constants ? `${types}\n${constants}\n` : types,
|
|
OUTPUT_PATH,
|
|
ROOT
|
|
)
|
|
const renderedSchemaModule = formatGeneratedSource(
|
|
renderRuntimeSchemaModule(schema),
|
|
RUNTIME_SCHEMA_OUTPUT_PATH,
|
|
ROOT
|
|
)
|
|
|
|
if (checkOnly) {
|
|
const existing = await readFile(OUTPUT_PATH, 'utf8').catch(() => null)
|
|
const existingSchemaModule = await readFile(RUNTIME_SCHEMA_OUTPUT_PATH, 'utf8').catch(
|
|
() => null
|
|
)
|
|
if (existing !== rendered || existingSchemaModule !== renderedSchemaModule) {
|
|
throw new Error(
|
|
`Generated mothership stream contract is stale. Run: bun run mship-contracts:generate`
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
await mkdir(dirname(OUTPUT_PATH), { recursive: true })
|
|
await writeFile(OUTPUT_PATH, rendered, 'utf8')
|
|
await writeFile(RUNTIME_SCHEMA_OUTPUT_PATH, renderedSchemaModule, 'utf8')
|
|
}
|
|
|
|
await main()
|