From 9b59e98470d307d1a8a63133d49c35ae6143ff48 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Sat, 5 Jul 2025 01:56:47 +0200 Subject: [PATCH] chore: enforce consistent imports --- .github/workflows/build.yml | 1 + package.json | 3 ++- scripts/fix-imports.js | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 scripts/fix-imports.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2f265a60..375beec1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,6 +19,7 @@ jobs: - run: npm run lint:ci - run: npm run prettier - run: npm run update-translations + - run: npm run fix-imports - name: Fail if there are uncommitted changes run: | if [[ -n "$(git status --porcelain)" ]]; then diff --git a/package.json b/package.json index ef29ea57..fc40a54d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "lint:ci": "eslint --ext .ts --rule \"prettier/prettier: off\" .", "lint:fix": "eslint --fix --ext .ts .", "prettier": "prettier --write .", - "update-translations": "cd scripts && node update-translations.js" + "update-translations": "cd scripts && node update-translations.js", + "fix-imports": "cd scripts && node fix-imports.js" }, "license": "GNU", "dependencies": { diff --git a/scripts/fix-imports.js b/scripts/fix-imports.js new file mode 100644 index 00000000..66878694 --- /dev/null +++ b/scripts/fix-imports.js @@ -0,0 +1,46 @@ +/* eslint-disable */ +const fs = require("fs"); +const path = require("path"); + +const root = path.join(process.cwd(), ".."); + +function listFiles(dir) { + const entries = fs.readdirSync(dir, { withFileTypes: true }); + let results = []; + for (const entry of entries) { + const fullPath = path.join(dir, entry.name); + if (entry.isDirectory()) { + results = results.concat(listFiles(fullPath)); + } else { + results.push(fullPath); + } + } + return results; +} + +const files = listFiles(path.join(root, "src")); + +for (const file of files) { + let content; + try { + content = fs.readFileSync(file, "utf8"); + } catch (e) { + continue; + } + const dir = path.dirname(file); + const fixedContent = content.replaceAll(/} from "([^"]+)";/g, (sub, importPath) => { + if (!importPath.startsWith("@/")) { + const fullImportPath = path.resolve(dir, importPath); + if (fs.existsSync(fullImportPath + ".ts")) { + const relative = path.relative(root, fullImportPath).replace(/\\/g, "/"); + const fixedPath = "@/" + relative; + console.log(`${importPath} -> ${fixedPath}`); + return sub.split(importPath).join(fixedPath); + } + } + return sub; + }); + if (content != fixedContent) { + fs.writeFileSync(file, fixedContent, "utf8"); + } +}