Drop live Swagger fetch so clients stay aligned through backend OpenAPI sync PRs.
75 lines
2.2 KiB
JavaScript
75 lines
2.2 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Generate Orval client into api/generated/.
|
|
*
|
|
* OpenAPI source priority:
|
|
* 1. Sibling Nest package (`../ghabilee-backend` or `../backend`) via `generate:openapi`
|
|
* 2. Committed `./openapi.json` (CI, satellite clones, OpenAPI sync PRs)
|
|
*
|
|
* Live Swagger fetch is intentionally not used — clients stay in sync via
|
|
* ghabilee-backend's `sync-openapi-clients` workflow.
|
|
*/
|
|
import { spawnSync } from 'node:child_process'
|
|
import { copyFileSync, existsSync } from 'node:fs'
|
|
import { dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const projectRoot = join(dirname(fileURLToPath(import.meta.url)), '..')
|
|
const localOpenapi = join(projectRoot, 'openapi.json')
|
|
const siblingBackendDirs = [
|
|
join(projectRoot, '../ghabilee-backend'),
|
|
join(projectRoot, '../backend'),
|
|
]
|
|
|
|
function run(command, args, opts = {}) {
|
|
const result = spawnSync(command, args, {
|
|
stdio: 'inherit',
|
|
cwd: projectRoot,
|
|
shell: process.platform === 'win32',
|
|
...opts,
|
|
})
|
|
if (result.status !== 0) {
|
|
process.exit(result.status ?? 1)
|
|
}
|
|
}
|
|
|
|
function trySiblingBackend() {
|
|
for (const backendDir of siblingBackendDirs) {
|
|
if (!existsSync(backendDir) || !existsSync(join(backendDir, 'package.json'))) {
|
|
continue
|
|
}
|
|
|
|
const backendOpenapi = join(backendDir, 'openapi.json')
|
|
console.log(`==> Exporting OpenAPI from sibling backend (${backendDir})...`)
|
|
run('pnpm', ['--dir', backendDir, 'generate:openapi'])
|
|
|
|
if (!existsSync(backendOpenapi)) {
|
|
console.error(`Missing ${backendOpenapi} after export`)
|
|
process.exit(1)
|
|
}
|
|
|
|
copyFileSync(backendOpenapi, localOpenapi)
|
|
console.log('==> Copied sibling openapi.json → ./openapi.json')
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
if (!trySiblingBackend()) {
|
|
if (!existsSync(localOpenapi)) {
|
|
console.error(
|
|
[
|
|
'No ../ghabilee-backend (or ../backend) and no ./openapi.json — cannot generate API client.',
|
|
'Place ghabilee-backend next to this repo, or wait for an OpenAPI sync PR / commit openapi.json.',
|
|
].join('\n'),
|
|
)
|
|
process.exit(1)
|
|
}
|
|
console.log('==> Using committed ./openapi.json')
|
|
}
|
|
|
|
console.log('==> Running orval...')
|
|
run('pnpm', ['exec', 'orval', '--config', 'orval.config.ts'])
|
|
console.log('✓ api/generated updated')
|