@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
39 lines (34 loc) • 1.76 kB
JavaScript
const { getDataProductCsnFromLocalRepo } = require ('./get-csn.js')
const { getDataProductCsnViaDpCli } = require ('./get-csn-via-dp-cli.js')
const { prepareCsn } = require ('./prepare-csn.js')
const fs = require ('node:fs/promises')
const path = require ('node:path')
exports.load_data_product = async function (sources, o) {
let service = /\/([^/]+)\//.exec(o.services)?.[1].replaceAll('\\.','.')
if (service != 'all') {
// ORD ID has been provided as parameter "-s", valid example: "sap.s4com:apiResource:Product:v1"
if (!service.match(/^([^:]+):apiResource:([^:]+):(v[0-9]+)$/))
throw new Error(`"${service}" is not a valid output port ORD ID`)
}
else {
// try to deduce ORD ID from file name, valid example: "sap-s4com-Product-v1.json"
const m = /^(.+)-([^-]+)-(v[0-9]+)\.(json|csn)$/ .exec (path.basename(String(sources)))
if (m?.length != 5) throw new Error(`Output port ORD ID cannot be deduced from file name`)
const [,namespace,name,version] = m
service = namespace.replace(/-/g,'.') + ':apiResource:' + name + ':' + version
}
const ordId = o.apiResourceOrdId = service
delete o.services // otherwise we minify by ordID, which results in empty csn.definitions
const from = String (sources)
const csn = await getCsn(from, ordId);
const [,namespace, name, version] = /^([^:]+):apiResource:([^:]+):(v[0-9]+)/.exec(ordId)
return prepareCsn (csn, namespace + '.' + name + '.' + version, ordId)
}
async function getCsn(from, ordId) {
if (/dp-metadata$/.test(from))
return getDataProductCsnFromLocalRepo (from, 'eu10', 'stage-canary', ordId)
else if(from === 'dp-cli')
return getDataProductCsnViaDpCli(ordId)
else
return JSON.parse(await fs.readFile(from, { encoding: 'utf-8' }))
}