-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy-runtime.js
More file actions
210 lines (176 loc) · 6.08 KB
/
copy-runtime.js
File metadata and controls
210 lines (176 loc) · 6.08 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env node
/**
* Copy runtime build output to demo node_modules for testing
*
* This script copies the built runtime from runtime/out to demo/node_modules/@decillion/runtime/out
* to avoid symlink issues during development.
*
* Usage:
* node copy-runtime.js
* node copy-runtime.js --watch
* node copy-runtime.js --verbose
* node copy-runtime.js --watch --verbose
*/
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
// Parse command line arguments
const args = process.argv.slice(2);
const watch = args.includes('--watch');
const verbose = args.includes('--verbose');
// Define paths
const scriptDir = __dirname;
const runtimeOutPath = path.join(scriptDir, 'runtime', 'out');
const demoNodeModulesPath = path.join(scriptDir, 'demo', 'node_modules', '@decillion', 'runtime');
const targetOutPath = path.join(demoNodeModulesPath, 'out');
function log(message, color = 'white') {
const colors = {
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
gray: '\x1b[90m',
white: '\x1b[37m',
reset: '\x1b[0m'
};
console.log(`${colors[color]}${message}${colors.reset}`);
}
function copyDirectorySync(src, dest) {
if (!fs.existsSync(src)) {
return false;
}
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirectorySync(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
return true;
}
function countFiles(dir) {
let count = 0;
function countRecursive(currentDir) {
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
countRecursive(fullPath);
} else {
count++;
}
}
}
if (fs.existsSync(dir)) {
countRecursive(dir);
}
return count;
}
function copyRuntimeFiles(isVerbose = false) {
// Check if source exists
if (!fs.existsSync(runtimeOutPath)) {
log(`Runtime output directory not found at: ${runtimeOutPath}`, 'red');
log("Please run 'npm run build' in the runtime directory first.", 'yellow');
return false;
}
// Create target directory structure if it doesn't exist
if (!fs.existsSync(demoNodeModulesPath)) {
if (isVerbose) {
log(`Creating directory: ${demoNodeModulesPath}`, 'cyan');
}
fs.mkdirSync(demoNodeModulesPath, { recursive: true });
}
// Remove existing out directory if it exists
if (fs.existsSync(targetOutPath)) {
if (isVerbose) {
log(`Removing existing: ${targetOutPath}`, 'yellow');
}
fs.rmSync(targetOutPath, { recursive: true, force: true });
}
// Copy the out directory
try {
if (isVerbose) {
log(`Copying ${runtimeOutPath} -> ${targetOutPath}`, 'green');
}
copyDirectorySync(runtimeOutPath, targetOutPath);
const fileCount = countFiles(targetOutPath);
log(`✅ Successfully copied ${fileCount} files to demo/node_modules/@decillion/runtime/out`, 'green');
return true;
} catch (error) {
log(`Failed to copy runtime files: ${error.message}`, 'red');
return false;
}
}
// Initial copy
log('🔄 Copying runtime output to demo...', 'blue');
const success = copyRuntimeFiles(verbose);
if (!success) {
process.exit(1);
}
// Watch mode
if (watch) {
log('👀 Watching for changes in runtime/out...', 'blue');
log('Press Ctrl+C to stop watching', 'gray');
const chokidar = require('chokidar');
// Check if chokidar is available, if not provide fallback
let watcher;
try {
watcher = chokidar.watch(runtimeOutPath, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
ignoreInitial: true
});
let timeoutId;
const debouncedCopy = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
copyRuntimeFiles(verbose);
}, 500);
};
watcher
.on('add', (filePath) => {
log(`🔄 Detected file added: ${path.relative(runtimeOutPath, filePath)}`, 'yellow');
debouncedCopy();
})
.on('change', (filePath) => {
log(`🔄 Detected file changed: ${path.relative(runtimeOutPath, filePath)}`, 'yellow');
debouncedCopy();
})
.on('unlink', (filePath) => {
log(`🔄 Detected file deleted: ${path.relative(runtimeOutPath, filePath)}`, 'yellow');
debouncedCopy();
});
} catch (error) {
log('chokidar not found, falling back to basic fs.watch', 'yellow');
log('For better file watching, install chokidar: npm install chokidar', 'yellow');
// Fallback to fs.watch
let timeoutId;
const debouncedCopy = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
copyRuntimeFiles(verbose);
}, 500);
};
fs.watch(runtimeOutPath, { recursive: true }, (eventType, filename) => {
if (filename) {
log(`🔄 Detected ${eventType} in ${filename}`, 'yellow');
debouncedCopy();
}
});
}
// Keep the process running
process.on('SIGINT', () => {
log('\n👋 Stopping file watcher...', 'gray');
if (watcher && watcher.close) {
watcher.close();
}
process.exit(0);
});
}