a tool for shared writing and social publishing
1import * as fs from "fs";
2import * as path from "path";
3
4/**
5 * Recursively processes all files in a directory and removes .js extensions from imports
6 */
7function fixExtensionsInDirectory(dir: string): void {
8 const entries = fs.readdirSync(dir, { withFileTypes: true });
9
10 for (const entry of entries) {
11 const fullPath = path.join(dir, entry.name);
12
13 if (entry.isDirectory()) {
14 fixExtensionsInDirectory(fullPath);
15 } else if (entry.isFile() && (entry.name.endsWith(".ts") || entry.name.endsWith(".js"))) {
16 fixExtensionsInFile(fullPath);
17 }
18 }
19}
20
21/**
22 * Removes .js extensions from import/export statements in a file
23 */
24function fixExtensionsInFile(filePath: string): void {
25 const content = fs.readFileSync(filePath, "utf-8");
26 const fixedContent = content.replace(/\.js'/g, "'");
27
28 if (content !== fixedContent) {
29 fs.writeFileSync(filePath, fixedContent, "utf-8");
30 console.log(`Fixed: ${filePath}`);
31 }
32}
33
34// Get the directory to process from command line arguments
35const targetDir = process.argv[2] || "./lexicons/api";
36
37if (!fs.existsSync(targetDir)) {
38 console.error(`Directory not found: ${targetDir}`);
39 process.exit(1);
40}
41
42console.log(`Fixing extensions in: ${targetDir}`);
43fixExtensionsInDirectory(targetDir);
44console.log("Done!");