UNPKG

@storm-software/k8s-tools

Version:

Tools for managing Kubernetes (k8s) infrastructure within a Nx workspace.

203 lines (199 loc) 5.94 kB
import { applyWorkspaceProjectTokens, applyWorkspaceTokens } from "./chunk-C4ZGS3L3.mjs"; import { brandIcon, findWorkspaceRoot, formatLogMessage, getConfig, getStopwatch, writeDebug, writeError, writeFatal, writeInfo, writeSuccess, writeTrace, writeWarning } from "./chunk-TOP3LZ4E.mjs"; import { createHelmClient } from "./chunk-HK6QKQTS.mjs"; // ../workspace-tools/src/base/base-executor.ts import { defu } from "defu"; var withRunExecutor = (name, executorFn, executorOptions = {}) => async (_options, context) => { const stopwatch = getStopwatch(name); let options = _options; let config = {}; try { if (!context.projectsConfigurations?.projects || !context.projectName || !context.projectsConfigurations.projects[context.projectName]) { throw new Error( "The Build process failed because the context is not valid. Please run this command from a workspace." ); } const workspaceRoot = findWorkspaceRoot(); const projectRoot = context.projectsConfigurations.projects[context.projectName].root || workspaceRoot; const sourceRoot = context.projectsConfigurations.projects[context.projectName].sourceRoot || projectRoot || workspaceRoot; const projectName = context.projectName; config.workspaceRoot = workspaceRoot; writeInfo( `${brandIcon(config)} Running the ${name} executor for ${projectName} `, config ); if (!executorOptions.skipReadingConfig) { writeTrace( `Loading the Storm Config from environment variables and storm.config.js file... - workspaceRoot: ${workspaceRoot} - projectRoot: ${projectRoot} - sourceRoot: ${sourceRoot} - projectName: ${projectName} `, config ); config = await getConfig(workspaceRoot); } if (executorOptions?.hooks?.applyDefaultOptions) { writeDebug("Running the applyDefaultOptions hook...", config); options = await Promise.resolve( executorOptions.hooks.applyDefaultOptions(options, config) ); writeDebug("Completed the applyDefaultOptions hook", config); } writeTrace( `Executor schema options \u2699\uFE0F ${formatLogMessage(options)} `, config ); const tokenized = await applyWorkspaceTokens( options, defu( { workspaceRoot, projectRoot, sourceRoot, projectName, config }, config, context.projectsConfigurations.projects[context.projectName] ), applyWorkspaceProjectTokens ); writeTrace( `Executor schema tokenized options \u2699\uFE0F ${formatLogMessage(tokenized)} `, config ); if (executorOptions?.hooks?.preProcess) { writeDebug("Running the preProcess hook...", config); await Promise.resolve( executorOptions.hooks.preProcess(tokenized, config) ); writeDebug("Completed the preProcess hook", config); } const ret = executorFn(tokenized, context, config); if (_isFunction(ret?.next)) { const asyncGen = ret; for await (const iter of asyncGen) { void iter; } } const result = await Promise.resolve( ret ); if (result && (!result.success || result.error && result?.error?.message && typeof result?.error?.message === "string" && result?.error?.name && typeof result?.error?.name === "string")) { throw new Error( `Failure determined while running the ${name} executor ${formatLogMessage( result )}`, { cause: result?.error } ); } if (executorOptions?.hooks?.postProcess) { writeDebug("Running the postProcess hook...", config); await Promise.resolve(executorOptions.hooks.postProcess(config)); writeDebug("Completed the postProcess hook", config); } writeSuccess(`Completed running the ${name} task executor! `, config); return { success: true }; } catch (error) { writeFatal( "A fatal error occurred while running the executor - the process was forced to terminate", config ); writeError( `An exception was thrown in the executor's process - Details: ${error.message} - Stacktrace: ${error.stack}`, config ); return { success: false }; } finally { stopwatch(); } }; var _isFunction = (value) => { try { return value instanceof Function || typeof value === "function" || !!(value?.constructor && value?.call && value?.apply); } catch (e) { return false; } }; // src/executors/helm-package/executor.ts async function serveExecutor(options, context, config) { if (!context?.projectName || !context?.projectsConfigurations?.projects?.[context.projectName]?.root) { throw new Error("Nx executor context was invalid"); } const helm = createHelmClient(); if (options.dependencies?.repositories) { for (const repository of options.dependencies.repositories) { if (repository.name && repository.url) { helm.addRepository(repository.name, repository.url); } else { throw new Error("Repository name and url are required"); } } } if (options.dependencies?.update) { helm.dependencyUpdate(options.chartFolder); } if (options.dependencies?.build) { helm.dependencyBuild(options.chartFolder); } const chartPath = await helm.package({ chartFolder: options.chartFolder, outputFolder: options.outputFolder }); if (options.push && chartPath && options.remote) { helm.push({ chartPath, remote: options.remote }); } else { writeWarning(`Chart packaged at: ${chartPath}`, config); } return { success: true }; } var executor_default = withRunExecutor( "Helm Chart Package executor", serveExecutor, { skipReadingConfig: false, hooks: { applyDefaultOptions: (options) => { options.port ??= 4500; return options; } } } ); export { serveExecutor, executor_default };