-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip-html-extensions.ts
More file actions
executable file
·56 lines (44 loc) · 1.35 KB
/
Copy pathstrip-html-extensions.ts
File metadata and controls
executable file
·56 lines (44 loc) · 1.35 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
import { readdir, readFile, stat, writeFile } from "fs/promises";
import { join } from "path";
async function findHtmlFiles(dir: string) {
const htmlFiles: string[] = [];
async function traverse(currentDir: string) {
const items = await readdir(currentDir);
for (const item of items) {
const fullPath = join(currentDir, item);
const stats = await stat(fullPath);
if (stats.isDirectory()) {
await traverse(fullPath);
} else if (stats.isFile() && item.endsWith(".html")) {
htmlFiles.push(fullPath);
}
}
}
await traverse(dir);
return htmlFiles;
}
async function stripHtmlExtensions(filePath: string) {
let content = await readFile(filePath, "utf-8");
content = content.replace(/\/index\.html/g, "");
content = content.replace(/\.html/g, "");
await writeFile(filePath, content);
}
const distDir = "./dist";
console.log(
"\n[strip-html-extensions] Stripping .html extensions in HTML files...",
);
try {
const htmlFiles = await findHtmlFiles(distDir);
if (htmlFiles.length === 0) {
console.log("[strip-html-extensions] No HTML files found in ./dist");
process.exit(0);
}
console.log(`[strip-html-extensions] Found ${htmlFiles.length} HTML files`);
for (const file of htmlFiles) {
await stripHtmlExtensions(file);
}
console.log("[strip-html-extensions] Done!");
} catch (error) {
console.error(error);
process.exit(1);
}