-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-content-images.js
More file actions
114 lines (88 loc) · 3.24 KB
/
Copy pathcleanup-content-images.js
File metadata and controls
114 lines (88 loc) · 3.24 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
#!/usr/bin/env node
const fs = require("fs/promises");
const path = require("path");
const ROOT_DIR = process.cwd();
const CONTENT_DIR = path.join(ROOT_DIR, "content");
const IMAGES_DIR = path.join(ROOT_DIR, "public", "images");
const REFERENCED_LIST_PATH = path.join(ROOT_DIR, "referenced-content-images.txt");
const IMAGE_EXT_REGEX = /\.(jpe?g|png|gif|webp|svg|avif|tiff?)$/i;
const CANDIDATE_REGEX =
/[A-Za-z0-9_./%+@:-]+\.(?:jpe?g|png|gif|webp|svg|avif|tiff?)(?:\?[^\s"')\]]+|#[^\s"')\]]+)?/gi;
function hasImageExtension(filePath) {
return IMAGE_EXT_REGEX.test(filePath);
}
async function listFilesRecursive(startDir) {
const files = [];
const stack = [startDir];
while (stack.length > 0) {
const current = stack.pop();
const entries = await fs.readdir(current, { withFileTypes: true });
for (const entry of entries) {
if (entry.name === ".DS_Store") continue;
const fullPath = path.join(current, entry.name);
if (entry.isDirectory()) {
stack.push(fullPath);
} else if (entry.isFile()) {
files.push(fullPath);
}
}
}
return files;
}
function extractReferencedImageBasenames(content) {
const matches = content.match(CANDIDATE_REGEX) || [];
const names = new Set();
for (const match of matches) {
const withoutQueryOrHash = match.split(/[?#]/)[0];
const normalized = withoutQueryOrHash.replace(/\\/g, "/");
const fileName = path.posix.basename(normalized);
if (!fileName || !hasImageExtension(fileName)) continue;
try {
names.add(decodeURIComponent(fileName));
} catch {
names.add(fileName);
}
}
return names;
}
async function main() {
const shouldApply = process.argv.includes("--apply");
const contentFiles = await listFilesRecursive(CONTENT_DIR);
const imageFiles = await listFilesRecursive(IMAGES_DIR);
const referencedBasenames = new Set();
for (const filePath of contentFiles) {
const fileContent = await fs.readFile(filePath, "utf8");
const names = extractReferencedImageBasenames(fileContent);
for (const name of names) referencedBasenames.add(name);
}
const referencedList = Array.from(referencedBasenames).sort((a, b) =>
a.localeCompare(b)
);
await fs.writeFile(REFERENCED_LIST_PATH, `${referencedList.join("\n")}\n`, "utf8");
const existingImageFiles = imageFiles.filter((filePath) =>
hasImageExtension(filePath)
);
const unreferenced = existingImageFiles.filter((filePath) => {
const baseName = path.basename(filePath);
return !referencedBasenames.has(baseName);
});
console.log(`Scanned content files: ${contentFiles.length}`);
console.log(`Referenced image names found: ${referencedBasenames.size}`);
console.log(`Image files in public/images: ${existingImageFiles.length}`);
console.log(`Unreferenced files: ${unreferenced.length}`);
console.log(`Reference list written to: ${path.relative(ROOT_DIR, REFERENCED_LIST_PATH)}`);
if (!shouldApply) {
console.log("\nDry run only. Re-run with --apply to delete unreferenced files.");
return;
}
let deleted = 0;
for (const filePath of unreferenced) {
await fs.unlink(filePath);
deleted += 1;
}
console.log(`Deleted files: ${deleted}`);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});