Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 8b0f601

Browse files
committed
add custom build task contribution sample
1 parent c4529f3 commit 8b0f601

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

bookshop/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"express": "^4.17.1",
99
"passport": "0.4.1"
1010
},
11+
"devDependencies": {
12+
"@cds/cds-plugin-openapi": "*"
13+
},
1114
"scripts": {
1215
"genres": "cds serve test/genres.cds",
1316
"start": "cds run",

cds-plugin-openapi/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable require-await */
2+
const path = require("path")
3+
const fs = require("fs")
4+
const cds = require('@sap/cds'), { BuildTaskProvider, BuildTaskHandler } = cds.build
5+
const cdsdk = require('@sap/cds-dk')
6+
const { path4 } = cds.serve
7+
const logger = cds.log("build")
8+
9+
module.exports.activate = () => {
10+
cds.build.registerProvider(
11+
new (class extends BuildTaskProvider {
12+
get id() {
13+
return 'openapi'
14+
}
15+
get dependents() {
16+
return ['node-cf', 'java-cf']
17+
}
18+
applyTaskDefaults(task) {
19+
task.src = task.src || cds.env.folders.srv.replace(/\/$/, '')
20+
}
21+
async lookupTasks() {
22+
if (process.env.NODE_ENV === 'production') {
23+
return [{ for: this.id }]
24+
}
25+
}
26+
get handler() {
27+
return class extends BuildTaskHandler {
28+
async clean() {
29+
fs.rm(path.join(this.task.dest, "gen/docs"), { recursive: true, force: true }, (err) => logger.error(err))
30+
}
31+
32+
async build() {
33+
const model = await this.model()
34+
const { options } = this.task
35+
36+
// generate openapi files for all services
37+
await Promise.all(cds.linked(model).services.map(service => {
38+
const openApi = cdsdk.compile.to.openapi(model, {
39+
service: service.name,
40+
'openapi:url': path4(service).replace(/\.[^.]+$/, ''),
41+
'openapi:diagram': options.diagram
42+
})
43+
44+
this.write(openApi).to(`gen/docs/${service.name}.openapi3.json`)
45+
}))
46+
}
47+
}
48+
}
49+
})()
50+
)
51+
}

cds-plugin-openapi/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@sap/cds-plugin-openapi",
3+
"version": "1.0.0",
4+
"description": "OpenAPI service specification build plugin",
5+
"main": "index.js",
6+
"dependencies": {
7+
"@sap/cds-dk": "^4"
8+
}
9+
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
"@capire/media": "./media",
1313
"@capire/orders": "./orders",
1414
"@capire/reviews": "./reviews",
15-
"@sap/cds": "^5.1.5"
15+
"@sap/cds": "git+https://github.tools.sap/cap/cds#add/customBuildTaskProviders"
1616
},
1717
"devDependencies": {
1818
"cds-swagger-ui-express": "^0.2.0",
1919
"chai": "^4.2.0",
2020
"chai-as-promised": "^7.1.1",
2121
"chai-subset": "^1.6.0",
22-
"sqlite3": "^5.0.0"
22+
"sqlite3": "^5.0.0",
23+
"@cds/cds-plugin-openapi": "./cds-plugin-openapi"
2324
},
2425
"scripts": {
2526
"cleanup": "rm -rf node_modules && rm -rf */node_modules && rm -rf */*/node_modules",

0 commit comments

Comments
 (0)