-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-imports.js
56 lines (49 loc) · 1.82 KB
/
update-imports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Usage: node update-imports.js
// Description: This script updates import statements in .js and .ts files to include the ".ts" extension.
import fs from 'fs';
import path from 'path';
const srcDir = './src';
// Function to recursively process files in a directory
async function processDirectory(directory) {
const files = await fs.promises.readdir(directory, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
// Recurse into subdirectories
await processDirectory(path.join(directory, file.name));
} else if (file.name.endsWith('.js') || file.name.endsWith('.ts')) {
// Process .js and .ts files
const filePath = path.join(directory, file.name);
await processFile(filePath);
}
}
}
// Function to process a single file
async function processFile(filePath) {
let content = await fs.promises.readFile(filePath, 'utf8');
// Modify import statements that do not end with ".ts"
content = content.replace(
/import\s+([\s\S]+?)\s+from\s+['"]([^'"\s]+)['"];?/g,
(match, importClause, importPath) => {
if (
!importPath.endsWith('.ts') &&
!importPath.startsWith('.') &&
!importPath.startsWith('@')
) {
return `import ${importClause} from '${importPath}.ts';`;
} else if (
!importPath.endsWith('.ts') &&
importPath.startsWith('.') &&
!importPath.includes('.js')
) {
// Adjust the script to handle relative imports within the project
return `import ${importClause} from '${importPath}.ts';`;
}
return match;
},
);
await fs.promises.writeFile(filePath, content, 'utf8');
}
// Start processing the src directory
processDirectory(srcDir)
.then(() => console.log('Import statements updated.'))
.catch((error) => console.error('An error occurred:', error));