-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-imports.ts
101 lines (82 loc) · 2.99 KB
/
update-imports.ts
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
import { readFileSync, writeFileSync, readdirSync, statSync } from 'fs';
import { join, relative } from 'path';
const addReactImport = (content: string): string => {
if (!content.includes("import * as React from 'react'")) {
return `import * as React from 'react';\n${content}`;
}
return content;
};
const updateComponentImports = (content: string): string => {
return content
.replace(/@\/components\/ui\//g, '@/components/shared/')
.replace(/from ['"]\.\.\/ui\//g, 'from "../shared/')
.replace(/from ['"]\.\.\/\.\.\/components\/ui\//g, 'from "../../components/shared/')
.replace(/from ['"]\.\.\/\.\.\/components\/contact\//g, 'from "../../components/features/contact/')
.replace(/from ['"]\.\.\/contact\//g, 'from "../features/contact/')
.replace(/from ['"]\.\.\/\.\.\/components\/analytics\//g, 'from "../../components/features/analytics/')
.replace(/from ['"]\.\.\/analytics\//g, 'from "../features/analytics/');
};
const getAllFiles = (dir: string): string[] => {
const files: string[] = [];
const traverse = (currentDir: string) => {
const entries = readdirSync(currentDir);
entries.forEach(entry => {
const fullPath = join(currentDir, entry);
const stat = statSync(fullPath);
if (stat.isDirectory()) {
if (!fullPath.includes('node_modules') && !fullPath.includes('.next')) {
traverse(fullPath);
}
} else if (entry.endsWith('.ts') || entry.endsWith('.tsx')) {
files.push(fullPath);
}
});
};
traverse(dir);
return files;
};
const processFile = (filePath: string) => {
try {
let content = readFileSync(filePath, 'utf8');
// Add React import for .tsx files
if (filePath.endsWith('.tsx')) {
content = addReactImport(content);
}
// Update component imports
content = updateComponentImports(content);
writeFileSync(filePath, content);
console.log(`Updated ${filePath}`);
} catch (error) {
console.error(`Error processing ${filePath}:`, error);
}
};
// Process all TypeScript/React files
const files = getAllFiles('.');
files.forEach(processFile);
// Update index files
const componentsIndex = `// Re-export shared components
export * from './shared';
// Re-export feature components
export * from './features';
`;
const sharedIndex = `// Re-export UI components
export * from './button';
export * from './card';
export * from './input';
export * from './table';
export * from './toast';
export * from './toaster';
export * from './use-toast';
// Re-export types
export type { ButtonProps } from './button';
export type { InputProps } from './input';
export type { ToastProps, ToastActionElement } from './toast';
`;
const featuresIndex = `// Re-export feature components
export * from './analytics';
export * from './contact';
`;
writeFileSync('components/index.ts', componentsIndex);
writeFileSync('components/shared/index.ts', sharedIndex);
writeFileSync('components/features/index.ts', featuresIndex);
console.log('Import updates complete!');