@digital-ai/plugin-scaffolder-backend-module-digitalai-xlcli
Version:
xlcli custom action for @backstage/plugin-scaffolder-backend
128 lines (114 loc) • 4.05 kB
JavaScript
;
const XLCLI_VERSION = "24.3.6";
const XLCLI_DIST_URL = "https://dist.xebialabs.com/public/xl-cli";
const SUPPORTED_ARCHITECTURES_BY_PLATFORM = new Map(
Object.entries({
darwin: ["amd64", "arm64"],
linux: ["amd64", "arm64"],
windows: ["amd64"],
})
);
function getArchitecture() {
switch (process.arch) {
case "x64":
return "amd64";
case "arm64":
return "arm64";
default:
return process.arch;
}
}
async function cachingFetchAndVerify(version, platform, arch) {
const path = await import("path");
const fs = await import("fs");
const { default: got } = await import('got');
const {packageConfigSync} = await import("package-config");
const os = await import("os");
const baseUrl = `${XLCLI_DIST_URL}/${version}/${platform}-${arch}/`;
let filename;
switch (platform) {
case "windows":
filename = "xl.exe";
break;
case "darwin":
case "linux":
filename = "xl";
break;
default:
throw new Error("Unsupported platform");
}
const backendDir = path.resolve(process.cwd(), '../../../packages/backend');
const conf = packageConfigSync("xlcli", {
cwd: backendDir,
defaults: {
binaryPath: os.tmpdir(),
},
});
const url = `${baseUrl}${filename}`;
const cacheDir = process.env.BACKSTAGE_XLCLI_PLUGIN_PATH || conf.binaryPath;
const cachedFilePath = path.join(cacheDir, filename);
console.info(`Downloading ${url} to ${cacheDir}`);
fs.writeFileSync(cachedFilePath, await got(url).buffer(), {
flag: fs.constants.O_CREAT | fs.constants.O_TRUNC | fs.constants.O_WRONLY,
});
console.info(`Downloaded ${filename}`);
fs.chmodSync(cachedFilePath, 0o755);
console.log(`xl-cli binary (${version}) available at '${cachedFilePath}'`);
return cachedFilePath;
}
async function download({ version, platform, arch }) {
console.log(`Downloading xl-cli version ${version} for ${platform}-${arch}`);
return await cachingFetchAndVerify(version, platform, arch);
}
async function enforcePlatformAndArch(platform, arch) {
if (!SUPPORTED_ARCHITECTURES_BY_PLATFORM.has(platform)) {
throw new Error(
`No binary available for platform: ${platform}. Supported platforms: ${Array.from(
SUPPORTED_ARCHITECTURES_BY_PLATFORM.keys()
).join(", ")}`
);
}
const archs = SUPPORTED_ARCHITECTURES_BY_PLATFORM.get(platform);
if (!archs?.includes(arch)) {
throw new Error(
`No binary available for platform/arch: ${platform}/${arch}. Supported architectures for ${platform}: ${archs.join(
", "
)}`
);
}
}
async function buildArguments() {
console.log('Current working directory:', process.cwd());
const path = require("path");
const {packageConfigSync} = await import("package-config");
const os = await import("os");
const backendDir = path.resolve(process.cwd(), '../../../packages/backend');
console.log('backendDir directory:', backendDir);
const conf = packageConfigSync("xlcli", {
cwd: backendDir,
defaults: {
version: XLCLI_VERSION,
distUrl: XLCLI_DIST_URL,
},
});
console.log(`env version: ${process.env.BACKSTAGE_XLCLI_PLUGIN_XLCLI_VERSION}`);
console.log(`env platform: ${process.env.BACKSTAGE_XLCLI_PLUGIN_TARGET_OS}`);
console.log(`env arch: ${process.env.BACKSTAGE_XLCLI_PLUGIN_TARGET_ARCH}`);
const version = process.env.BACKSTAGE_XLCLI_PLUGIN_XLCLI_VERSION || conf.version;
const platform = process.env.BACKSTAGE_XLCLI_PLUGIN_TARGET_OS || os.platform();
const arch = process.env.BACKSTAGE_XLCLI_PLUGIN_TARGET_ARCH || getArchitecture();
console.log(`Using version: ${version}`);
console.log(`Using platform: ${platform}`);
console.log(`Using arch: ${arch}`);
return {
version,
platform,
arch,
};
}
module.exports = async function downloadXLCLI () {
const args = await buildArguments();
console.log(`args: ${JSON.stringify(args)}`);
await enforcePlatformAndArch(args.platform, args.arch);
return await download(args);
};