@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
67 lines • 2.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileWriter = exports.yamlStringifyOptions = void 0;
const promises_1 = require("fs/promises");
const yaml_1 = require("yaml");
exports.yamlStringifyOptions = {
// prevents colapsing long command statements into multiple lines
lineWidth: 0,
// represent multi line commands as single line json strings
doubleQuotedAsJSON: true,
// Better readability when bash commands most often use double quotes
singleQuote: true,
};
class FileWriter {
static create(config) {
return new FileWriter(config);
}
constructor(config) {
this.config = config;
this.config = config;
}
async writeTheFile(path, content) {
var _a, _b;
const transformedContent = await ((_b = (_a = this.config.hooks) === null || _a === void 0 ? void 0 : _a.transformFileBeforeWrite) === null || _b === void 0 ? void 0 : _b.call(_a, {
...(await this.getBaseHookContext(path)),
content,
}));
await (0, promises_1.writeFile)(path, transformedContent !== null && transformedContent !== void 0 ? transformedContent : content, {
encoding: "utf-8",
});
}
async getBaseHookContext(path) {
var _a, _b;
const filename = (_a = path.split("/").pop()) !== null && _a !== void 0 ? _a : "";
return {
filename,
path,
extension: (_b = path.split(".").pop()) !== null && _b !== void 0 ? _b : "",
};
}
async writeGeneratedFile(path, content, options) {
await this.writeTheFile(path, [this.getAutoGeneratedHeader(options.commentChar), content].join("\n"));
}
async writeYamlfile(path, data) {
var _a, _b;
const dataTransformed = await ((_b = (_a = this.config.hooks) === null || _a === void 0 ? void 0 : _a.transformYamlBeforeWrite) === null || _b === void 0 ? void 0 : _b.call(_a, {
...(await this.getBaseHookContext(path)),
data,
}));
return this.writeGeneratedFile(path, (0, yaml_1.stringify)(dataTransformed !== null && dataTransformed !== void 0 ? dataTransformed : data, exports.yamlStringifyOptions), {
commentChar: "#",
});
}
getAutoGeneratedHeader(commentChar) {
return [
"-------------------------------------------------",
`🐱 🔨 This file is generated by catladder`,
`🚨 Do not edit this file manually 🚨`,
"-------------------------------------------------",
]
.map((line) => `${commentChar} ${line}`)
.join("\n")
.concat("\n");
}
}
exports.FileWriter = FileWriter;
//# sourceMappingURL=writeFiles.js.map