80 lines
2.3 KiB
JavaScript
80 lines
2.3 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { execFile as execFileCallback } from 'node:child_process'
|
|
import fs from 'node:fs/promises'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import { promisify } from 'node:util'
|
|
import test from 'node:test'
|
|
|
|
const execFile = promisify(execFileCallback)
|
|
|
|
const createTempDir = async () => {
|
|
return fs.mkdtemp(path.join(os.tmpdir(), 'yx-generate-api-cli-'))
|
|
}
|
|
|
|
test('cli gen supports spaced flags and syncs the external index file', async () => {
|
|
const tempDir = await createTempDir()
|
|
|
|
try {
|
|
const swaggerPath = path.join(tempDir, 'swagger.json')
|
|
const configPath = path.join(tempDir, 'yx-generate-api.config.mjs')
|
|
const outputDir = path.join(tempDir, 'generated')
|
|
const externalIndexFile = path.join(tempDir, 'api-index.js')
|
|
const cliPath = path.resolve('bin/yx-generate-api.js')
|
|
|
|
await fs.writeFile(
|
|
swaggerPath,
|
|
JSON.stringify(
|
|
{
|
|
openapi: '3.0.0',
|
|
paths: {
|
|
'/api/v1/Alpha/GetThing': {
|
|
get: {
|
|
tags: ['Alpha'],
|
|
summary: 'Get alpha thing',
|
|
responses: {
|
|
200: { description: 'OK' },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
'utf8',
|
|
)
|
|
|
|
await fs.writeFile(
|
|
configPath,
|
|
`export default {
|
|
swaggerUrl: './swagger.json',
|
|
swaggerTimeoutMs: 1000,
|
|
outputDir: './generated',
|
|
externalIndexFile: './api-index.js',
|
|
requestImport: '../request',
|
|
cleanOutput: true,
|
|
sync: {
|
|
enabled: true,
|
|
},
|
|
}
|
|
`,
|
|
'utf8',
|
|
)
|
|
|
|
await execFile(process.execPath, [cliPath, 'gen', '--config', configPath, '--timeout', '1000'], {
|
|
cwd: tempDir,
|
|
})
|
|
|
|
const generatedIndexContent = await fs.readFile(path.join(outputDir, 'index.js'), 'utf8')
|
|
const externalIndexContent = await fs.readFile(externalIndexFile, 'utf8')
|
|
|
|
assert.match(generatedIndexContent, /export \* as alphaApi from ["']\.\/alpha["']/)
|
|
assert.match(generatedIndexContent, /export \{ getThingApi \} from ["']\.\/alpha["']/)
|
|
assert.match(externalIndexContent, /AUTO-GENERATED API EXPORTS START/)
|
|
assert.match(externalIndexContent, /export \* from ["']\.\/generated["']/)
|
|
} finally {
|
|
await fs.rm(tempDir, { recursive: true, force: true })
|
|
}
|
|
})
|