|
| 1 | +import { readFile, writeFile, readdir as readDir, rm, mkdir } from "node:fs/promises"; |
| 2 | +import { parse, latestEcmaVersion } from "espree"; |
| 3 | +import { traverse, Syntax } from "estraverse"; |
| 4 | +import { resolve as pathResolve, basename, dirname, join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const CLASS_SELECTOR_REGEX = /(\.[a-zA-Z0-9_-]+)/g; |
| 8 | + |
| 9 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | + |
| 11 | + |
| 12 | +async function ParseJsFile(file, classNames) { |
| 13 | + //TODO remove /c. once it's not needed |
| 14 | + if (file.endsWith("licenses.js") || file.includes("/c.")) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + try { |
| 19 | + const code = await readFile(file); |
| 20 | + const ast = parse(code, { |
| 21 | + ecmaVersion: latestEcmaVersion, |
| 22 | + sourceType: "module", |
| 23 | + loc: true, |
| 24 | + }); |
| 25 | + |
| 26 | + traverse(ast, { |
| 27 | + enter: (node) => { |
| 28 | + if ( |
| 29 | + node.type === Syntax.AssignmentExpression && |
| 30 | + node.left.type === Syntax.MemberExpression && |
| 31 | + node.left.property.type === Syntax.Identifier && |
| 32 | + node.left.property.name === "exports" && |
| 33 | + node.right.type === Syntax.ObjectExpression && |
| 34 | + node.right.properties.constructor === Array |
| 35 | + ) { |
| 36 | + for (const array_node of node.right.properties) { |
| 37 | + if (array_node.value.type !== Syntax.Literal || isProbablyNumeric(String(array_node.value.value))) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + const value = array_node.key.name ?? array_node.key.value; |
| 41 | + if (!Object.hasOwn(classNames, array_node.value.value)) { |
| 42 | + classNames[array_node.value.value] = value; |
| 43 | + } else { |
| 44 | + if (!classNames[array_node.value.value]?.includes(value)) { |
| 45 | + // this makes invalid css but we don't have to care |
| 46 | + classNames[array_node.value.value] = `${classNames[array_node.value.value]}|${value}`; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }, |
| 52 | + }); |
| 53 | + } catch (e) { |
| 54 | + console.error(`Unable to parse "${file}":`, e); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +async function FixCssFile(path, classNames) { |
| 59 | + try { |
| 60 | + const css = (await readFile(path)).toString(); |
| 61 | + let output = css; |
| 62 | + const matches = [...new Set(css.match(CLASS_SELECTOR_REGEX))]; |
| 63 | + if (matches) { |
| 64 | + for (const match of matches) { |
| 65 | + if (Object.hasOwn(classNames, match.slice(1))) { |
| 66 | + output = output.replaceAll(match, `.${classNames[match.slice(1)]}`); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + const outputPath = path.includes("/ClientExtracted/") ? join(dirname(path), "/c.", basename(path)) : join(__dirname, "/c/", path.replace(__dirname, "")); |
| 71 | + if (output !== css) { |
| 72 | + await mkdir(dirname(outputPath), { recursive: true }); |
| 73 | + await writeFile(outputPath, output); |
| 74 | + } |
| 75 | + } catch (e) { |
| 76 | + console.error(`Unable to fix "${path}":`, e); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +async function* GetRecursiveFiles(dir, ext) { |
| 81 | + const dirents = await readDir(dir, { withFileTypes: true }); |
| 82 | + for (const dirent of dirents) { |
| 83 | + const res = pathResolve(dir, dirent.name); |
| 84 | + if (dirent.isDirectory()) { |
| 85 | + yield* GetRecursiveFiles(res, ext); |
| 86 | + } else if (dirent.isFile() && dirent.name.endsWith(ext)) { |
| 87 | + yield res; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function isProbablyNumeric(string, nbrRecursiceSlice) { |
| 93 | + if (!Number.isNaN(Number(string))) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + let testStr = string; |
| 97 | + for (let i = 0; i < 2 && testStr.length > 0; i++) { |
| 98 | + testStr = testStr.slice(0, -1); |
| 99 | + if (!Number.isNaN(Number(testStr))) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + return false; |
| 104 | +} |
| 105 | + |
| 106 | +async function cleanPreviousRun(cssPath) { |
| 107 | + try { |
| 108 | + const rmPath = join(__dirname, "/c/", cssPath, "/**.css"); |
| 109 | + await rm(rmPath, { force: true }); |
| 110 | + } catch (e) { |
| 111 | + if (e.code !== 'ENOENT') { |
| 112 | + console.error(`Unable to clean "${cssPath}":`, e); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +let paths; |
| 118 | +if (process.argv.length > 2) { |
| 119 | + paths = [process.argv[2]]; |
| 120 | +} |
| 121 | +else { |
| 122 | + paths = [ |
| 123 | + "./Scripts/WebUI/", |
| 124 | + "./ClientExtracted/", |
| 125 | + "./help.steampowered.com/", |
| 126 | + "./partner.steamgames.com/", |
| 127 | + "./steamcommunity.com/", |
| 128 | + "./store.steampowered.com/", |
| 129 | + "./www.dota2.com/", |
| 130 | + "./www.underlords.com/", |
| 131 | + "./www.counter-strike.net/", |
| 132 | + ]; |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +for (const path of paths) { |
| 137 | + const classNames = {}; |
| 138 | + |
| 139 | + cleanPreviousRun(path); |
| 140 | + |
| 141 | + for await (const file of GetRecursiveFiles(path, ".js")) { |
| 142 | + await ParseJsFile(file, classNames); |
| 143 | + } |
| 144 | + |
| 145 | + for await (const file of GetRecursiveFiles(path, ".css")) { |
| 146 | + await FixCssFile(file, classNames); |
| 147 | + } |
| 148 | +} |
0 commit comments