Skip to content

Commit 53691b1

Browse files
committed
refactor?
1 parent fe4ec24 commit 53691b1

File tree

3 files changed

+145
-80
lines changed

3 files changed

+145
-80
lines changed

dump_javascript_paths.mjs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,3 @@ export async function* GetRecursiveFilesToParse() {
7171
yield* GetRecursiveJavascriptFiles(path);
7272
}
7373
}
74-
75-
async function* GetRecursiveCssFiles(dir) {
76-
const dirents = await readDir(dir, { withFileTypes: true });
77-
for (const dirent of dirents) {
78-
const res = pathResolve(dir, dirent.name);
79-
if (dirent.isDirectory()) {
80-
yield* GetRecursiveCssFiles(res);
81-
} else if (dirent.isFile() && dirent.name.endsWith(".css")) {
82-
yield res;
83-
}
84-
}
85-
}
86-
87-
export async function* GetRecursiveFilesToFix() {
88-
for (const path of pathsToRecurse) {
89-
yield* GetRecursiveCssFiles(path);
90-
}
91-
}

fix_css.mjs

Lines changed: 131 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,141 @@
1-
import { readFile, writeFile } from "node:fs/promises";
2-
import { createWriteStream } from "node:fs";
1+
import { readFile, writeFile, readdir as readDir, rm } from "node:fs/promises";
32
import { parse, latestEcmaVersion } from "espree";
43
import { traverse, Syntax } from "estraverse";
5-
import { GetFilesToParse, GetRecursiveFilesToFix } from "./dump_javascript_paths.mjs";
6-
import { match } from "node:assert";
4+
import { resolve as pathResolve, dirname } from "node:path";
5+
import { fileURLToPath } from "node:url";
76

87
const classRegex = /(\.[a-zA-Z0-9_-]+)/g;
9-
const files = await GetFilesToParse();
108

11-
const classNames = {};
9+
const paths = [
10+
"./Scripts/WebUI/",
11+
"./ClientExtracted/",
12+
"./help.steampowered.com/",
13+
"./partner.steamgames.com/",
14+
"./steamcommunity.com/",
15+
"./store.steampowered.com/",
16+
"./www.dota2.com/",
17+
"./www.underlords.com/",
18+
"./www.counter-strike.net/",
19+
];
1220

13-
for (const file of files) {
14-
try {
21+
async function ParseJsFile(file, classNames) {
22+
if (file.endsWith("licenses.js")) {
23+
return;
24+
}
1525

16-
const code = await readFile(file);
17-
const ast = parse(code, {
18-
ecmaVersion: latestEcmaVersion,
19-
sourceType: "module",
20-
loc: true,
21-
});
22-
23-
traverse(ast, {
24-
enter: (node) => {
25-
// console.log(node);
26-
if (
27-
node.type === Syntax.AssignmentExpression &&
28-
node.left.type === Syntax.MemberExpression &&
29-
node.left.property.type === Syntax.Identifier &&
30-
node.left.property.name === "exports" &&
31-
node.right.type === Syntax.ObjectExpression &&
32-
node.right.properties.constructor === Array
33-
) {
34-
// TODO: store it under a per website value to make sure there's no class colision
35-
for (const array_node of node.right.properties) {
36-
if (!isNaN(array_node.value.value)) {
37-
continue;
38-
}
39-
if (!classNames.hasOwnProperty(array_node.value.value)) {
40-
classNames[array_node.value.value] = [array_node.key.name];
41-
} else
42-
{
43-
if (!classNames[array_node.value.value].includes(array_node.key.name)) {
44-
classNames[array_node.value.value].push(array_node.key.name);
45-
}
46-
}
47-
}
48-
}
49-
},
50-
});
51-
} catch (e) {
52-
console.error(`Unable to parse "${file}":`, e);
53-
}
26+
try {
27+
const code = await readFile(file);
28+
const ast = parse(code, {
29+
ecmaVersion: latestEcmaVersion,
30+
sourceType: "module",
31+
loc: true,
32+
});
33+
34+
traverse(ast, {
35+
enter: (node) => {
36+
// console.log(node);
37+
if (
38+
node.type === Syntax.AssignmentExpression &&
39+
node.left.type === Syntax.MemberExpression &&
40+
node.left.property.type === Syntax.Identifier &&
41+
node.left.property.name === "exports" &&
42+
node.right.type === Syntax.ObjectExpression &&
43+
node.right.properties.constructor === Array
44+
) {
45+
for (const array_node of node.right.properties) {
46+
if (array_node.value.type !== Syntax.Literal || isNumber(String(array_node.value.value), 2)) {
47+
continue;
48+
}
49+
if (!Object.hasOwn(classNames, array_node.value.value)) {
50+
classNames[array_node.value.value] = [array_node.key.name];
51+
} else {
52+
if (!classNames[array_node.value.value].includes(array_node.key.name)) {
53+
classNames[array_node.value.value].push(array_node.key.name);
54+
}
55+
}
56+
}
57+
}
58+
},
59+
});
60+
} catch (e) {
61+
console.error(`Unable to parse "${file}":`, e);
62+
}
63+
}
64+
65+
async function FixCssFile(path, classNames) {
66+
try {
67+
const css = (await readFile(path)).toString();
68+
let output = css;
69+
const matches = [...new Set(css.match(classRegex))];
70+
if (matches) {
71+
for (const match of matches) {
72+
if (Object.hasOwn(classNames, match.slice(1))) {
73+
// replacing by multiple classes creates non valid css but it makes it easy to detect issues
74+
output = output.replaceAll(match, `.${classNames[match.slice(1)].join("|")}`);
75+
}
76+
}
77+
}
78+
const outputPath = path.replace(".css", ".fixed.css");
79+
if (output !== css) {
80+
await writeFile(outputPath, output);
81+
} else {
82+
// best effort to clean our mess if we did some,
83+
// although it might have some problematic effect if the Js parse part need a fix at some point in time
84+
await rm(outputPath, { force: true });
85+
}
86+
} catch (e) {
87+
console.error(`Unable to fix "${path}":`, e);
88+
}
5489
}
5590

56-
for await (const file of GetRecursiveFilesToFix()) {
57-
try {
58-
console.log(file);
59-
let css = (await readFile(file)).toString();
60-
const matches = [...new Set (css.match(classRegex))];
61-
if (matches) {
62-
for (const match of matches) {
63-
if (classNames.hasOwnProperty(match.slice(1))) {
64-
css = css.replaceAll( match, `.${classNames[match.slice(1)].join("|")}`);
65-
}
66-
}
67-
}
68-
await writeFile(file, css);
69-
} catch (e) {
70-
console.error(`Unable to fix "${file}":`, e);
71-
}
91+
async function* GetRecursiveFiles(dir, ext) {
92+
const dirents = await readDir(dir, { withFileTypes: true });
93+
for (const dirent of dirents) {
94+
const res = pathResolve(dir, dirent.name);
95+
if (dirent.isDirectory()) {
96+
yield* GetRecursiveFiles(res, ext);
97+
} else if (dirent.isFile() && dirent.name.endsWith(ext)) {
98+
yield res;
99+
}
100+
}
101+
}
102+
103+
// Why is this not working?
104+
// function isNumber(string, nbrRecursiceSlice = 0) {
105+
// return !Number.isNaN(Number(string)) || (string.length > 0 && nbrRecursiceSlice > 0 ) ? isNumber(string.slice(0,-1), nbrRecursiceSlice - 1) : false;
106+
// }
107+
108+
// best effort to guess if a string is a number (tries to remove the unit at the end)
109+
function isNumber(string, nbrRecursiceSlice) {
110+
while (string.length > 0 && nbrRecursiceSlice > 0) {
111+
if (Number.isNaN(Number(string))) {
112+
string = string.slice(0, -1);
113+
} else {
114+
return true;
115+
}
116+
}
117+
return false;
118+
}
119+
120+
if (process.argv.length > 2) {
121+
const classNames = {};
122+
const __dirname = dirname(fileURLToPath(import.meta.url));
123+
const jsPath = process.argv[2].replace(__dirname, "").split("/")[1];
124+
125+
for await (const file of GetRecursiveFiles(jsPath, ".js")) {
126+
await ParseJsFile(file, classNames);
127+
}
128+
await FixCssFile(process.argv[2], classNames);
129+
} else {
130+
for (const path of paths) {
131+
const classNames = {};
132+
133+
for await (const file of GetRecursiveFiles(path, ".js")) {
134+
await ParseJsFile(file, classNames);
135+
}
136+
137+
for await (const file of GetRecursiveFiles(path, ".css")) {
138+
await FixCssFile(file, classNames);
139+
}
140+
}
72141
}

update.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ private function HandleResponse( string $File, string $Data, string $URL ) : boo
587587

588588
system( 'npm run prettier ' . escapeshellarg( $File ) );
589589

590+
if( str_ends_with( $File, '.css' ) )
591+
{
592+
system( 'node fix_css.mjs ' . escapeshellarg( $File ) );
593+
}
594+
590595
return true;
591596
}
592597

@@ -940,6 +945,15 @@ private function ProcessManifests( array $KnownUrls ) : array
940945
$this->Log( 'Chunk ' . $FilepathOnDisk . ' no longer exists in manifest' );
941946

942947
unlink( $FilepathOnDisk );
948+
949+
if ( str_ends_with( $Filename, '.css'))
950+
{
951+
$FixedFilepathOnDisk = str_replace('.css', '.fixed.css', $FilepathOnDisk);
952+
if ( file_exists( $FixedFilepathOnDisk) ){
953+
unlink( $FixedFilepathOnDisk );
954+
}
955+
956+
}
943957
}
944958
}
945959
}

0 commit comments

Comments
 (0)