|
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"; |
3 | 2 | import { parse, latestEcmaVersion } from "espree";
|
4 | 3 | 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"; |
7 | 6 |
|
8 | 7 | const classRegex = /(\.[a-zA-Z0-9_-]+)/g;
|
9 |
| -const files = await GetFilesToParse(); |
10 | 8 |
|
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 | +]; |
12 | 20 |
|
13 |
| -for (const file of files) { |
14 |
| - try { |
| 21 | +async function ParseJsFile(file, classNames) { |
| 22 | + if (file.endsWith("licenses.js")) { |
| 23 | + return; |
| 24 | + } |
15 | 25 |
|
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 | + } |
54 | 89 | }
|
55 | 90 |
|
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 | + } |
72 | 141 | }
|
0 commit comments