-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-paths.js
102 lines (89 loc) · 3.06 KB
/
update-paths.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs');
const path = require('path');
const updateImports = (content) => {
// Add React import if it's a TSX file and doesn't already have React imported
if (!content.includes("import * as React from 'react'") &&
!content.includes('import * as React from "react"')) {
content = `import * as React from 'react';\n${content}`;
}
// Update import paths
const replacements = [
[/@\/components\/ui\//g, '@/components/shared/'],
[/from ['"]\.\.\/ui\//g, 'from "../shared/'],
[/from ['"]\.\.\/\.\.\/components\/ui\//g, 'from "../../components/shared/'],
[/from ['"]\.\.\/\.\.\/components\/contact\//g, 'from "../../components/features/contact/'],
[/from ['"]\.\.\/contact\//g, 'from "../features/contact/'],
[/from ['"]\.\.\/\.\.\/components\/analytics\//g, 'from "../../components/features/analytics/'],
[/from ['"]\.\.\/analytics\//g, 'from "../features/analytics/']
];
return replacements.reduce((acc, [pattern, replacement]) =>
acc.replace(pattern, replacement), content);
};
const processFile = (filePath) => {
try {
if (filePath.endsWith('.ts') || filePath.endsWith('.tsx')) {
console.log(`Processing ${filePath}...`);
const content = fs.readFileSync(filePath, 'utf8');
const updatedContent = updateImports(content);
if (content !== updatedContent) {
fs.writeFileSync(filePath, updatedContent);
console.log(`Updated imports in ${filePath}`);
}
}
} catch (error) {
console.error(`Error processing ${filePath}:`, error.message);
}
};
const walkDir = (dir) => {
try {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// Skip node_modules and .next directories
if (file !== 'node_modules' && file !== '.next') {
walkDir(filePath);
}
} else {
processFile(filePath);
}
});
} catch (error) {
console.error(`Error walking directory ${dir}:`, error.message);
}
};
// Update tsconfig.json
const updateTsConfig = () => {
const tsConfigPath = 'tsconfig.json';
try {
if (fs.existsSync(tsConfigPath)) {
const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf8'));
tsConfig.compilerOptions = {
...tsConfig.compilerOptions,
forceConsistentCasingInFileNames: true,
baseUrl: ".",
paths: {
"@/*": ["./*"]
}
};
fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2));
console.log('Updated tsconfig.json');
}
} catch (error) {
console.error('Error updating tsconfig.json:', error.message);
}
};
// Remove jsconfig.json if it exists
try {
if (fs.existsSync('jsconfig.json')) {
fs.unlinkSync('jsconfig.json');
console.log('Removed jsconfig.json');
}
} catch (error) {
console.error('Error removing jsconfig.json:', error.message);
}
console.log('Starting path updates...');
walkDir('.');
updateTsConfig();
console.log('Path updates complete!');