-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathvalidate.js
More file actions
96 lines (78 loc) · 2.47 KB
/
Copy pathvalidate.js
File metadata and controls
96 lines (78 loc) · 2.47 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
import fs from "node:fs";
import { pathToFileURL } from "node:url";
import isSvg from "is-svg";
function isValidSVG(filePath) {
try {
const content = fs.readFileSync(filePath, "utf8");
// Check if file is empty
if (!content.trim()) {
return { valid: false, error: "File is empty" };
}
// Use is-svg library for validation
if (!isSvg(content)) {
return { valid: false, error: "Not a valid SVG" };
}
return { valid: true };
} catch (error) {
return { valid: false, error: error.message };
}
}
export function validate() {
const mappingFiles = fs.readdirSync("./mappings");
const svgFiles = fs.readdirSync("./svgs");
const mappingNames = new Set(mappingFiles);
const svgNames = new Set(svgFiles.map((f) => f.replace(".svg", "")));
let hasErrors = false;
// Check for malformed SVGs
const malformedSvgs = [];
for (const svgFile of svgFiles) {
const filePath = `./svgs/${svgFile}`;
const validation = isValidSVG(filePath);
if (!validation.valid) {
malformedSvgs.push({ file: svgFile.replace(".svg", ""), error: validation.error });
}
}
if (malformedSvgs.length > 0) {
console.error("\nError: Invalid SVG files:");
malformedSvgs.forEach(({ file, error }) =>
console.error(` - ${file} - ${error}`)
);
hasErrors = true;
}
// Check for mappings without corresponding SVGs
const missingMappings = [];
for (const mapping of mappingNames) {
if (!svgNames.has(mapping)) {
missingMappings.push(mapping);
}
}
if (missingMappings.length > 0) {
console.error("\nError: Mappings without corresponding SVGs:");
missingMappings.forEach((m) => console.error(` - ${m}`));
hasErrors = true;
}
// Report orphaned SVGs (informational, not an error)
const orphanedSvgs = [];
for (const svg of svgNames) {
if (!mappingNames.has(svg)) {
orphanedSvgs.push(svg);
}
}
if (orphanedSvgs.length > 0) {
console.log("\nInfo: SVGs without mappings (utility icons):");
orphanedSvgs.forEach((s) => console.log(` - ${s}`));
}
// Summary
console.log(`\nValidated ${mappingNames.size} mappings against ${svgNames.size} SVGs`);
if (hasErrors) {
console.error("\nValidation failed\n");
process.exit(1);
} else {
console.log("All mappings have corresponding SVGs\n");
}
return { hasErrors };
}
// only execute if run directly (ESM)
if (import.meta.url === pathToFileURL(process.argv[1]).toString()) {
validate();
}