76 lines
No EOL
2.7 KiB
TypeScript
76 lines
No EOL
2.7 KiB
TypeScript
/**
|
|
* Generates path aliases in tsconfig.json for every directory under src/
|
|
* and optionally data/ and messages/.
|
|
*
|
|
* Usage: npx ts-node scripts/generate-aliases.ts
|
|
*/
|
|
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
const TSCONFIG_PATH = path.join(__dirname, "../tsconfig.json");
|
|
const SRC_DIR = path.join(__dirname, "../src");
|
|
|
|
function getDirs(dir: string, prefix: string): Record<string, string[]> {
|
|
const result: Record<string, string[]> = {};
|
|
if (!fs.existsSync(dir)) return result;
|
|
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
if (!entry.isDirectory()) continue;
|
|
const relPath = path.join(prefix, entry.name);
|
|
const aliasKey = `@${entry.name}/*`;
|
|
const aliasVal = [`src/${relPath}/*`];
|
|
result[aliasKey] = aliasVal;
|
|
|
|
// Also add a direct alias for files in this dir (e.g. @utils → src/utils)
|
|
const directKey = `@${entry.name}`;
|
|
result[directKey] = [`src/${relPath}`];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Also alias every .ts file directly under src/ (e.g. @utils → src/utils)
|
|
function getFiles(dir: string): Record<string, string[]> {
|
|
const result: Record<string, string[]> = {};
|
|
if (!fs.existsSync(dir)) return result;
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
if (!entry.isFile() || !entry.name.endsWith(".ts")) continue;
|
|
const name = path.basename(entry.name, ".ts");
|
|
result[`@${name}`] = [`src/${name}`];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
const tsconfig = JSON.parse(fs.readFileSync(TSCONFIG_PATH, "utf8"));
|
|
|
|
// Generate aliases for all dirs under src/
|
|
const newPaths: Record<string, string[]> = {
|
|
"@src/*": ["src/*"],
|
|
...getDirs(SRC_DIR, ""),
|
|
...getFiles(SRC_DIR),
|
|
};
|
|
|
|
// Also scan subdirs (systems, subcommands, handlers, commands)
|
|
for (const subdir of ["systems", "subcommands", "handlers", "commands"]) {
|
|
const full = path.join(SRC_DIR, subdir);
|
|
if (!fs.existsSync(full)) continue;
|
|
for (const entry of fs.readdirSync(full, { withFileTypes: true })) {
|
|
const name = entry.isDirectory() ? entry.name : path.basename(entry.name, ".ts");
|
|
if (!entry.isDirectory() && !entry.name.endsWith(".ts")) continue;
|
|
// Don't override parent alias
|
|
if (!newPaths[`@${name}`]) {
|
|
newPaths[`@${name}`] = entry.isDirectory()
|
|
? [`src/${subdir}/${name}/*`]
|
|
: [`src/${subdir}/${name}`];
|
|
}
|
|
if (entry.isDirectory() && !newPaths[`@${name}/*`]) {
|
|
newPaths[`@${name}/*`] = [`src/${subdir}/${name}/*`];
|
|
}
|
|
}
|
|
}
|
|
|
|
tsconfig.compilerOptions.paths = newPaths;
|
|
fs.writeFileSync(TSCONFIG_PATH, JSON.stringify(tsconfig, null, 2));
|
|
|
|
console.log(`✅ Generated ${Object.keys(newPaths).length} path aliases in tsconfig.json`);
|
|
console.log(Object.keys(newPaths).sort().join("\n")); |