-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetchWpGlobalStyles.mjs
49 lines (39 loc) · 1.36 KB
/
fetchWpGlobalStyles.mjs
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
import fs from "fs";
import path from "path";
async function fetchWpGlobalStyles(
endpoint,
outputPath = "src/styles/hwp-global-styles.css",
types = ["variables", "presets", "styles", "base-layout-styles"]
) {
const typeEnums = types.map((type) => type.toUpperCase().replace(/-/g, "_"));
const query = `
query {
globalStylesheet(types: [${typeEnums.join(", ")}])
}
`;
try {
const response = await fetch(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query }),
});
const json = await response.json();
if (!json.data || !json.data.globalStylesheet) {
throw new Error("No stylesheet data returned from the API.");
}
let css = json.data.globalStylesheet;
// Optional minification for production use
css = css
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/\s+/g, " ")
.replace(/\s*([{}:;,])\s*/g, "$1")
.trim();
const absoluteOutputPath = path.resolve(process.cwd(), outputPath);
fs.mkdirSync(path.dirname(absoluteOutputPath), { recursive: true });
fs.writeFileSync(absoluteOutputPath, css);
console.log(`Global styles saved to: ${absoluteOutputPath}`);
} catch (error) {
console.error("Error fetching global styles:", error.message);
}
}
fetchWpGlobalStyles("http://localhost:8888/graphql");