Skip to content

Commit

Permalink
deps: use new cds build API @sap/cds-dk >= 7.5.0 (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
LotharBender authored Mar 7, 2024
1 parent 820f971 commit ef22ebe
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
45 changes: 41 additions & 4 deletions postgres/cds-plugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
const cds = require('@sap/cds')
const { fs, path } = cds.utils

if (!cds.env.fiori.lean_draft) {
throw new Error('"@cap-js/postgres" only works if cds.fiori.lean_draft is enabled. Please adapt your configuration.')
}

// requires @sap/cds-dk version >= 7.3.2
cds.build?.register?.('postgres', {
impl: '@cap-js/postgres/lib/build.js',
taskDefaults: { src: cds.env.folders.db }
// requires @sap/cds-dk version >= 7.5.0
cds.build?.register?.('postgres', class PostgresBuildPlugin extends cds.build.Plugin {

static taskDefaults = { src: cds.env.folders.db }

static hasTask() { return cds.requires.db?.kind === 'postgres' }

init() {
// different from the default build output structure
this.task.dest = path.join(cds.root, cds.env.build.target !== '.' ? cds.env.build.target : 'gen', 'pg')
}

async build() {
const model = await this.model()
if (!model) return

const promises = []
if (fs.existsSync(path.join(this.task.src, 'package.json'))) {
promises.push(this.copy(path.join(this.task.src, 'package.json')).to('package.json'))
} else {
promises.push(
this.write({
dependencies: { '@sap/cds': '^7', '@cap-js/postgres': '^1' },
scripts: { start: 'cds-deploy' },
}).to('package.json'),
)
}
promises.push(this.write(cds.compile.to.json(model)).to(path.join('db', 'csn.json')))

let data
if (fs.existsSync(path.join(this.task.src, 'data'))) {
data = 'data'
} else if (fs.existsSync(path.join(this.task.src, 'csv'))) {
data = 'csv'
}
if (data) {
promises.push(this.copy(data).to(path.join('db', 'data')))
}
return Promise.all(promises)
}
})
42 changes: 0 additions & 42 deletions postgres/lib/build.js

This file was deleted.

2 changes: 1 addition & 1 deletion postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"peerDependencies": {
"@sap/cds": ">=7.6",
"@sap/cds-dk": ">=7"
"@sap/cds-dk": ">=7.5"
},
"peerDependenciesMeta": {
"@sap/cds-dk": {
Expand Down

0 comments on commit ef22ebe

Please sign in to comment.