-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.ts
More file actions
73 lines (57 loc) · 2.57 KB
/
Copy pathsplit.ts
File metadata and controls
73 lines (57 loc) · 2.57 KB
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
import { Project, SyntaxKind } from 'ts-morph';
import * as fs from 'fs';
import * as path from 'path';
const project = new Project();
const sourceFile = project.addSourceFileAtPath('src/App.tsx');
const screensDir = path.join(process.cwd(), 'src', 'screens');
const componentsDir = path.join(process.cwd(), 'src', 'components');
const typesDir = path.join(process.cwd(), 'src', 'types');
[screensDir, componentsDir, typesDir].forEach(dir => {
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
});
// Extract types
const typeAliases = sourceFile.getTypeAliases();
let typesContent = '';
typeAliases.forEach(typeAlias => {
typesContent += 'export ' + typeAlias.getText() + '\n\n';
});
fs.writeFileSync(path.join(typesDir, 'index.ts'), typesContent);
// Get imports
const imports = sourceFile.getImportDeclarations().map(imp => imp.getText()).join('\n');
// Extract ExploreIcon
const variableStatements = sourceFile.getVariableStatements();
const exploreIconVar = variableStatements.find(v => v.getDeclarations().some(d => d.getName() === 'ExploreIcon'));
if (exploreIconVar) {
let content = `import React from 'react';\n\n`;
content += `export const ExploreIcon = ${exploreIconVar.getDeclarations()[0].getInitializer()?.getText()};\n`;
fs.writeFileSync(path.join(componentsDir, 'ExploreIcon.tsx'), content);
}
// Extract functions
const functions = sourceFile.getFunctions();
const appFunction = functions.find(f => f.getName() === 'App');
const otherFunctions = functions.filter(f => f.getName() !== 'App');
otherFunctions.forEach(func => {
const name = func.getName();
if (!name) return;
let content = `${imports}\n\n`;
content += `import { Screen, Tab } from '../types';\n`;
content += `import { ExploreIcon } from '../components/ExploreIcon';\n\n`;
content += `export default ${func.getText()}\n`;
const dir = name.includes('Screen') || name.includes('Layout') ? screensDir : componentsDir;
fs.writeFileSync(path.join(dir, `${name}.tsx`), content);
});
// Now rewrite App.tsx
let newAppContent = `${imports}\n\n`;
newAppContent += `import { Screen, Tab } from './types';\n`;
newAppContent += `import { ExploreIcon } from './components/ExploreIcon';\n`;
otherFunctions.forEach(func => {
const name = func.getName();
if (!name) return;
const dir = name.includes('Screen') || name.includes('Layout') ? 'screens' : 'components';
newAppContent += `import ${name} from './${dir}/${name}';\n`;
});
if (appFunction) {
newAppContent += `\nexport default ${appFunction.getText()}\n`;
}
fs.writeFileSync('src/App.tsx', newAppContent);
console.log('Split complete!');