-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
181 lines (153 loc) · 5.13 KB
/
Copy pathcli.ts
File metadata and controls
181 lines (153 loc) · 5.13 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
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import chokidar from "chokidar";
import { CONFIG } from "./config";
import { markFileChanged, scanAllBackendFunctions } from "./generator";
import { shouldProcessFile } from "./utils/utils";
const args = process.argv.slice(2);
const isWatchMode = args.includes("--watch") || args.includes("-w");
const isVersionMode = args.includes("--version") || args.includes("-v");
const isHelpMode = args.includes("--help") || args.includes("-h");
if (isVersionMode) {
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json"), "utf-8"));
console.log(`quickwire v${packageJson.version}`);
process.exit(0);
}
if (isHelpMode) {
console.log(`
🚀 Quickwire - Automatic API Generator for Next.js
Usage:
quickwire [options]
Options:
--watch, -w Watch for file changes and regenerate automatically
--version, -v Show version number
--help, -h Show this help message
Examples:
quickwire # Generate API routes once
quickwire --watch # Watch for changes and regenerate
quickwire -w # Short form of watch mode
Configuration:
Place a quickwire.config.json file in your scripts/ directory to customize settings.
For more information, visit: https://github.com/quickwire/quickwire
`);
process.exit(0);
}
let watchTimeout: NodeJS.Timeout | null = null;
function debouncedScan(): void {
if (watchTimeout) {
clearTimeout(watchTimeout);
}
watchTimeout = setTimeout(() => {
console.log("🔄 Files changed, regenerating...");
scanAllBackendFunctions();
}, CONFIG.watchDebounceMs);
}
function runWatch(): void {
console.log("🚀 Quickwire watch mode started...");
console.log(`📂 Watching: ${CONFIG.backendDir}`);
console.log("🔧 HTTP Method Detection Enabled:");
Object.entries(CONFIG.httpMethods).forEach(([method, prefixes]) => {
console.log(` ${method}: ${prefixes.slice(0, 5).join(', ')}${prefixes.length > 5 ? ', ...' : ''}`);
});
// Initial scan without debouncing
console.log("🔍 Performing initial scan...");
scanAllBackendFunctions();
const watcher = chokidar.watch(CONFIG.backendDir, {
ignoreInitial: true,
ignored: [
...CONFIG.excludePatterns.map((p) => `**/${p}`),
"**/node_modules/**",
"**/.git/**",
"**/.next/**",
"**/dist/**",
"**/build/**",
],
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 50,
},
});
watcher
.on("add", (filePath) => {
console.log(`📄 Added: ${path.relative(process.cwd(), filePath)}`);
markFileChanged(filePath);
debouncedScan();
})
.on("change", (filePath) => {
console.log(`📝 Changed: ${path.relative(process.cwd(), filePath)}`);
markFileChanged(filePath);
debouncedScan();
})
.on("unlink", (filePath) => {
console.log(`🗑️ Removed: ${path.relative(process.cwd(), filePath)}`);
if (shouldProcessFile(filePath, CONFIG)) {
// Handle file deletion
markFileChanged(filePath);
}
debouncedScan();
})
.on("error", (error) => {
console.error("❌ Watch error:", error);
});
// Graceful shutdown
process.on("SIGINT", () => {
console.log("\n🛑 Shutting down Quickwire watch mode...");
watcher.close();
process.exit(0);
});
process.on("SIGTERM", () => {
console.log("\n🛑 Terminating Quickwire watch mode...");
watcher.close();
process.exit(0);
});
}
function ensureUtilsFile(): void {
const targetUtilsPath = path.join(process.cwd(), "src", "lib", "utils.quickwire.ts");
const sourceUtilsPath = path.join(__dirname, "utils", "utils.quickwire.ts");
if (!fs.existsSync(targetUtilsPath)) {
console.log("📄 utils.quickwire.ts not found, copying from source...");
// Ensure target directory exists
const targetDir = path.dirname(targetUtilsPath);
fs.mkdirSync(targetDir, { recursive: true });
// Copy the file
try {
fs.copyFileSync(sourceUtilsPath, targetUtilsPath);
console.log("✅ Successfully copied utils.quickwire.ts to @/lib/utils.quickwire.ts");
} catch (error) {
console.error("❌ Failed to copy utils.quickwire.ts:", error);
throw error;
}
}
}
function main(): void {
try {
// Ensure utils file exists first
ensureUtilsFile();
// Ensure directories exist
fs.mkdirSync(CONFIG.backendDir, { recursive: true });
fs.mkdirSync(CONFIG.apiDir, { recursive: true });
fs.mkdirSync(CONFIG.quickwireDir, { recursive: true });
if (isWatchMode) {
runWatch();
} else {
console.log("🔧 Running Quickwire generation...");
scanAllBackendFunctions();
console.log("✅ Quickwire generation complete");
}
} catch (error) {
console.error("❌ Fatal error:", error);
process.exit(1);
}
}
// Handle uncaught exceptions
process.on("uncaughtException", (error) => {
console.error("❌ Uncaught exception:", error);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
console.error("❌ Unhandled rejection at:", promise, "reason:", reason);
process.exit(1);
});
main();