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
This commit is contained in:
Lothar Bender
2021-12-09 13:04:08 +01:00
parent c796bda9ad
commit 9a6f4b71b3

View File

@@ -1,28 +1,18 @@
/* eslint-disable require-await */ /* eslint-disable require-await */
const path = require("path") const cds = require('@sap/cds'), { BuildTaskHandler } = cds.build
const fs = require("fs")
const cds = require('@sap/cds'), { BuildTaskProvider, BuildTaskHandler } = cds.build
const cdsdk = require('@sap/cds-dk') const cdsdk = require('@sap/cds-dk')
const logger = cds.log("build")
// module.exports.activate = () => { cds.build.register(class OpenApiHandler extends BuildTaskHandler {
cds.build.registerProvider( static get meta() {
new (class extends BuildTaskProvider { return {
get id() { id: 'openapi',
return 'openapi' runWith: ['node-cf', 'java-cf'],
config: { src: cds.env.folders.srv.replace(/\/$/, '') }
} }
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() { async clean() {
fs.rm(path.join(this.task.dest, "openapi-docs"), { recursive: true, force: true }, (err) => err ? logger.error(err) : '') return this.remove('openapi-docs')
} }
async build() { async build() {
const model = await this.model() const model = await this.model()
const { options } = this.task const { options } = this.task
@@ -36,18 +26,4 @@ const logger = cds.log("build")
this.write(openApi).to(`openapi-docs/${service.name}.openapi3.json`) 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 }]
// }
// }
})()
)
// }