Skip to content

Commit

Permalink
add biome
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifeismana committed Nov 28, 2024
1 parent 802f613 commit 45c8e1a
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 56 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/biome.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Linter & Formatter Check
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install modules
run: npm ci
- name: Run Biome
run: npx biome check .
34 changes: 34 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": ["SteamTracking", "GameTracking-SteamVR/"]
},
"formatter": {
"enabled": true,
"indentStyle": "tab",
"lineWidth": 150
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"noForEach": "off"
}
}
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
}
}
14 changes: 4 additions & 10 deletions dump_javascript_paths.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/* Based on https://github.com/SteamDatabase/SteamTracking/blob/master/dump_javascript_paths.mjs
Thanks xPaw! */
import { resolve as pathResolve } from "path";
import { readdir as readDir } from "fs/promises";
import { readdir as readDir } from "node:fs/promises";
import { resolve as pathResolve } from "node:path";

const pathsToRecurse = [
"./SteamTracking/",
"./GameTracking-SteamVR/",
];
const pathsToRecurse = ["./SteamTracking/", "./GameTracking-SteamVR/"];

const blocklist = [
"licenses.js",
"steamaudio.js",
];
const blocklist = ["licenses.js", "steamaudio.js"];

async function* GetRecursiveJavascriptCssFiles(dir) {
const dirents = await readDir(dir, { withFileTypes: true });
Expand Down
65 changes: 29 additions & 36 deletions find_svg.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {basename, sep as pathSep } from "path";
import { readFile } from "fs/promises";
import { createWriteStream, existsSync, mkdirSync, rmSync } from "fs";
import { createHash } from "node:crypto";
import { parse, latestEcmaVersion } from "espree";
import { traverse, Syntax } from "estraverse";
import { GetRecursiveFilesToParse } from "./dump_javascript_paths.mjs";
import { createWriteStream, existsSync, mkdirSync, rmSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { basename, sep as pathSep } from "node:path";
import { latestEcmaVersion, parse } from "espree";
import { Syntax, traverse } from "estraverse";
import { create } from "xmlbuilder2";
import { GetRecursiveFilesToParse } from "./dump_javascript_paths.mjs";

const svgOutputPath = "./svgs";
const pngOutputPath = "./pngs";
Expand All @@ -18,88 +18,81 @@ if (existsSync(pngOutputPath)) {
rmSync(pngOutputPath, { recursive: true });
}

const base64PngPattern = RegExp("data:image/png;base64,([A-Za-z0-9+/=]+)", "g");
const base64PngPattern = /data:image\/png;base64,([A-Za-z0-9+\/=]+)/g;

for await (const file of GetRecursiveFilesToParse()) {
try {
console.log("::group::Parsing", file);

const code = await readFile(file, "utf8");

const file_basename = basename(file, '.js');
const file_basename = basename(file, ".js");

if ( file.endsWith('.js') ) {
if (file.endsWith(".js")) {
console.log("Looking for svgs");

const ast = parse(code, { ecmaVersion: latestEcmaVersion, loc: true });
let last_function_seen = null;


// output folder / resource folder / file name
const outputFolder = `${svgOutputPath}/${file.replace(process.cwd(), '').split(pathSep)[1]}/${file_basename}`;
if (!existsSync(outputFolder))
mkdirSync(outputFolder, { recursive: true });
const outputFolder = `${svgOutputPath}/${file.replace(process.cwd(), "").split(pathSep)[1]}/${file_basename}`;
if (!existsSync(outputFolder)) mkdirSync(outputFolder, { recursive: true });

traverse(ast, {
enter: function (node) {
if(node.type === Syntax.FunctionDeclaration) {
if (node.type === Syntax.FunctionDeclaration) {
last_function_seen = node;
}
if (node.type === Syntax.CallExpression && node.callee?.property?.name === 'createElement' && node.arguments?.[0]?.value === 'svg') {

if (node.type === Syntax.CallExpression && node.callee?.property?.name === "createElement" && node.arguments?.[0]?.value === "svg") {
// as i understand it we don't want to go deeper if it's an svg (bc there can be svg in svg but we're only interested in the one most "outside")
this.skip();
const svg = (createSvgBody(node)).end({ prettyPrint: true });
const hash = createHash('sha1').update(svg).digest('hex').substring(0,16);
const svg = createSvgBody(node).end({ prettyPrint: true });
const hash = createHash("sha1").update(svg).digest("hex").substring(0, 16);
console.debug(`Hash ${hash} from ${file} line ${node.loc.start.line} col ${node.loc.start.column}`);
OutputToFile(`${outputFolder}/${last_function_seen?.id.name ?? "null"}_${hash}.svg`, `${svg}\n`);
}}
}
},
});
}

console.log("Looking for pngs")
console.log("Looking for pngs");

// output folder / resource folder / file name
const outputFolder = `${pngOutputPath}/${file.replace(process.cwd(), '').split(pathSep)[1]}/${file_basename}`;
if (!existsSync(outputFolder))
mkdirSync(outputFolder, { recursive: true });

const outputFolder = `${pngOutputPath}/${file.replace(process.cwd(), "").split(pathSep)[1]}/${file_basename}`;
if (!existsSync(outputFolder)) mkdirSync(outputFolder, { recursive: true });

const result = code.matchAll(base64PngPattern);
for (const match of result) {
const png = Buffer.from(match[1], "base64");
const hash = createHash('sha1').update(png).digest('hex').substring(0,16);
const hash = createHash("sha1").update(png).digest("hex").substring(0, 16);
console.debug(`Hash ${hash} from ${file}`);
OutputToFile(`${outputFolder}/${hash}.png`, png);
}

} catch (e) {
console.error(`::error::Unable to parse "${file}":`, e);
continue;
} finally {
console.log("::endgroup::");
}
}

function createSvgBody(node, xml = undefined) {
if (!xml) {
xml = create();
}
function createSvgBody(node, xml = create()) {
if (!node) {
return xml;
}
try {
const elem = xml.ele(node.arguments[0].value);
node.arguments[1].properties?.forEach((prop) => {
if (prop.type === 'SpreadElement' || prop.key.name === 'className') return;
elem.att(fixSVGKeyName(prop.key) , prop.value.value);
})
if (prop.type === "SpreadElement" || prop.key.name === "className") return;
elem.att(fixSVGKeyName(prop.key), prop.value.value);
});
node.arguments.slice(2)?.forEach((prop) => {
if (prop.type === 'CallExpression') {
if (prop.type === "CallExpression") {
createSvgBody(prop, elem);
}
});
} catch (e) {
console.warn("::warning::probably some vars that i can do nothing about",e);
console.warn("::warning::probably some vars that i can do nothing about", e);
}
return xml;
}
Expand Down
167 changes: 167 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"dependencies": {
"espree": "^10.1.0",
"estraverse": "github:Lifeismana/estraverse#static-block",
"xmlbuilder2": "^3.1.1"
}
"dependencies": {
"espree": "^10.1.0",
"estraverse": "github:Lifeismana/estraverse#static-block",
"xmlbuilder2": "^3.1.1"
},
"devDependencies": {
"@biomejs/biome": "1.9.4"
}
}
6 changes: 2 additions & 4 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:recommended"]
}
Loading

0 comments on commit 45c8e1a

Please sign in to comment.