UNPKG

@rm3l/plugin-scaffolder-odo-actions

Version:

odo custom actions for Backstage (Backend Plugin)

203 lines (195 loc) 7.84 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node'); var fs = require('fs-extra'); var node_path = require('node:path'); var node_os = require('node:os'); var cachedir = require('cachedir'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs); var cachedir__default = /*#__PURE__*/_interopDefaultLegacy(cachedir); const odoAction = (odoConfig) => { return pluginScaffolderNode.createTemplateAction({ id: "devfile:odo:command", schema: { input: { required: ["command"], type: "object", properties: { command: { type: "string", title: "Command", description: "The odo command to run from the scaffolder workspace" }, args: { type: "array", items: { type: "string" }, title: "Arguments", description: "Arguments to pass to the command" } } } }, async handler(ctx) { var _a, _b; let args = [ctx.input.command]; if ((_a = ctx.input.args) == null ? void 0 : _a.length) { args = [...args, ...ctx.input.args]; } ctx.logger.info(`Workspace: "${ctx.workspacePath}"`); ctx.logger.info(`Running ${args}...`); const telemetryDisabled = (_b = odoConfig == null ? void 0 : odoConfig.getOptionalBoolean("telemetry.disabled")) != null ? _b : false; ctx.logger.info(`...telemetry disabled: ${telemetryDisabled}`); const tmpDir = await fs__default["default"].mkdtemp(node_path.join(node_os.tmpdir(), "odo-")); const odoConfigFilePath = node_path.join(tmpDir, "config"); const envVars = { // Due to a limitation in Node's child_process, the command lookup will be performed using options.env.PATH if options.env is defined. // See https://nodejs.org/docs/latest-v18.x/api/child_process.html#child_process_child_process ...process.env, GLOBALODOCONFIG: odoConfigFilePath, ODO_TRACKING_CONSENT: telemetryDisabled ? "no" : "yes", TELEMETRY_CALLER: "backstage" }; let odoBinaryPath = odoConfig == null ? void 0 : odoConfig.getOptionalString("binaryPath"); if (!odoBinaryPath) { odoBinaryPath = node_path.join(cachedir__default["default"]("odo"), `odo${process.platform === "win32" ? ".exe" : ""}`); if (!fs__default["default"].existsSync(odoBinaryPath)) { ctx.logger.info( `odo binary path not set in app-config.yaml and not found in auto-download path (${odoBinaryPath}) => falling back to "odo" in the system PATH` ); odoBinaryPath = "odo"; } } ctx.logger.info(`odo binary path: ${odoBinaryPath}`); await fs__default["default"].createFile(odoConfigFilePath); await pluginScaffolderNode.executeShellCommand({ command: odoBinaryPath, args, logStream: ctx.logStream, options: { cwd: ctx.workspacePath, env: envVars } }); fs__default["default"].rm(tmpDir, { recursive: true, maxRetries: 2, force: true }, () => { }); ctx.logger.info(`Finished executing odo ${ctx.input.command}`); } }); }; const odoInitAction = (odoConfig) => { return pluginScaffolderNode.createTemplateAction({ id: "devfile:odo:component:init", schema: { input: { required: ["devfile", "version", "name"], type: "object", properties: { devfile: { type: "string", title: "Devfile", description: "The Devfile" }, version: { type: "string", title: "Version", description: "The Devfile Stack version" }, starter_project: { type: "string", title: "Starter Project", description: "The starter project" }, name: { type: "string", title: "Component name", description: "The new component name" } } } }, async handler(ctx) { var _a, _b; ctx.logger.info(`Workspace: "${ctx.workspacePath}"`); ctx.logger.info( `Init "${ctx.input.name}" from: devfile=${ctx.input.devfile}, version=${ctx.input.version}, starterProject=${ctx.input.starter_project}...` ); const telemetryDisabled = (_a = odoConfig == null ? void 0 : odoConfig.getOptionalBoolean("telemetry.disabled")) != null ? _a : false; ctx.logger.info(`...telemetry disabled: ${telemetryDisabled}`); const tmpDir = await fs__default["default"].mkdtemp(node_path.join(node_os.tmpdir(), "odo-init-")); const odoConfigFilePath = node_path.join(tmpDir, "config"); ctx.logger.info(`...temp dir for odo config: ${tmpDir}`); const envVars = { // Due to a limitation in Node's child_process, the command lookup will be performed using options.env.PATH if options.env is defined. // See https://nodejs.org/docs/latest-v18.x/api/child_process.html#child_process_child_process ...process.env, GLOBALODOCONFIG: odoConfigFilePath, ODO_TRACKING_CONSENT: telemetryDisabled ? "no" : "yes", TELEMETRY_CALLER: "backstage" }; const randomRegistryName = "GeneratedRegistryName"; const devfileRegistryUrl = (_b = odoConfig == null ? void 0 : odoConfig.getOptionalString("devfileRegistry.url")) != null ? _b : "https://registry.devfile.io"; ctx.logger.info(`...devfile registry URL: ${devfileRegistryUrl}`); await fs__default["default"].createFile(odoConfigFilePath); let odoBinaryPath = odoConfig == null ? void 0 : odoConfig.getOptionalString("binaryPath"); if (!odoBinaryPath) { odoBinaryPath = node_path.join(cachedir__default["default"]("odo"), `odo${process.platform === "win32" ? ".exe" : ""}`); if (!fs__default["default"].existsSync(odoBinaryPath)) { ctx.logger.info( `odo binary path not set in app-config.yaml and not found in auto-download path (${odoBinaryPath}) => falling back to "odo" in the system PATH` ); odoBinaryPath = "odo"; } } ctx.logger.info(`odo binary path: ${odoBinaryPath}`); await pluginScaffolderNode.executeShellCommand({ command: odoBinaryPath, args: [ "preference", "add", "registry", randomRegistryName, devfileRegistryUrl ], logStream: ctx.logStream, options: { cwd: ctx.workspacePath, env: envVars } }); const initArgs = [ "init", "--name", ctx.input.name, "--devfile-registry", randomRegistryName, "--devfile", ctx.input.devfile, "--devfile-version", ctx.input.version ]; if (ctx.input.starter_project) { initArgs.push("--starter", ctx.input.starter_project); } await pluginScaffolderNode.executeShellCommand({ command: odoBinaryPath, args: initArgs, logStream: ctx.logStream, options: { cwd: ctx.workspacePath, env: envVars } }); fs__default["default"].rm(tmpDir, { recursive: true, maxRetries: 2, force: true }, () => { }); ctx.logger.info( `...Finished creating "${ctx.input.name}" from: devfile=${ctx.input.devfile}, version=${ctx.input.version}, starterProject=${ctx.input.starter_project}` ); } }); }; exports.odoAction = odoAction; exports.odoInitAction = odoInitAction; //# sourceMappingURL=index.cjs.js.map