@sap/ams-dev
Version: 
NodesJS AMS development environment
61 lines (53 loc) • 2.06 kB
JavaScript
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const stream = require('stream');
const { promisify } = require('util');
const finished = promisify(stream.pipeline);
async function start() {
	const pkg = JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8', flag: 'r' }));
	const compilerVer = pkg.config.dclCompilerVersion;
	const compilerVerPath = path.join('resources', 'dcl-compiler', 'VERSION');
	if (! fs.existsSync(path.dirname(compilerVerPath))) {
		fs.mkdirSync(path.dirname(compilerVerPath), { recursive: true });
	}
	await updateBinary('dcl-compiler', compilerVerPath, compilerVer);
}
async function updateBinary(name, versionPath, desiredVersion) {
	if (fs.existsSync(versionPath)) {
		const currentVersion = fs.readFileSync(versionPath, { encoding: 'utf8', flag: 'r' });
		if (currentVersion === desiredVersion) {
			console.log(`${name} is up to date`);
			return;
		} else {
			console.log(`${name} is outdated: current: ${currentVersion} desired: ${desiredVersion}`);
		}
	}
	rmDirContent(path.join('resources', name));
	console.log(`downloading new ${name} version ${desiredVersion}`);
	switch (name) {
	case 'dcl-compiler':
		await downloadDclCompiler(desiredVersion);
		break;
	}
	fs.writeFileSync(path.join('resources', name, 'VERSION'), desiredVersion);
}
async function downloadDclCompiler(version) {
	const COMPILER_DOWN_URL = `https://int.repositories.cloud.sap/artifactory/build-snapshots/com/sap/cloud/security/ams/dcl/compiler/${version}/compiler-${version}-full.jar`;
	await downloadFile(COMPILER_DOWN_URL, path.join('resources', 'dcl-compiler', 'dcl.jar'));
}
function rmDirContent(directory) {
	if (fs.existsSync(directory)) fs.rmSync(directory, { recursive: true, force: true });
	fs.mkdirSync(directory);
}
async function downloadFile(url, dest) {
	try {
		const request = await axios.get(url, {
			responseType: 'stream',
		});
		await finished(request.data, fs.createWriteStream(dest));
	} catch (error) {
		throw(`download of ${url} failed ${error}`);
	}
}
start();