| 1 |
import * as fs from 'fs' |
| 2 |
|
| 3 |
async function modifyOpenAPIFile(filePath) { |
| 4 |
try { |
| 5 |
const data = await fs.promises.readFile(filePath) |
| 6 |
const openapiContent = JSON.parse(data) |
| 7 |
|
| 8 |
const paths = openapiContent.paths |
| 9 |
for (const pathKey of Object.keys(paths)) { |
| 10 |
const pathData = paths[pathKey] |
| 11 |
for (const method of Object.keys(pathData)) { |
| 12 |
const operation = pathData[method] |
| 13 |
if (operation.tags && operation.tags.length > 0) { |
| 14 |
const tag = operation.tags[0] |
| 15 |
const operationId = operation.operationId |
| 16 |
const toRemove = `${tag}-` |
| 17 |
if (operationId.startsWith(toRemove)) { |
| 18 |
const newOperationId = operationId.substring(toRemove.length) |
| 19 |
operation.operationId = newOperationId |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
await fs.promises.writeFile( |
| 26 |
filePath, |
| 27 |
JSON.stringify(openapiContent, null, 2), |
| 28 |
) |
| 29 |
console.log('File successfully modified') |
| 30 |
} catch (err) { |
| 31 |
console.error('Error:', err) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
const filePath = './openapi.json' |
| 36 |
modifyOpenAPIFile(filePath) |