-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_gluu.js
executable file
·116 lines (105 loc) · 2.97 KB
/
safe_gluu.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
var HTMLParser = require("node-html-parser");
var fs = require("fs");
// variable initialization
const [, , ...args] = process.argv;
var config = {
partialDirectory: "partials",
syntax: "partial",
output: "dist",
entry: "index.html"
}
// function to get values from config file
function readConfigFile() {
if (fs.existsSync('./gluu.config.json')) {
fs.readFile('./gluu.config.json', "utf8", (err, data) => {
if (err) {
console.log(err);
return err;
}
config = { ...JSON.parse(data) }
processFile(config.entry);
})
}
}
// Functions
function initialize() {
if (fs.existsSync("./" + config.output + "/")) {
fs.rmdirSync("./" + config.output + "/", { recursive: true });
}
fs.mkdir("./" + config.output + "/", (err) => {
if (err) {
return console.error(err);
}
});
}
function saveFile(data, fileName,) {
initialize();
var stream = fs.createWriteStream("./" + fileName);
stream.once("open", function (fd) {
var html = "" + data;
stream.end(html);
});
}
function processFile(fileName) {
if (fileName == "") return console.log("Entry point file is required.");
if (fs.existsSync(fileName)) {
fs.readFile(fileName, "utf8", function (err, data) {
if (err) {
return console.log(err);
}
const root = HTMLParser.parse(data);
const partials = root.querySelectorAll(config.syntax);
partials.forEach((partial) => {
const partialName = partial.getAttribute("name");
const newHTML = readPartial(
"./" + config.partialDirectory + "/" + partialName
);
const attrs = partial.rawAttributes;
const keys = Object.keys(attrs);
Promise.resolve(newHTML).then((value) => {
var filteredData = "" + filterPartialHTML(value);
keys.forEach((key, index) => {
var reg = new RegExp("\\{\\b" + key + "\\b\\}");
filteredData = filteredData.replace(reg, attrs[key]);
});
partial.insertAdjacentHTML("afterend", filteredData);
partial.remove();
});
saveFile(root, config.output + "/" + fileName);
});
});
} else {
console.log(`Entry file not found. Project must have a \"${config.entry}\" file`);
}
}
async function readPartial(partialName) {
var html = fs.readFileSync(partialName).toString();
return html;
}
function filterPartialHTML(rawHtml) {
const htmlTree = HTMLParser.parse(rawHtml);
htmlTree.querySelectorAll("ignore").forEach((element) => {
element.remove();
});
return htmlTree;
}
function createConfigFile() {
if (!fs.existsSync('./gluu.config.json')) {
saveFile(JSON.stringify(config), "gluu.config.json")
}
if (!fs.existsSync(config.partialDirectory)) {
fs.mkdir("./" + config.partialDirectory + "/", (err) => {
if (err) {
return console.error(err);
}
});
}
}
if (args.includes('init')) {
createConfigFile()
initialize()
} else {
// execution
readConfigFile()
}