Skip to content

Commit

Permalink
feat: adapt build plugin implementation to new API (#319)
Browse files Browse the repository at this point in the history
Co-authored-by: Lothar Bender <lothar.bender@sap.com>
  • Loading branch information
swaldmann and LotharBender authored Nov 6, 2023
1 parent 95b6ee9 commit aec9966
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
10 changes: 6 additions & 4 deletions postgres/cds-plugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const cds = require('@sap/cds/lib')
const cds = require('@sap/cds')

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.')
}

if (cds.cli.command === 'build') {
module.exports = require('./lib/build')
}
// 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 }
})
38 changes: 8 additions & 30 deletions postgres/lib/build.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
const cds = require('@sap/cds')
const { fs, path } = cds.utils

let BuildPlugin
try {
({ BuildPlugin } = require('@sap/cds-dk/lib/build'))
} catch (e) {
if (e.code === 'ENOTFOUND') throw `No build plugin mechanism for @sap/cds-dk found. Please install @sap/cds-dk for development using 'npm i -D @sap/cds-dk@^7.3.0'`
else throw e
}

const { fs, path, rimraf } = cds.utils
module.exports = class PostgresBuildPlugin extends cds.build.BuildPlugin {

module.exports = class PostgresBuildPlugin extends BuildPlugin {
static hasTask() { // REVISIT: should be unnecessary -> plugin mechanism knows what to pull
return cds.requires.db.kind === 'postgres'
}

static getTaskDefaults() {
return { src: cds.env.folders.db }
}

init() {
this.task.dest = cds.env.build.target === '.' ? path.join('gen','pg') : path.join('gen', 'pg')
}

async clean() {
await rimraf(this.task.dest)
static hasTask() {
return cds.requires.db?.kind === 'postgres'
}

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

const promises = []
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')))
}).to('pg/package.json'))
promises.push(this.write(cds.compile.to.json(model)).to(path.join('pg/db', 'csn.json')))

let data
if (fs.existsSync(path.join(this.task.src, 'data'))) {
Expand All @@ -47,7 +25,7 @@ module.exports = class PostgresBuildPlugin extends BuildPlugin {
data = 'csv'
}
if (data) {
promises.push(this.copy(data).to(path.join('db', 'data')))
promises.push(this.copy(data).to(path.join('pg/db', 'data')))
}
return Promise.all(promises)
}
Expand Down

0 comments on commit aec9966

Please sign in to comment.