UNPKG

k8ts

Version:

Powerful framework for building Kubernetes manifests in TypeScript.

73 lines 2.44 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Manifester = void 0; const emittery_1 = __importDefault(require("emittery")); const lodash_1 = require("lodash"); const version_1 = require("../../version"); const meta_1 = require("./meta"); class Manifester extends emittery_1.default { _options; constructor(_options) { super(); this._options = _options; } _cleanSpecificEmptyObjects(manifest) { const clone = (0, lodash_1.cloneDeepWith)(manifest, (value, key) => { if (key !== "metadata") { return; } for (const k in value) { if (["labels", "annotations"].includes(k)) { if ((0, lodash_1.isEmpty)(value[k])) { (0, lodash_1.unset)(value, k); } } } }); return clone; } _cleanNullishValues(manifest) { const _cleanKeys = (obj) => { if (typeof obj !== "object") { return obj; } for (const [k, v] of Object.entries(obj)) { if (v == null) { delete obj[k]; } } return undefined; }; const clone = (0, lodash_1.cloneDeep)(manifest); return (0, lodash_1.cloneDeepWith)(clone, _cleanKeys); } async _generate(resource) { const manifest = await resource["manifest"](); const noNullish = this._cleanNullishValues(manifest); const noEmpty = this._cleanSpecificEmptyObjects(noNullish); return noEmpty; } _attachProductionAnnotations(resource) { const loc = resource.trace.format({ cwd: this._options.cwd }); resource.meta.add(meta_1.k8ts_namespace, { "^constructed-at": loc, "^produced-by": `k8ts@${version_1.version}` }); } async generate(res) { this._attachProductionAnnotations(res); await this.emit("manifest", { resource: res }); const manifest = await this._generate(res.entity); return { node: res, manifest: manifest }; } } exports.Manifester = Manifester; //# sourceMappingURL=manifester.js.map