Repository navigation
Expand file tree
/
Copy pathextract-classes.js
More file actions
112 lines (97 loc) · 3.6 KB
/
Copy pathextract-classes.js
File metadata and controls
112 lines (97 loc) · 3.6 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
const fs = require('fs');
const postcss = require('postcss');
const chokidar = require('chokidar');
const path = require('path');
// Get absolute paths
const pluginsDir = path.resolve(process.cwd(), '..');
const frontCssFile = path.join(process.cwd(), 'assets/css/front.css');
const adminCssFile = path.join(process.cwd(), 'assets/css/admin/admin.css');
const outputFile = path.join(pluginsDir, 'digicommerce-pro', 'classes.txt');
// Pre-process CSS to fix malformed selectors
function fixCssSelectors(css) {
// Simple approach to fix the missing closing parentheses or complete patterns
// Add a closing parenthesis to any :where that's missing it
return css.replace(/:where\(\[dir="(ltr|rtl)"\](?!\))/g, ':where([dir="$1"])');
}
async function extractClassesFromFile(filePath) {
try {
if (!fs.existsSync(filePath)) {
return new Set();
}
// Read the CSS file and fix malformed selectors
let css = fs.readFileSync(filePath, 'utf8');
css = fixCssSelectors(css);
// Process with PostCSS
const result = await postcss().process(css, { from: filePath });
// Extract class names
const classNames = new Set();
result.root.walkRules((rule) => {
if (rule.selector && !rule.selector.startsWith('@')) {
rule.selector.split(',').forEach((selector) => {
selector = selector.trim();
if (selector.startsWith('.')) {
// Get just the class name part (up to a space, [, :, etc.)
const match = selector.match(/\.([\w-\\:]+)/);
if (match && match[0]) {
classNames.add(match[0]);
}
}
});
}
});
return classNames;
} catch (error) {
// If PostCSS fails, try a direct regex approach as fallback
try {
const css = fs.readFileSync(filePath, 'utf8');
const fixedCss = fixCssSelectors(css);
const classRegex = /\.([\w-\\:]+)/g;
const classNames = new Set();
let match;
while ((match = classRegex.exec(fixedCss)) !== null) {
if (match[0].startsWith('.')) {
classNames.add(match[0]);
}
}
return classNames;
} catch (fallbackError) {
return new Set();
}
}
}
async function extractAllClasses() {
try {
// Extract classes from both files
const frontClasses = await extractClassesFromFile(frontCssFile);
const adminClasses = await extractClassesFromFile(adminCssFile);
// Combine all classes
const allClasses = new Set([...frontClasses, ...adminClasses]);
// Sort classes
const sortedClasses = Array.from(allClasses).sort();
// Write to output file
fs.writeFileSync(outputFile, sortedClasses.join('\n'), 'utf8');
} catch (error) {}
}
// Check if running in watch mode
const watchMode = process.argv.includes('--watch');
if (watchMode) {
// Watch mode for both files
chokidar
.watch([frontCssFile, adminCssFile], {
persistent: true,
ignoreInitial: false,
})
.on('all', (event, path) => {
if (event === 'change' || event === 'add') {
extractAllClasses();
}
});
} else {
extractAllClasses()
.then(() => {
process.exit(0);
})
.catch((error) => {
process.exit(1);
});
}