@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
67 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.joinBashExpressions = exports.getBashVariable = exports.BashExpression = void 0;
/**
* this class represents a string that can be evaluated in bash scripts.
*
* you can do basic transforms like lowercase, but that does not return you a lowercase string, but instead an experssion represeting a lowercase string
*/
class BashExpression {
constructor(value) {
this.value = value;
}
toJSON() {
return this.toString();
}
toString() {
return this.value.toString();
}
replace(searchValue, replacer) {
return new BashExpression(this.value.toString().replace(searchValue, replacer));
}
/**
*
* @returns a bash expression to lowercase the string
*/
toLowerCase() {
return this.transformWithCommand("awk '{print tolower($0)}'");
}
/**
* concats a value to this one and returns a new BashExpression
* @param value
* @returns
*/
concat(...values) {
return new BashExpression(this.toString().concat(...values.map((v) => v.toString())));
}
transformWithCommand(command) {
return new BashExpression(
// see https://stackoverflow.com/a/2264537
`$(printf %s "${this.toString()}" | ${command})`);
}
}
exports.BashExpression = BashExpression;
const getBashVariable = (name) => new BashExpression(`$${name}`);
exports.getBashVariable = getBashVariable;
/**
* joins bash expressions together with a joiner
* returns a bash expression if any of the parts is a bash expression
* returns a string otherwise
*
* @param parts
* @param joiner
* @returns
*/
const joinBashExpressions = (parts, joiner = "") => {
const anyIsBashExpression = parts.some((p) => p instanceof BashExpression);
if (anyIsBashExpression) {
return new BashExpression(parts
.map((p) => (p instanceof BashExpression ? p.toString() : p))
.join(joiner));
}
else {
return parts.join(joiner);
}
};
exports.joinBashExpressions = joinBashExpressions;
//# sourceMappingURL=BashExpression.js.map