-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-process.mjs
169 lines (140 loc) · 4.4 KB
/
post-process.mjs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// @ts-check
import 'dotenv/config';
import fs from 'fs/promises';
import path from 'path';
import prettier from 'prettier';
const emailFileExtension = process.env.EMAIL_FILE_EXTENSION;
const compressHTML = process.env.COMPRESS_HTML === 'TRUE' ? true : false;
const disPath = 'dist'; // Change this to your actual directory path
/**
* Modify Html Files
*
* @param {string} location - The path to the directory containing the files.
* @returns {Promise<void>} A Promise that resolves when the renaming is complete.
*/
async function modifyHtmlFiles(location) {
try {
// Read the contents of the specified directory
const files = await fs.readdir(location);
// Iterate through the files
for (const file of files) {
if (file.endsWith('.html')) {
const filepath = path.join(location, file);
// Read the HTML file asynchronously
let data = await fs.readFile(filepath, 'utf-8');
data = data
.replace(/'/g, `'`)
.replace(/"/g, `"`)
.replace(/</g, `<`)
.replace(/>/g, `>`);
if (!compressHTML) {
data = await prettier.format(data, {
parser: 'html',
});
}
await fs.writeFile(filepath, data, 'utf-8');
}
}
console.log('[Post Processing]: Fixing text symbols');
if (!compressHTML) {
console.log('[Post Processing]: Beautify HTML');
}
return Promise.resolve();
} catch (err) {
return Promise.reject(err);
}
}
/**
* Asynchronously renames HTML files to EJS files within a specified location.
*
* @param {string} location - The path to the directory containing the files.
* @param {string} fileExtension - The path to the directory containing the files.
* @returns {Promise<void>} A Promise that resolves when the renaming is complete.
*/
async function renameFiles(location, fileExtension) {
try {
// Read the contents of the specified directory
const files = await fs.readdir(location);
// Iterate through the files
for (const file of files) {
if (file.endsWith('.html')) {
// Construct the old and new file paths
const oldPath = path.join(location, file);
const newPath = path.join(location, file.replace(/\.html$/, `.${fileExtension}`));
// Rename the file
await fs.rename(oldPath, newPath);
}
}
console.log(`[Post Processing]: Rename .html to .${fileExtension}`);
return Promise.resolve();
} catch (err) {
return Promise.reject(err);
}
}
/**
* A class for deleting folders asynchronously.
*/
class FolderDeleter {
/**
* Create a FolderDeleter instance.
* @param {string} folderPath - The path to the folder to be deleted.
*/
constructor(folderPath) {
/**
* The path to the folder to be deleted.
* @type {string}
* @private
*/
this.folderPath = folderPath;
}
/**
* Asynchronously delete the specified folder and its contents.
* @async
* @returns {Promise<void>} A Promise that resolves when the deletion is complete.
*/
async deleteFolder() {
try {
await fs.rm(this.folderPath, { recursive: true });
return Promise.resolve();
} catch (err) {
return Promise.reject(err);
}
}
}
/**
* Asynchronously renames HTML files to EJS files within a specified location.
*
* @param {string} location - The path to the directory containing the files.
* @returns {Promise<void>} A Promise that resolves when the renaming is complete.
*/
async function cleanupUnsuedFiles(location) {
try {
// Read the contents of the specified directory
const files = await fs.readdir(location);
// Delete 'index.html' if it exists
const indexPath = path.join(location, 'index.html');
if (files.includes('index.html')) {
await fs.unlink(indexPath);
}
// Delete _astro folder
const astroFolderPath = path.join(location, '_astro');
const folderDeleter = new FolderDeleter(astroFolderPath);
await folderDeleter.deleteFolder();
console.log('[Post Processing]: Cleanup Unsued Files');
return Promise.resolve();
} catch (err) {
return Promise.reject(err);
}
}
async function main() {
try {
await cleanupUnsuedFiles(disPath);
await modifyHtmlFiles(disPath);
if (emailFileExtension && emailFileExtension !== 'html') {
await renameFiles(disPath, emailFileExtension);
}
} catch (e) {
console.error('Error:', e);
}
}
main();