UNPKG

k8ts

Version:

Powerful framework for building Kubernetes manifests in TypeScript.

116 lines 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Summarizer = void 0; const instruments_1 = require("@k8ts/instruments"); const metadata_1 = require("@k8ts/metadata"); const immutable_1 = require("immutable"); const js_yaml_1 = require("js-yaml"); class Summarizer { _options; _post = new instruments_1.TextPostProcessor(); constructor(_options) { this._options = _options; } _formatRefFromTo(node, edge) { return this._token((0, instruments_1.pretty) `${edge}`); } _token(text) { if (typeof text === "object") { text = (0, instruments_1.pretty) `${text}`; } const token = this._post.token(text); return token; } _resource(resource) { const subs = resource.kids .map(sub => { const heading = instruments_1.Displayers.get(sub).pretty("local"); const description = this._resource(sub); return { [this._token(heading)]: description }; }) .toArray() .pull(); const depends = resource.relations .map(x => this._formatRefFromTo(resource, x)) .toArray() .pull(); if (depends.length === 0 && subs.length === 0) { return null; } return [...subs, ...depends]; } _resources(resources) { const resourceContainer = (0, immutable_1.List)(resources) .map(resource => { const text = (0, instruments_1.pretty) `${resource}`; const token = this._token(text); const objForm = this._resource(resource); if (!objForm) { return token; } return { [token]: [...objForm, ...(resource.isRoot ? ["SPACE"] : [])] }; }) .reduce((a, b) => { return [...a, b]; }, []); return resourceContainer; } _yaml(input) { const result = (0, js_yaml_1.dump)(input, { lineWidth: 800, noArrayIndent: true, indent: 2, noRefs: true, replacer(key, value) { if (value instanceof metadata_1.Meta.Meta) { return value.values; } return value; } }); return result; } _serialize(input) { const tree = this._yaml(input); const rendered = this._post.render(tree); return rendered.replaceAll("- SPACE", ""); } _getOptions(result) { const obj = { options: result.options, output: result.files.map(x => { return { path: x.path, name: x.filename, saved: x.bytes, manifested: x.artifacts.length }; }) }; return this._serialize(obj); } result(result) { const outputs = []; if (this._options.printOptions) { outputs.push(this._getOptions(result)); } outputs.push(this.files(result.files.map(x => ({ origin: x.file, resources: x.artifacts.map(x => x.node) })))); return outputs.join("\n"); } files(obj) { let pairs = obj.flatMap(({ origin, resources }) => { return [ [this._token((0, instruments_1.pretty) `\n${origin}`), this._resources(resources)] ]; }); const x = (0, immutable_1.Map)(pairs).toJS(); return this._serialize(x); } } exports.Summarizer = Summarizer; //# sourceMappingURL=summarizer.js.map