-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.js
96 lines (67 loc) · 2.29 KB
/
template.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
/**
* It's node.js helper for template.sh
*/
import path from "path";
import fs from "fs";
import log from "inspc";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const cp = [...process.argv];
const output = cp.pop();
const inpufile = cp.pop();
if (typeof inpufile !== "string") {
throw new Error(`input file not specified`);
}
if (typeof output !== "string") {
throw new Error(`output file not specified`);
}
if (!fs.existsSync(inpufile)) {
// warning: if under dirfile there will be broken link (pointing to something nonexisting) then this function will return false even if link DO EXIST but it's broken
throw new Error(`file '${inpufile}' doesn't exist`);
}
if (fs.existsSync(output)) {
// warning: if under dirfile there will be broken link (pointing to something nonexisting) then this function will return false even if link DO EXIST but it's broken
fs.unlinkSync(output);
}
const content = fs.readFileSync(inpufile, "utf8").toString();
let data = content;
data = data.replace(/<%([a-z]+)\s*(.*?)\s*%>/g, (all, type, file) => {
file = path.resolve(__dirname, file);
if (!fs.existsSync(file)) {
// warning: if under dirfile there will be broken link (pointing to something nonexisting) then this function will return false even if link DO EXIST but it's broken
throw new Error(`file '${file}' doesn't exist`);
}
const data = fs.readFileSync(file, "utf8").toString();
if (type === "url") {
return data.replace(/"/g, "%22");
}
if (type === "inject") {
return data;
}
throw new Error(
`Template file '${inpufile}' contains unsupported <%${type} ... %> placelholder, supported are <%url ... %> and <%inject ... %>`
);
});
fs.writeFileSync(
output,
`<!--
WARNING:
File was created by /bin/bash template.sh template engine.
Edit template instead of this file
WARNING:
WARNING:
File was created by /bin/bash template.sh template engine.
Edit template instead of this file
WARNING:
WARNING:
File was created by /bin/bash template.sh template engine.
Edit template instead of this file
WARNING:
WARNING:
File was created by /bin/bash template.sh template engine.
Edit template instead of this file
WARNING:
-->${data}`
);
console.log(`node: generated "${output}"`);