-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamespace-tailwind-classes.cjs
66 lines (57 loc) · 2 KB
/
namespace-tailwind-classes.cjs
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
/**
* This script is used to replace class names in JSX files with Tailwind CSS namespaced class names.
* It'll get you part of the way there, but since it only catches neatly formatted "className=" attributes,
* you'll still need to manually replace any class names that are used in other ways (e.g. as variables).
*/
const fs = require("fs");
const path = require("path");
// Regex pattern to match class names within className attribute
const regex =
/(?<=className=".*)(?!\bxs:)(?!\bsm:)(?!\bmd:)(?!\blg:)(?!\bxl:)([\[\]\/#a-zA-Z0-9_-]+)(?!:)(?=.*")/g;
// Function to recursively process files in a directory
function processFiles(directory) {
fs.readdir(directory, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
}
files.forEach((file) => {
const filePath = path.join(directory, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error("Error getting file stats:", err);
return;
}
if (stats.isDirectory()) {
// If directory, recursively process its files
processFiles(filePath);
} else if (filePath.endsWith(".jsx")) {
// If file is a .jsx file, process it
processJSXFile(filePath);
}
});
});
});
}
// Function to process a single .jsx file
function processJSXFile(filePath) {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
// Apply regex replacement
const modifiedData = data.replace(regex, "gw-$1");
// Write modified data back to the file
fs.writeFile(filePath, modifiedData, "utf8", (err) => {
if (err) {
console.error("Error writing file:", err);
return;
}
console.log(`File ${filePath} processed successfully.`);
});
});
}
// Start processing files in the specified directory
const directoryPath = "./src"; // Replace this with your directory path
processFiles(directoryPath);