UNPKG

@storm-software/k8s-tools

Version:

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

137 lines (134 loc) 3.49 kB
import { ensureInitialized } from "./chunk-HGXQ6SSU.mjs"; import { AbstractHelmClient } from "./chunk-YXXZO2AJ.mjs"; import { __decorateClass } from "./chunk-KBO64DJX.mjs"; // src/utils/client.ts import { execSync } from "node:child_process"; var HelmClient = class extends AbstractHelmClient { /** * Creates an instance of HelmClient */ constructor() { super(); } package(options) { let chartPath = void 0; let output = {}; try { output = this.runCommand([ "helm", "package", options.chartFolder, "-d", options.outputFolder ]); } catch (err) { if (err?.stderr.length > 0 && err?.exitCode !== 0) { throw new Error(`Failed to package chart: ${err.stderr}`); } } if (output?.stderr.length > 0 && output?.exitCode !== 0) { throw new Error(`Failed to package chart: ${output.stderr}`); } const match = output.stdout?.match( /Successfully packaged chart and saved it to: (.+)/ ); if (!match || match.length < 2) { throw new Error("Failed to parse chart path from helm output"); } chartPath = match[1]?.trim(); return new Promise((resolve) => resolve(chartPath)); } push(options) { try { this.runCommand(["helm", "push", options.chartPath, options.remote]); } catch (err) { if (err?.stderr.length > 0 && err?.exitCode !== 0) { throw new Error(`Failed to push chart: ${err.stderr}`); } } } dependencyUpdate(chartFolder) { try { this.runCommand(["helm", "dependency", "update", chartFolder]); } catch (err) { if (err?.stderr.length > 0 && err?.exitCode !== 0) { throw new Error(`Failed to update chart dependencies: ${err.stderr}`); } } } dependencyBuild(chartFolder) { try { this.runCommand(["helm", "dependency", "build", chartFolder]); } catch (err) { if (err?.stderr.length > 0 && err?.exitCode !== 0) { throw new Error(`Failed to build chart dependencies: ${err.stderr}`); } } } addRepository(name, url) { try { this.runCommand(["helm", "repo", "add", name, url]); } catch (err) { if (err?.stderr.length > 0 && err?.exitCode !== 0) { throw new Error(`Failed to add repository: ${err.stderr}`); } } } /** * Initialize Helm * * @returns A promise */ async initialize() { if (this.initialized) { return; } try { this.runCommand(["helm", "version"]); } catch (err) { if (err?.stderr.length > 0 && err?.exitCode !== 0) { throw new Error(`Helm is not installed: ${err.stderr}`); } } return new Promise((resolve) => { this.initialized = true; resolve(); }); } runCommand(commands) { return execSync(commands.filter(Boolean).join(" "), { encoding: "utf8", windowsHide: true, maxBuffer: 1024 * 1e6, stdio: "pipe" }); } }; __decorateClass([ ensureInitialized ], HelmClient.prototype, "package", 1); __decorateClass([ ensureInitialized ], HelmClient.prototype, "push", 1); __decorateClass([ ensureInitialized ], HelmClient.prototype, "dependencyUpdate", 1); __decorateClass([ ensureInitialized ], HelmClient.prototype, "dependencyBuild", 1); __decorateClass([ ensureInitialized ], HelmClient.prototype, "addRepository", 1); var createHelmClient = () => { return new HelmClient(); }; export { HelmClient, createHelmClient };