@sap/ams-dev
Version:
NodesJS AMS development environment
99 lines (88 loc) • 3.07 kB
JavaScript
const fs = require('fs');
const { spawnSync } = require('child_process');
const yargs = require('yargs');
const path = require('path');
const compilerPath = path.join('..', 'resources', 'dcl-compiler', 'dcl.jar');
const jarOption = '--add-opens=java.base/java.lang=ALL-UNNAMED';
function compileDcl(args) {
const options = yargs(args)
.usage('Usage: compile-dcl --dcl [DCL_SRC_ROOT_DIR] --output [DCN_OUTPUT_ROOT_DIR]')
.options({
d: { type: 'string', alias: 'dcl', describe: 'path to DCL root directory', demandOption: true },
o: { type: 'string', alias: 'output', describe: `path to DCN output root directory`, demandOption: true },
l: { type: 'string', alias: 'log-level', describe: 'log level', default: 'error', demandOption: false, choices: ['info', 'error', 'silent'] },
f: { type: 'string', alias: 'failOn', describe: 'fail on error, deprecation or warning', default: 'error', choices: ['error', 'deprecation', 'warning'] }
})
.example('compile-dcl -d src/dcl -o build/dcn', 'compiles DCL in root directory src/dcl to DCN in output root directory build/dcn/')
.parseSync();
const failOn = options.f;
let outputPath = options.o;
let dclPath = options.d;
// adjust the path in case relative paths are used
if (!path.isAbsolute(outputPath)) outputPath = path.join(process.cwd(), outputPath);
if (!path.isAbsolute(dclPath)) dclPath = path.join(process.cwd(), dclPath);
if (!isJavaInstalled(options.l) || !checkDirsExist(dclPath, outputPath, options.l)) {
return 255;
}
const arr = [
jarOption,
'-jar',
compilerPath,
`-out=${outputPath}`,
`-failOn=${failOn}`,
'-dcn=pretty',
dclPath
];
const child = spawnSync(
'java',
arr,
{
cwd: __dirname
}
);
const stdout = child.stdout.toString().replace(/SLF4J.*\n/g, ''); // filter out SLF4J warnings;
const stderr = child.stderr.toString().replace(/SLF4J.*\n/g, ''); // filter out SLF4J warnings;
if (stdout && options.l !== "silent") console.info(stdout);
if (stderr && options.l === "error") console.error(stderr);
if (child.error || child.status != 0) {
return 255;
}
return 0;
}
function checkDirsExist(src, target, logLevel) {
// Check dcl resources dir
try {
fs.accessSync(src);
// eslint-disable-next-line no-unused-vars
} catch (err) {
if (logLevel === "error") {
console.error(`DCL resource dir: ${src} does not exist.`);
}
return false;
}
// Check dcl target dir
try {
fs.accessSync(target);
// eslint-disable-next-line no-unused-vars
} catch (err) {
if (logLevel !== "silent") {
console.info(`compile target dir: ${target} does not exist. creating it.`);
}
fs.mkdirSync(target, { recursive: true });
}
return true;
}
function isJavaInstalled(logLevel) {
const child = spawnSync('java', ['--version']);
if (child.error || child.status != 0) {
if (logLevel === "error") {
console.error('Failed Java version check. Is Java installed?');
}
return false;
}
return true;
}
function getCompilerPath() {
return compilerPath;
}
module.exports = { compileDcl, isJavaInstalled, getCompilerPath };