@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
143 lines (127 loc) • 6.2 kB
JavaScript
const { readProject } = require('../../projectReader')
const { merge } = require('../../merge')
const { srv4, appDeployer, appFront, xsuaa } = require('../../registries/mta')
const { odataApi } = require('../../registries/xs-app')
const { renderAndCopy } = require('../../template')
const { sh:$, path:{join}, fs:{promises:{rename}}, write, read, exists } = require('../../../cds').utils
module.exports = class extends require('../../plugin') {
static help() {
return 'SAP BTP Application Frontend service'
}
static hasInProduction(env) {
return !!env.requires['app-front']
}
async run() {
const project = readProject()
const { appUIPaths, apps, appPath, configFile, hasUI5 } = project
await merge(__dirname, 'files/package.json.hbs').into(configFile, { project })
if (hasUI5) {
for (const { app } of apps) {
project.app = app
await merge(__dirname, 'files/ui5.yaml.hbs').into(join(appPath, app, 'ui5.yaml'), { project })
}
}
for (const { app } of apps) {
project.app = app
await merge(__dirname, 'files/app-package.json.hbs').into(join(appPath, app, 'package.json'), { project })
if (hasUI5) {
await renderAndCopy(join(__dirname, 'files/index.html.hbs'), join(appPath, app, 'webapp/index.html'), project)
}
}
// app deployer requires a manifest.json
const manifestRoot = hasUI5 ? 'webapp' : 'public'
await Promise.all(appUIPaths.map(p =>
merge(__dirname, 'files/manifest.json')
.into(join(appPath, p, manifestRoot, 'manifest.json'), { project })
))
// manifest.json IDs must be unique -> otherwise deployment failures on conflicts
await Promise.all(project.appUIPaths.map(async p => {
const manifest = await read(join(appPath, p, manifestRoot, 'manifest.json'))
manifest['sap.app'].id = project.appName + '.' + p
await write(join(appPath, p, manifestRoot, 'manifest.json'), manifest, { spaces: 2 })
}))
}
async combine() {
const project = readProject()
const {
srvPath, appPath, apps,
hasMta, addMta, hasXsuaa, addSample, addApprouter, addWorkzoneStandard, addXsuaa, addIas,
addVue, addReact, addAppFrontend
} = project
await Promise.all(apps.map(async ({ app }) => {
project.app = app
project.hasIndexHtml = exists(join(appPath, app, 'webapp/index.html'))
const additions = [odataApi]
await merge(__dirname, 'files/xs-app.json.hbs').into(join(appPath, app, 'xs-app.json'), { with: project, additions })
}))
// later -> rather consider adding to app-front package.json
// if (hasMultitenancy) {
// await merge({ cds: { requires: { 'app-front':true } } }).into('mtx/sidecar/package.json')
// }
const affected = addXsuaa || addApprouter || addWorkzoneStandard || addSample || addIas
if (addMta || affected && hasMta) {
const srv = srv4(srvPath)
project.apps = apps
const relationships = [{
insert: [appFront, 'name'],
into: [appDeployer, 'requires', 'name'],
}]
const appModules = []
for (const { app } of project.apps) {
const html5App = { in: 'modules', where: { type: 'html5', path: `${appPath}${app}` } }
relationships.push({
insert: [html5App],
into: [appDeployer, 'build-parameters.requires'],
})
}
if (hasXsuaa) {
relationships.push({
insert: [xsuaa, 'name'],
into: [appDeployer, 'requires']
})
}
await merge(__dirname, 'files/mta.yaml.hbs').into('mta.yaml', {
with: project,
additions: [srv, appDeployer, appFront, ...appModules],
relationships
})
}
// Vue + React
const promises = []
const cds = require('../../../cds')
if (addAppFrontend || addVue || addReact) {
for (const { app } of apps) {
const appPath = join(cds.root, cds.env.folders.app, app)
const pJson = await read(join(appPath, 'package.json'))
if (!pJson.devDependencies?.vite) continue
const addVite = cds.cli.argv[0] === 'react' || cds.cli.argv[0] === 'vue'
if (addVite && cds.cli.options.into !== app) continue
// Move xs-app.json and manifest.json to /public to be included in /dist
const fromXsApp = join(appPath, 'xs-app.json')
const toXsApp = join(appPath, 'public/xs-app.json')
if (exists(fromXsApp) && !exists(toXsApp)) {
promises.push(rename(fromXsApp, toXsApp))
}
const fromManifest = join(appPath, 'webapp/manifest.json')
const toManifest = join(appPath, 'public/manifest.json')
if (exists(fromManifest) && !exists(toManifest)) {
promises.push(rename(fromManifest, toManifest))
}
$`npm install --prefix ${appPath} adm-zip --save-dev` // required for zipping /dist in the Vite build step
}
}
await Promise.all(promises)
// When Helm support is added
// if (hasContainerize || hasKyma) {
// const additions = [{
// in: 'modules',
// where: { name: `${project.appName}-content-deployer` }
// }]
// await merge(__dirname, '/files/containerize.yaml.hbs').into('containerize.yaml', { with: project, additions })
// }
// if (hasKyma) {
// await renderAndCopy(join(__dirname, 'files/ui-resources'), 'ui-resources', project)
// await merge(__dirname, 'files/values.yaml.hbs').into('chart/values.yaml', { with: project })
// }
}
}