|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +import pascalcase from 'pascalcase'; |
| 5 | +import Helpers from 'handlebars-helpers'; |
| 6 | +import { mkdirpSync } from 'mkdirp'; |
| 7 | +import OpenAPIParser from '@readme/openapi-parser'; |
| 8 | +import type { HelperDelegate } from 'handlebars'; |
| 9 | +import type { NodePlopAPI } from 'plop'; |
| 10 | + |
| 11 | +import { Prompts } from './prompts.js'; |
| 12 | +import { useCliAction } from './actions.js'; |
| 13 | + |
| 14 | +async function writeMissing( |
| 15 | + api: string, |
| 16 | + methods: Record<string, Record<string, { operationId: string; }>>, |
| 17 | + apiName: string) { |
| 18 | + const handlerPath = path.join('src', 'handlers', `${api}.ts`); |
| 19 | + const exists = fs.existsSync(handlerPath); |
| 20 | + if (!exists) { |
| 21 | + console.log('Creating', handlerPath); |
| 22 | + mkdirpSync(path.dirname(handlerPath)); |
| 23 | + fs.writeFileSync( |
| 24 | + handlerPath, |
| 25 | + `import type { ${apiName} } from '@/types'; |
| 26 | +
|
| 27 | +${Object.keys(methods) |
| 28 | + .filter((f) => !['parameters'].includes(f)) |
| 29 | + .map( |
| 30 | + (method) => |
| 31 | + `export const ${method}: ${apiName}['${methods[method].operationId}'] = async (req, res) => { |
| 32 | + // TODO - implement ${method} handler |
| 33 | + res.sendStatus(501); |
| 34 | +}; |
| 35 | +`, |
| 36 | + )}`, |
| 37 | + ); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// eslint-disable-next-line import/no-default-export |
| 42 | +export default function (plop: NodePlopAPI) { |
| 43 | + const helpers = Helpers(); |
| 44 | + Object.entries(helpers).forEach(([name, helper]) => { |
| 45 | + if (!['raw'].includes(name)) { |
| 46 | + plop.setHelper(name, helper); |
| 47 | + } |
| 48 | + }); |
| 49 | + plop.setHelper('ternary', ((test, yes, no) => (test ? yes : no)) as HelperDelegate); |
| 50 | + useCliAction(plop); |
| 51 | + |
| 52 | + // controller generator |
| 53 | + plop.setGenerator('handlers', { |
| 54 | + description: 'Generate missing API handlers', |
| 55 | + prompts: [Prompts.handlerWarning], |
| 56 | + actions() { |
| 57 | + return [ |
| 58 | + async () => { |
| 59 | + const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); |
| 60 | + const svcName = pkg.name.split('/').pop(); |
| 61 | + const apiSpec = `api/${svcName}.yaml`; |
| 62 | + const { paths } = await OpenAPIParser.validate(apiSpec); |
| 63 | + const apiName = `${pascalcase(svcName)}Api`; |
| 64 | + if (paths) { |
| 65 | + Object.entries(paths).forEach(([api, methods]) => writeMissing(api, methods, apiName)); |
| 66 | + } |
| 67 | + return ''; |
| 68 | + }, |
| 69 | + ` |
| 70 | +We have added any missing handler FILES to your project. If new methods |
| 71 | +needed to be added to existing handler files, we did not add those. |
| 72 | +
|
| 73 | +Happy hacking!`, |
| 74 | + ].filter((a) => typeof a === 'object' || typeof a === 'string' || typeof a === 'function'); |
| 75 | + }, |
| 76 | + }); |
| 77 | +} |
0 commit comments