-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_generator.js
More file actions
executable file
·55 lines (43 loc) · 1.49 KB
/
template_generator.js
File metadata and controls
executable file
·55 lines (43 loc) · 1.49 KB
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
49
50
51
52
53
54
55
#!/bin/node
// Script for generating bash script for user interactive menu
// or script generating templates for template engine ;)
import fs from 'fs';
const templates = process.argv.slice(2);
for(let template of templates) {
const td = JSON.parse(fs.readFileSync(template));
var result = `
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
filename=\$(basename "$0")
filename="\${filename%.*}"
clear
echo "\${bold}=================| ${td.title} |=================\${normal}"
`;
const jinjaCr = [];
for(let [groupId, group] of Object.entries(td.groups)) {
result += `
echo
echo "\${bold}${group.title}\${normal}"
`;
for(let [fieldId, field] of Object.entries(group.fields)) {
jinjaCr.push(fieldId);
result += `
echo "${field.text}${field.default != null ? ` [${field.default}]` : ``}:"
read -p "> " ${fieldId}
${fieldId}=\${${fieldId}:-"${field.default != null ? field.default : ``}"}
`;
}
}
result += `
echo
echo Building from $filename.j2 to "$filename.rsc"
jinja2 -o "$filename.rsc" "$filename.j2"
`;
result = result.replaceAll(" ", "");
result = result.substring(1, result.length-2);
for(let jnj of jinjaCr) {
result += ` \\\n -D ${jnj}=$${jnj}`;
}
fs.writeFileSync(template.replace(".json", ".sh"), result);
}