UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

41 lines (35 loc) 1.59 kB
const child_process = require ('node:child_process') const fs = require ('node:fs') exports.getDataProductCsnViaDpCli = function (ordId) { if (dpCliIsAvailable()) { const [namespace, , name, version] = ordId.split(':') const apiHubName = [namespace, name, version].join('-').replaceAll('.', '-'); const dataProductMetadata = getDataProductMetadata(apiHubName); if (dataProductMetadata.OrdId !== ordId) { throw new Error(`Data Product with OrdId ${ordId} not found in API Hub!`) } return getDataProductCsn(apiHubName); } } function dpCliIsAvailable() { try { const output = child_process.execSync('which dp-cli').toString(); return typeof output === 'string' && output.length > 0; } catch { return false; } } function getDataProductMetadata(apiHubName) { const metadataString = child_process.execSync('dp-cli get -m ' + apiHubName).toString(); return JSON.parse(metadataString); } function getDataProductCsn(apiHubName) { if (fs.existsSync(apiHubName + '.json')) { throw new Error(`Temporary file ${apiHubName}.json already exists! Please remove it before running this command again - or import directly from the file.`) } // TODO: Do not pipe to file but rather save in memory via custom stream? child_process.execSync('dp-cli get ' + apiHubName, { stdio: ['pipe', fs.openSync(apiHubName + '.json', 'w'), 'pipe'] }); const csn = fs.readFileSync(apiHubName + '.json', 'utf-8'); fs.unlinkSync(apiHubName + '.json'); return JSON.parse(csn); }