forked from thesysdev/openui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp-css.js
More file actions
84 lines (70 loc) · 2.67 KB
/
cp-css.js
File metadata and controls
84 lines (70 loc) · 2.67 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
import fs from "fs";
import { camelCase } from "lodash-es";
import path from "path";
const dirname = path.dirname(new URL(import.meta.url).pathname);
// Create directories if they don't exist
function ensureDirectoryExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
// Replace .scss imports with .css imports in compiled JS files
function fixScssImportsInJs(dir) {
const entries = fs.readdirSync(dir);
entries.forEach((entry) => {
const fullPath = path.join(dir, entry);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
fixScssImportsInJs(fullPath);
} else if (entry.endsWith(".js")) {
const content = fs.readFileSync(fullPath, "utf8");
const fixed = content.replace(/(['"])([^'"]*\.scss)\1/g, (match, quote, p) => {
return `${quote}${p.replace(/\.scss$/, ".css")}${quote}`;
});
if (fixed !== content) {
fs.writeFileSync(fullPath, fixed, "utf8");
}
}
});
}
// Copy CSS files from src to dist
function copyCssFiles() {
const srcDir = path.join(dirname, "dist", "components");
const distDir = path.join(dirname, "dist", "styles");
// Ensure the dist/styles directory exists
ensureDirectoryExists(distDir);
// Read all component directories
const components = fs.readdirSync(srcDir);
components.forEach((component) => {
const componentSrcPath = path.join(srcDir, component);
const componentStylesheetName = `${camelCase(component)}.css`;
// Skip if not a directory
if (!fs.statSync(componentSrcPath).isDirectory()) {
return;
}
const stylePath = path.join(componentSrcPath, componentStylesheetName);
const distFile = path.join(distDir, componentStylesheetName);
if (fs.existsSync(stylePath)) {
fs.copyFileSync(stylePath, distFile);
} else {
console.warn(`No stylesheet found for ${component}`);
}
});
const indexCSSContent = fs.readFileSync(path.join(srcDir, "index.css"), "utf8");
fs.writeFileSync(path.join(distDir, "index.css"), indexCSSContent);
const cssUtilsSrc = fs.readFileSync(path.join(dirname, "src", "cssUtils.scss"), "utf8");
fs.writeFileSync(path.join(distDir, "cssUtils.scss"), cssUtilsSrc);
const defaultsCssPath = path.join(dirname, "dist", "openui-defaults.css");
if (fs.existsSync(defaultsCssPath)) {
fs.copyFileSync(defaultsCssPath, path.join(distDir, "openui-defaults.css"));
}
// Fix .scss imports in compiled JS to point to .css files instead
fixScssImportsInJs(path.join(dirname, "dist"));
}
try {
copyCssFiles();
console.log("CSS files copied successfully!");
} catch (error) {
console.error("Error copying CSS files:", error);
process.exit(1);
}