-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
161 lines (144 loc) · 3.93 KB
/
build.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fs from "node:fs";
import path from "node:path";
import postcss from "postcss";
import postcssStyl from "postcss-styl";
import stylus from "stylus";
const SHARED_DIR = path.join("src", "shared");
const SHARED_PP_DIR = path.join("src", "shared++");
const SHARED_STYLUS_DIR = path.join("src", "shared-styl");
const ERROR_POINTER_STRING = "\n^^^ HERE";
const STEAM_CLASS_MAPS_FILE = "class_map.json";
const readDir = (e) => fs.readdirSync(e).map((p) => path.join(e, p));
const readFile = (e) => fs.readFileSync(e).toString();
const mapText = (text, fn) => text.split("\n").map(fn).join("\n");
const selectorReplacerPlugin = (opts) => (css) => {
css.walkRules((rule) => {
rule.selector = rule.selector.replace(opts.match, opts.replace);
});
};
selectorReplacerPlugin.postcss = true;
const usePostcss = (opts, css) =>
postcss(selectorReplacerPlugin(opts))
.process(css, {
syntax: postcssStyl,
})
.catch((e) => {
if (e.postcssNode) {
console.log(
"%o @ %o is undefined",
e.postcssNode.nodes[0].parent.selector.replace(/\n/g, " "),
path.basename(e.stack.split("\n")[1]),
);
} else {
const source = e.input.source.split("\n");
const line = e.message.split(":")[1];
source[line] =
source[line] === ""
? ERROR_POINTER_STRING
: source[line] + ERROR_POINTER_STRING;
console.log("%s\nInput:\n%s", e.message, source.join("\n"));
}
});
const progs = fs.readdirSync("src").filter((e) => !e.startsWith("shared"));
if (process.argv.length === 2 || !progs.some((e) => process.argv[2] === e)) {
console.error(
"Usage: %s [%s]",
path.basename(process.argv[1]),
progs.join("|"),
);
process.exit(2);
}
const file = process.argv[2];
const rootDir = path.join("src", file);
const outFile = path.join("dist", file, `${file}.css`);
const includedDirsFilter = (() => {
switch (file) {
case "discord":
return (e) => e === "vencord";
case "steam":
return (e) => e !== `${file}.styl` && e !== "imports";
default:
return (e) => fs.lstatSync(path.join(rootDir, e)).isDirectory();
}
})();
const includedDirs = fs
.readdirSync(rootDir)
.filter(includedDirsFilter)
.map((e) => `${rootDir}/${e}/*`);
const processedFiles = (() => {
switch (file) {
case "steam": {
const importsDir = path.join("src", file, "imports");
const map = JSON.parse(readFile(STEAM_CLASS_MAPS_FILE));
return fs.readdirSync(importsDir).map((e) =>
usePostcss(
{
match: /#(\w+)/g,
replace: (_, s) => {
const id = map[e.replace(".styl", "")][s];
if (!id) {
console.log("%o @ %o is undefined", `#${s}`, e);
}
return `.${id}`;
},
},
readFile(path.join(importsDir, e)),
),
);
}
case "discord":
return readDir(rootDir)
.filter(
(e) =>
e !== path.join(rootDir, "vencord") &&
fs.lstatSync(e).isDirectory(),
)
.flatMap((file) => readDir(file))
.map((e) => [file, readFile(e)])
.map((e) =>
usePostcss(
{
match: /\.(\w+)/g,
replace: '[class*="$1_"]',
},
e[1],
),
);
default:
return [];
}
})();
const promises = await Promise.all(processedFiles);
if (promises.some((e) => !e)) {
process.exit(1);
}
const imports = [
[
...readDir(SHARED_STYLUS_DIR),
...readDir(SHARED_PP_DIR),
path.join(rootDir, `${file}.styl`),
...includedDirs,
]
.map((e) => `@import '${e}';`)
.join("\n"),
promises.map((e) => e.css).join("\n"),
].join("\n");
// Variables have to be manually edited due to a Stylus bug (or its age).
const cssVars = readFile(path.join(SHARED_DIR, "variables.css")).replace(
/ \/ /g,
" \\/ ",
);
const actualCssVars = mapText(cssVars, (e) =>
e.endsWith(")") ? `${e} \\` : e,
);
const renderer = stylus(actualCssVars + imports);
renderer.render((err, css) => {
if (err) {
fs.writeFileSync("/tmp/a", actualCssVars + imports);
throw new Error(err.message);
}
fs.writeFileSync(
outFile,
mapText(css, (e) => e.replace(/;$/g, " !important;")),
);
});