-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch-version.js
More file actions
166 lines (139 loc) · 4.56 KB
/
Copy pathswitch-version.js
File metadata and controls
166 lines (139 loc) · 4.56 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
/**
* Algorithmic Visual Evolution - Version Switcher
* A utility to switch between JavaScript and TypeScript versions
*
* Usage:
* node switch-version.js js # Run JavaScript version
* node switch-version.js ts # Run TypeScript version
*/
const fs = require('fs');
const path = require('path');
const { execSync, spawn } = require('child_process');
// Parse command line arguments
const versionArg = process.argv[2]?.toLowerCase();
if (!versionArg || (versionArg !== 'js' && versionArg !== 'ts')) {
console.error('Please specify which version to run: js or ts');
console.error('Example: node switch-version.js ts');
process.exit(1);
}
// Function to add .js extensions to imports in compiled files
function fixImports() {
function addJsExtensionToImports(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
// Replace import statements without extensions to include .js
const updatedContent = content.replace(
/from\s+['"](.+?)(?:\.js)?['"]/g,
(match, importPath) => {
// Skip external module imports
if (importPath.startsWith('.') || importPath.startsWith('/')) {
return `from '${importPath}.js'`;
}
return match;
}
);
fs.writeFileSync(filePath, updatedContent);
}
function processDirectory(directory) {
const files = fs.readdirSync(directory, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(directory, file.name);
if (file.isDirectory()) {
processDirectory(fullPath);
} else if (file.name.endsWith('.js')) {
addJsExtensionToImports(fullPath);
}
}
}
// Start processing the dist directory
processDirectory('./dist');
console.log('Added .js extensions to all imports in compiled files');
}
// Ensure the dist directory exists
function ensureDistDirectory() {
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist', { recursive: true });
}
// Also ensure subdirectories exist for JS version
if (versionArg === 'js') {
if (!fs.existsSync('dist/utils')) {
fs.mkdirSync('dist/utils', { recursive: true });
}
if (!fs.existsSync('dist/algorithms')) {
fs.mkdirSync('dist/algorithms', { recursive: true });
}
}
}
// Copy JS files to dist for the JS version
function copyJsFilesToDist() {
console.log('Copying JavaScript files to dist directory...');
// Copy main app.js
fs.copyFileSync('app.js', 'dist/app.js');
// Copy config.js
if (fs.existsSync('config.js')) {
fs.copyFileSync('config.js', 'dist/config.js');
}
// Copy types.js
if (fs.existsSync('types.js')) {
fs.copyFileSync('types.js', 'dist/types.js');
}
// Copy utils directory
if (fs.existsSync('utils')) {
const utilsFiles = fs.readdirSync('utils');
utilsFiles.forEach(file => {
if (file.endsWith('.js')) {
fs.copyFileSync(`utils/${file}`, `dist/utils/${file}`);
}
});
}
// Copy algorithms directory
if (fs.existsSync('algorithms')) {
const algoFiles = fs.readdirSync('algorithms');
algoFiles.forEach(file => {
if (file.endsWith('.js')) {
fs.copyFileSync(`algorithms/${file}`, `dist/algorithms/${file}`);
}
});
}
}
// Clean up function to be called before exit
function cleanup() {
// No need to delete any files as we're now keeping everything in dist/
process.exit(0);
}
// Register cleanup on exit
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
// Main execution logic
try {
// Clean dist directory for both versions
console.log('Cleaning dist directory...');
if (fs.existsSync('dist')) {
fs.rmSync('dist', { recursive: true, force: true });
}
ensureDistDirectory();
if (versionArg === 'js') {
console.log('🚀 Running JavaScript version');
console.log('📢 If you experience any UI issues, please run the JavaScript version again');
copyJsFilesToDist();
} else { // TypeScript version
console.log('🚀 Running TypeScript version');
console.log('📢 If parameters or UI elements are missing, try refreshing the browser');
// Compile TypeScript
console.log('Compiling TypeScript...');
execSync('tsc', { stdio: 'inherit' });
// Fix imports
console.log('Fixing module imports...');
fixImports();
}
// Start live-server
console.log('Starting development server...');
const server = spawn('live-server', ['--open=index.html'], {
stdio: 'inherit',
shell: true
});
server.on('exit', cleanup);
} catch (error) {
console.error('Error:', error.message);
cleanup();
}