Skip to content

Commit 731f362

Browse files
committed
refactor(scripts): inline SDK generation helper scripts
Consolidate prettify-base-json.mjs and generate-types.mjs into generate-sdk.mjs for simpler maintenance and faster execution. Changes: - Inlined prettifyOpenApiJson() function directly into generate-sdk.mjs - Inlined generateTypes() function with direct openapiTS import - Removed separate script files (prettify-base-json.mjs, generate-types.mjs) - Eliminated process spawning overhead - Simplified function calls with no external script dependencies Benefits: - Faster execution (no process spawning) - Easier to maintain (all SDK generation in one file) - Clearer code flow Savings: -72 lines, -2 files
1 parent 47cc7fe commit 731f362

File tree

3 files changed

+29
-107
lines changed

3 files changed

+29
-107
lines changed

scripts/generate-sdk.mjs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,55 @@
1010
* node scripts/generate-sdk.mjs
1111
*/
1212

13-
import { spawn } from 'node:child_process'
1413
import { readFileSync, writeFileSync } from 'node:fs'
1514
import { resolve } from 'node:path'
1615

1716
import * as parser from '@babel/parser'
1817
import traverse from '@babel/traverse'
1918
import * as t from '@babel/types'
2019
import MagicString from 'magic-string'
20+
import openapiTS from 'openapi-typescript'
2121

2222
import { getDefaultLogger } from '@socketsecurity/lib/logger'
2323

2424
import { getRootPath } from './utils/path-helpers.mjs'
2525
import { runCommand } from './utils/run-command.mjs'
2626

2727
const rootPath = getRootPath(import.meta.url)
28+
const openApiJsonPath = resolve(rootPath, 'openapi.json')
2829
const typesPath = resolve(rootPath, 'types/api.d.ts')
2930

3031
// Initialize logger
3132
const logger = getDefaultLogger()
3233

33-
async function generateTypes() {
34-
return new Promise((resolve, reject) => {
35-
const child = spawn('node', ['scripts/generate-types.mjs'], {
36-
cwd: rootPath,
37-
stdio: ['inherit', 'pipe', 'inherit'],
38-
})
39-
40-
let output = ''
41-
42-
child.stdout.on('data', data => {
43-
output += data.toString()
44-
})
45-
46-
child.on('exit', code => {
47-
if (code !== 0) {
48-
reject(new Error(`Type generation failed with exit code ${code}`))
49-
return
50-
}
34+
/**
35+
* Prettifies the OpenAPI JSON file.
36+
*/
37+
async function prettifyOpenApiJson() {
38+
const openApiData = readFileSync(openApiJsonPath, 'utf8')
39+
writeFileSync(
40+
openApiJsonPath,
41+
JSON.stringify(JSON.parse(openApiData), null, 2),
42+
)
43+
}
5144

52-
try {
53-
writeFileSync(typesPath, output, 'utf8')
54-
// Fix array syntax after writing to disk
55-
fixArraySyntax(typesPath)
56-
// Add SDK v3 method name aliases
57-
addSdkMethodAliases(typesPath)
58-
resolve()
59-
} catch (error) {
60-
reject(error)
45+
/**
46+
* Generates TypeScript types from OpenAPI schema.
47+
*/
48+
async function generateTypes() {
49+
const output = await openapiTS(openApiJsonPath, {
50+
transform(schemaObject) {
51+
if ('format' in schemaObject && schemaObject.format === 'binary') {
52+
return 'never'
6153
}
62-
})
63-
64-
child.on('error', reject)
54+
},
6555
})
56+
57+
writeFileSync(typesPath, output, 'utf8')
58+
// Fix array syntax after writing to disk
59+
fixArraySyntax(typesPath)
60+
// Add SDK v3 method name aliases
61+
addSdkMethodAliases(typesPath)
6662
}
6763

6864
/**
@@ -213,19 +209,15 @@ async function main() {
213209

214210
// Step 1: Prettify OpenAPI JSON
215211
logger.log(' 1. Prettifying OpenAPI JSON...')
216-
let exitCode = await runCommand('node', ['scripts/prettify-base-json.mjs'])
217-
if (exitCode !== 0) {
218-
process.exitCode = exitCode
219-
return
220-
}
212+
await prettifyOpenApiJson()
221213

222214
// Step 2: Generate types
223215
logger.log(' 2. Generating TypeScript types...')
224216
await generateTypes()
225217

226218
// Step 3: Format generated files
227219
logger.log(' 3. Formatting generated files...')
228-
exitCode = await runCommand('pnpm', [
220+
let exitCode = await runCommand('pnpm', [
229221
'exec',
230222
'biome',
231223
'format',

scripts/generate-types.mjs

Lines changed: 0 additions & 37 deletions
This file was deleted.

scripts/prettify-base-json.mjs

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)