@catladder/pipeline
Version:
Panter workflow for cloud CI/CD and DevOps
69 lines (68 loc) • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.writeBashYamlToFileScript = exports.yamlBashString = void 0;
var yaml_1 = require("yaml");
var BashExpression_1 = require("./BashExpression");
var VariableValueContainingReferences_1 = require("../variables/VariableValueContainingReferences");
var bashExpressionType = {
tag: "",
resolve: function (str) {
// not really needed,but let's make typescript happy
return new BashExpression_1.BashExpression(str);
},
stringify: function (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: function (v) {
return v instanceof BashExpression_1.BashExpression;
}
};
var indentNewlinesInBashExpression = function (expression, indent) {
return expression.transformWithCommand("sed '1!s/^/".concat(indent, "/'"));
};
var variableContainingReferences = {
tag: "",
resolve: function (str) {
// not really needed,but let's make typescript happy
return new VariableValueContainingReferences_1.VariableValueContainingReferences(str);
},
stringify: function (node, ctx) {
var value = node.value;
var stringified = value.parts.map(function (part) {
return part instanceof BashExpression_1.BashExpression ? indentNewlinesInBashExpression(part, ctx.indent) :
// indent every new line
part.toString().replace(/\n/g, "\n".concat(ctx.indent));
}).join("");
return "|-\n" + ctx.indent + stringified;
},
identify: function (v) {
return 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
*/
var yamlBashString = function (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
*/
var writeBashYamlToFileScript = function (value, filename) {
return ["cat > ".concat(filename, " <<EOF\n").concat((0, exports.yamlBashString)(value), "\nEOF\n")];
};
exports.writeBashYamlToFileScript = writeBashYamlToFileScript;