-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcds-plugin.js
48 lines (40 loc) · 1.54 KB
/
cds-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.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': '^8', '@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)
}
})