From 9a6f4b71b342070768b0ea4289ae82d5c6127842 Mon Sep 17 00:00:00 2001 From: Lothar Bender Date: Thu, 9 Dec 2021 13:04:08 +0100 Subject: [PATCH] adopt latest custom build task changes - single BuildTaskHandler instead of BuildTaskProvider/BuildTaskHandler approach - static build task handler config moved to meta - default build task properties are now returned as name/value pairs using property config - runWith to declare dependencies across build task handlers, ensuring correct dependency graph - optional getTasks method to implement build task specific constraints --- cds-plugin-openapi/index.js | 74 +++++++++++++------------------------ 1 file changed, 25 insertions(+), 49 deletions(-) diff --git a/cds-plugin-openapi/index.js b/cds-plugin-openapi/index.js index 5a81af5a..bdb7c8bf 100644 --- a/cds-plugin-openapi/index.js +++ b/cds-plugin-openapi/index.js @@ -1,53 +1,29 @@ /* eslint-disable require-await */ -const path = require("path") -const fs = require("fs") -const cds = require('@sap/cds'), { BuildTaskProvider, BuildTaskHandler } = cds.build +const cds = require('@sap/cds'), { BuildTaskHandler } = cds.build const cdsdk = require('@sap/cds-dk') -const logger = cds.log("build") -// module.exports.activate = () => { - cds.build.registerProvider( - new (class extends BuildTaskProvider { - get id() { - return 'openapi' - } - get dependents() { - return ['node-cf', 'java-cf'] - } - applyDefaults(task) { - task.src = task.src || cds.env.folders.srv.replace(/\/$/, '') - } - get handler() { - return class extends BuildTaskHandler { - async clean() { - fs.rm(path.join(this.task.dest, "openapi-docs"), { recursive: true, force: true }, (err) => err ? logger.error(err) : '') - } +cds.build.register(class OpenApiHandler extends BuildTaskHandler { + static get meta() { + return { + id: 'openapi', + runWith: ['node-cf', 'java-cf'], + config: { src: cds.env.folders.srv.replace(/\/$/, '') } + } + } + async clean() { + return this.remove('openapi-docs') + } + async build() { + const model = await this.model() + const { options } = this.task - async build() { - const model = await this.model() - const { options } = this.task - - // generate openapi files for all services - await Promise.all(cds.linked(model).services.map(service => { - const openApi = cdsdk.compile.to.openapi(model, { - service: service.name, - 'openapi:diagram': String(options.diagram) === 'true' - }) - this.write(openApi).to(`openapi-docs/${service.name}.openapi3.json`) - })) - } - } - } - /** - * Additional constraints can be defined, e.g. generate openapi service specification in production builds only. - * > cds build --production - * > cds build --for node-cf --production - */ - // async lookupTasks() { - // if (process.env.NODE_ENV === 'production') { - // return [{ for: this.id }] - // } - // } - })() - ) -// } \ No newline at end of file + // generate openapi files for all services + await Promise.all(cds.linked(model).services.map(service => { + const openApi = cdsdk.compile.to.openapi(model, { + service: service.name, + 'openapi:diagram': String(options.diagram) === 'true' + }) + this.write(openApi).to(`openapi-docs/${service.name}.openapi3.json`) + })) + } +}) \ No newline at end of file