@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
81 lines (66 loc) • 3.11 kB
JavaScript
const path = require('path');
const { bold } = require('../../util/term');
const HDI_DEPLOY_LIB = '@sap/hdi-deploy';
module.exports = new class HdiDeployUtil {
async deploy(dbDir, vcapEnv = {}, { autoUndeploy, parameter={} } = {}) {
console.log(`deploying to HANA from ${dbDir}`);
const { clean_env, deploy } = this._loadHdiDeployLib(dbDir);
const deployerEnv = JSON.parse(JSON.stringify(process.env));
const options = deployerEnv.HDI_DEPLOY_OPTIONS ? JSON.parse(deployerEnv.HDI_DEPLOY_OPTIONS) : { parameter };
delete options.root;
clean_env?.(deployerEnv);
if (vcapEnv.VCAP_SERVICES) {
deployerEnv.VCAP_SERVICES = JSON.stringify(vcapEnv.VCAP_SERVICES);
}
if (vcapEnv.SERVICE_REPLACEMENTS) {
deployerEnv.SERVICE_REPLACEMENTS = JSON.stringify(vcapEnv.SERVICE_REPLACEMENTS);
}
if (vcapEnv.TARGET_CONTAINER) {
deployerEnv.TARGET_CONTAINER = vcapEnv.TARGET_CONTAINER;
}
if (autoUndeploy) {
console.log(`HDI deployer automatically undeploys deleted resources using --auto-undeploy.`);
options.auto_undeploy = true;
}
try { require.resolve('hdb') } catch (e) {
if (e.code === 'MODULE_NOT_FOUND') options.use_hdb = false
else throw e
}
if (options.use_hdb !== false) options.use_hdb = true
if (options.use_hdb === false) options.use_hdb = undefined
deployerEnv.HDI_DEPLOY_OPTIONS = JSON.stringify(options)
return await new Promise((resolve, reject) => {
deploy(dbDir, deployerEnv, (error, response) => {
if (error) return reject(error)
if (response?.exitCode) {
let message = `HDI deployment failed with exit code ${response.exitCode}.`
if (response.signal) message += `. ${response.signal}`
return reject(new Error(message))
}
return resolve()
}, {
stderrCB: buffer => console.error(buffer.toString()),
stdoutCB: buffer => console.log(buffer.toString())
})
})
}
_loadHdiDeployLib(cwd) {
const searchPaths = [cwd, __dirname];
const libPath = require.resolve(path.join(HDI_DEPLOY_LIB, 'library'), { paths: searchPaths });
if (!libPath) {
throw new Error(`Required library '${HDI_DEPLOY_LIB}' not found in
${searchPaths.join('\n ')}
Add it either to ${bold('devDependencies')} using 'npm install -D ${HDI_DEPLOY_LIB}' or install it globally using 'npm install -g ${HDI_DEPLOY_LIB}'.`);
}
console.log(`HDI deployer path: ${libPath}`)
const libRoot = path.dirname(libPath);
try {
const libPackageJson = require(path.join(libRoot, 'package.json'));
console.log(`HDI deployer version: ${libPackageJson.version}`);
} catch {
// ignore
}
// let any error go through and abort deploy
return require(libPath);
}
}