-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathupdate-styles-json.mjs
More file actions
110 lines (96 loc) · 2.82 KB
/
Copy pathupdate-styles-json.mjs
File metadata and controls
110 lines (96 loc) · 2.82 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
import fs from "fs";
import path from "path";
import postcss from "postcss";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.join(__dirname, "websites");
const outputFile = path.join(__dirname, "styles.json");
const mappingFile = path.join(__dirname, "css-mapping.json");
const excludeFile = "userChrome.css";
// Create output file if it doesn't exist
if (!fs.existsSync(outputFile)) {
fs.writeFileSync(outputFile, JSON.stringify({ website: {}, mapping: {} }, null, 2));
}
function extractFeatures(cssContent) {
const root = postcss.parse(cssContent);
const features = {};
let currentFeature = null;
let buffer = [];
root.nodes.forEach((node) => {
if (node.type === "comment") {
if (/^@/.test(node.text.trim())) return;
if (currentFeature && buffer.length > 0) {
features[currentFeature] = buffer
.map((n) => nodeToCleanString(n))
.join("\n")
.trim();
buffer = [];
}
currentFeature = node.text.trim();
} else if (currentFeature) {
buffer.push(node);
}
});
if (currentFeature && buffer.length > 0) {
features[currentFeature] = buffer
.map((n) => nodeToCleanString(n))
.join("\n")
.trim();
}
return features;
}
function nodeToCleanString(node) {
if (node.type === "rule" || node.type === "atrule") {
const clone = node.clone();
if (clone.nodes) {
clone.nodes = clone.nodes.filter((n) => n.type !== "comment");
}
if (clone.selector) {
clone.selector = clone.selector
.replace(/\/\*[^*]*\*\//g, "")
.replace(/\s*,\s*,/g, ",")
.replace(/\s+/g, " ")
.trim();
}
return clone.toString();
}
if (node.type === "comment") {
return "";
}
return node.toString();
}
function updateStylesJson() {
// Read mapping data from css-mapping.json (or use empty object)
let mapping = {};
if (fs.existsSync(mappingFile)) {
try {
mapping = JSON.parse(fs.readFileSync(mappingFile, "utf-8"));
} catch (err) {
console.error("Failed to parse css-mapping.json:", err.message);
mapping = {};
}
}
// Prepare website data
const website = {};
fs.readdirSync(rootDir).forEach((file) => {
if (file.endsWith(".css") && file !== excludeFile) {
const content = fs.readFileSync(path.join(rootDir, file), "utf-8");
try {
const features = extractFeatures(content);
if (Object.keys(features).length > 0) {
website[file] = features;
}
} catch (err) {
console.error(`Failed to parse ${file}:`, err.message);
}
}
});
// Write combined output
const output = {
website,
mapping,
};
fs.writeFileSync(outputFile, JSON.stringify(output, null, 2));
}
updateStylesJson();