This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
48 lines (44 loc) · 1.52 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const path = require("path");
const fs = require("fs");
const yaml = require("js-yaml");
module.exports = {
// Function to read and parse YAML config file
readConfig: function readConfig() {
try {
const configPath = path.join(process.cwd(), "input-values.yaml");
const fileContents = fs.readFileSync(configPath, "utf8");
return yaml.load(fileContents);
} catch (error) {
console.error("Error reading config file:", error);
throw error;
}
},
// Function to read template files
readTemplate: function readTemplate(folder, file) {
try {
const templatePath = path.join(process.cwd(), folder, file);
const fileContents = fs.readFileSync(templatePath, "utf8");
return fileContents;
} catch (error) {
console.error("Error reading template file:", error);
throw error;
}
},
// Function to write the YAML output file
writeOutputFile: function writeOutputFile(content, folder, filepath) {
try {
const outputPath = path.join(process.cwd(), folder, filepath);
fs.writeFileSync(outputPath, content, "utf8");
console.log(`${filepath} file generated successfully.`);
} catch (error) {
console.error("Error writing output file:", error);
throw error;
}
},
// Function to replace placeholders in template with config values
replacePlaceholders: function replacePlaceholders(template, config) {
return template.replace(/\$(\w+)/g, (match, p1) => {
return config[p1] !== undefined ? config[p1] : match;
});
}
}