-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
137 lines (118 loc) · 3.45 KB
/
Copy pathutils.js
File metadata and controls
137 lines (118 loc) · 3.45 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const fs = require("fs");
const path = require("path");
function load_env() {
try {
// Read the .env file content
const envFilePath = path.resolve(__dirname, ".env");
const envFileContent = fs.readFileSync(envFilePath, { encoding: "utf-8" });
// Split the content into lines
const envVariables = envFileContent.split("\n");
// Iterate over each line
envVariables.forEach((line) => {
// Ignore lines that are empty or start with a hash (#)
if (line && !line.startsWith("#")) {
// Split line by first occurrence of "=" to separate key and value
const [key, ...values] = line.split("=");
const value = values.join("=").trim(); // Re-join in case value contains "="
// Set the environment variable if key is not empty
if (key) {
process.env[key.trim()] = value;
}
}
});
} catch (e) {
console.error("Failed to load .env file", e);
}
}
function truncate_text(text, words) {
return text.split(" ").slice(0, words).join(" ") + "...";
}
function replace_placeholders(template, placeholders) {
return Object.keys(placeholders).reduce((html, key) => {
const placeholder = `{{{${key}}}}`;
return html.replaceAll(placeholder, placeholders[key]);
}, template);
}
async function clean_public() {
try {
const files = await fs.promises.readdir("./public");
for (const file of files) {
const file_path = path.join("./public", file);
const stat = await fs.promises.stat(file_path);
if (stat.isFile() && path.extname(file) === ".html") {
await fs.promises.unlink(file_path);
}
}
} catch (error) {
console.error("Error deleting .html files:", error);
}
}
async function read_file(file_path) {
try {
const content = await fs.promises.readFile(file_path, "utf8");
return content;
} catch (e) {
throw new Error(`Failed to read template: ${e.message}`);
}
}
async function write_file(file_path, content) {
try {
await fs.promises.writeFile(file_path, content);
console.log(`"${file_path}" was created.`);
} catch (e) {
throw new Error(`Failed to write output: ${e.message}`);
}
}
function format_date(str) {
const date = new Date(str);
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const day_of_week = days[date.getDay()];
const month = months[date.getMonth()];
const day_of_month = date.getDate();
const year = date.getFullYear();
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
return `${day_of_week} ${month} ${day_of_month} ${year} at ${hours}:${minutes}`;
}
function slugify(str) {
str = str.replace(/^\s+|\s+$/g, "");
str = str.toLowerCase();
str = str.replace("'", "");
const from =
"ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđ߯a·/_,:;";
const to =
"AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
for (let i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
}
str = str
.replace(/[^a-z0-9 -]/g, "")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "");
return str;
}
module.exports = {
load_env,
clean_public,
truncate_text,
replace_placeholders,
read_file,
write_file,
format_date,
slugify,
};