UNPKG

@catladder/cli

Version:

Panter cli tool for cloud CI/CD and DevOps

66 lines (65 loc) 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeBashYamlToFileScript = exports.yamlBashString = void 0; const yaml_1 = require("yaml"); const BashExpression_1 = require("./BashExpression"); const VariableValueContainingReferences_1 = require("../variables/VariableValueContainingReferences"); const bashExpressionType = { tag: "", resolve: str => { // not really needed,but let's make typescript happy return new BashExpression_1.BashExpression(str); }, stringify(node, ctx) { // we create a BLOCK_LITERAL // but because bash will interpret the value, it may resolve to a multiline string // so we need to indent every line using sed return "|-\n" + ctx.indent + indentNewlinesInBashExpression(node.value, ctx.indent); }, identify: v => v instanceof BashExpression_1.BashExpression }; const indentNewlinesInBashExpression = (expression, indent) => { return expression.transformWithCommand(`sed '1!s/^/${indent}/'`); }; const variableContainingReferences = { tag: "", resolve: str => { // not really needed,but let's make typescript happy return new VariableValueContainingReferences_1.VariableValueContainingReferences(str); }, stringify(node, ctx) { const value = node.value; const stringified = value.parts.map(part => part instanceof BashExpression_1.BashExpression ? indentNewlinesInBashExpression(part, ctx.indent) : // indent every new line part.toString().replace(/\n/g, `\n${ctx.indent}`)).join(""); return "|-\n" + ctx.indent + stringified; }, identify: v => v instanceof VariableValueContainingReferences_1.VariableValueContainingReferences }; /*** * creates a yaml string that can be used in bash scripts * it handles BashExpressions correctly so that they can be evaluated in bash */ const yamlBashString = value => { return (0, yaml_1.stringify)(value, { defaultStringType: "BLOCK_LITERAL", defaultKeyType: "PLAIN", customTags: [bashExpressionType, variableContainingReferences], aliasDuplicateObjects: false, lineWidth: 0 }); }; exports.yamlBashString = yamlBashString; /** * creates a bash script that writes a yaml string to a file * @param value the yaml value to write, it supports BashExpressions */ const writeBashYamlToFileScript = (value, filename) => { return [`cat > ${filename} <<EOF ${(0, exports.yamlBashString)(value)} EOF `]; }; exports.writeBashYamlToFileScript = writeBashYamlToFileScript;