@objectql/cli 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/dist/commands/studio.d.ts +5 -0
- package/dist/commands/studio.js +279 -0
- package/dist/commands/studio.js.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
- package/src/commands/studio.ts +264 -0
- package/src/index.ts +16 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { ObjectQL } from '@objectql/core';
|
|
2
|
+
import { createNodeHandler, createStudioHandler, createMetadataHandler } from '@objectql/server';
|
|
3
|
+
import { createServer } from 'http';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import * as fs from 'fs';
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
import { exec } from 'child_process';
|
|
8
|
+
import { register } from 'ts-node';
|
|
9
|
+
import glob from 'fast-glob';
|
|
10
|
+
|
|
11
|
+
const SWAGGER_HTML = `
|
|
12
|
+
<!DOCTYPE html>
|
|
13
|
+
<html lang="en">
|
|
14
|
+
<head>
|
|
15
|
+
<meta charset="utf-8" />
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
17
|
+
<title>ObjectQL Swagger UI</title>
|
|
18
|
+
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css" />
|
|
19
|
+
<style>
|
|
20
|
+
body { margin: 0; padding: 0; }
|
|
21
|
+
</style>
|
|
22
|
+
</head>
|
|
23
|
+
<body>
|
|
24
|
+
<div id="swagger-ui"></div>
|
|
25
|
+
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js" crossorigin></script>
|
|
26
|
+
<script>
|
|
27
|
+
window.onload = () => {
|
|
28
|
+
window.ui = SwaggerUIBundle({
|
|
29
|
+
url: '/openapi.json',
|
|
30
|
+
dom_id: '#swagger-ui',
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
</script>
|
|
34
|
+
</body>
|
|
35
|
+
</html>
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
function openBrowser(url: string) {
|
|
39
|
+
const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
|
|
40
|
+
exec(`${start} ${url}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function startStudio(options: { port: number; dir: string, open?: boolean }) {
|
|
44
|
+
const port = options.port || 3000;
|
|
45
|
+
const rootDir = path.resolve(process.cwd(), options.dir || '.');
|
|
46
|
+
|
|
47
|
+
console.log(chalk.blue('Starting ObjectQL Studio...'));
|
|
48
|
+
console.log(chalk.gray(`Project Root: ${rootDir}`));
|
|
49
|
+
|
|
50
|
+
// Register ts-node
|
|
51
|
+
register({
|
|
52
|
+
transpileOnly: true,
|
|
53
|
+
compilerOptions: {
|
|
54
|
+
module: "commonjs"
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
let app: ObjectQL;
|
|
59
|
+
const configTs = path.join(rootDir, 'objectql.config.ts');
|
|
60
|
+
const configJs = path.join(rootDir, 'objectql.config.js');
|
|
61
|
+
|
|
62
|
+
if (fs.existsSync(configTs)) {
|
|
63
|
+
console.log(chalk.gray(`Loading config from ${configTs}`));
|
|
64
|
+
const mod = require(configTs);
|
|
65
|
+
app = mod.default || mod;
|
|
66
|
+
} else if (fs.existsSync(configJs)) {
|
|
67
|
+
console.log(chalk.gray(`Loading config from ${configJs}`));
|
|
68
|
+
const mod = require(configJs);
|
|
69
|
+
app = mod.default || mod;
|
|
70
|
+
} else {
|
|
71
|
+
console.error(chalk.red('\n❌ Error: Configuration file (objectql.config.ts) not found.'));
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!app) {
|
|
76
|
+
console.error(chalk.red('\n❌ Error: No default export found in configuration file.'));
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 2. Load Schema & Init
|
|
81
|
+
try {
|
|
82
|
+
await app.init();
|
|
83
|
+
} catch (e: any) {
|
|
84
|
+
console.error(chalk.red('❌ Failed to initialize application:'), e.message);
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 3. Setup HTTP Server
|
|
89
|
+
const nodeHandler = createNodeHandler(app);
|
|
90
|
+
const studioHandler = createStudioHandler();
|
|
91
|
+
const metadataHandler = createMetadataHandler(app);
|
|
92
|
+
|
|
93
|
+
const server = createServer(async (req, res) => {
|
|
94
|
+
// CORS
|
|
95
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
96
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
97
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
98
|
+
|
|
99
|
+
if (req.method === 'OPTIONS') {
|
|
100
|
+
res.writeHead(200);
|
|
101
|
+
res.end();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (req.url === '/openapi.json') {
|
|
106
|
+
return nodeHandler(req, res);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (req.url === '/swagger') {
|
|
110
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
111
|
+
res.end(SWAGGER_HTML);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Routing
|
|
116
|
+
if (req.url?.startsWith('/studio')) {
|
|
117
|
+
return studioHandler(req, res);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (req.url?.startsWith('/api/schema/files')) {
|
|
121
|
+
// List all .object.yml files
|
|
122
|
+
try {
|
|
123
|
+
// Find all object.yml files relative to rootDir
|
|
124
|
+
// Note: User might have configured objectql with specific source paths.
|
|
125
|
+
// We ignore common build folders to avoid duplicates/editing compiled files.
|
|
126
|
+
const files = await glob('**/*.object.yml', {
|
|
127
|
+
cwd: rootDir,
|
|
128
|
+
ignore: ['node_modules/**', 'dist/**', 'build/**', 'out/**', '.git/**', '.next/**']
|
|
129
|
+
});
|
|
130
|
+
res.setHeader('Content-Type', 'application/json');
|
|
131
|
+
res.end(JSON.stringify({ files }));
|
|
132
|
+
} catch (e: any) {
|
|
133
|
+
res.statusCode = 500;
|
|
134
|
+
res.end(JSON.stringify({ error: e.message }));
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (req.url?.startsWith('/api/schema/content')) {
|
|
140
|
+
const urlObj = new URL(req.url, `http://${req.headers.host}`);
|
|
141
|
+
const file = urlObj.searchParams.get('file');
|
|
142
|
+
|
|
143
|
+
if (!file) {
|
|
144
|
+
res.statusCode = 400;
|
|
145
|
+
res.end(JSON.stringify({ error: 'Missing file parameter' }));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const filePath = path.join(rootDir, file);
|
|
150
|
+
// Security check
|
|
151
|
+
if (!filePath.startsWith(rootDir)) {
|
|
152
|
+
res.statusCode = 403;
|
|
153
|
+
res.end(JSON.stringify({ error: 'Access denied' }));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (req.method === 'GET') {
|
|
158
|
+
try {
|
|
159
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
160
|
+
res.setHeader('Content-Type', 'text/plain'); // Plain text (YAML)
|
|
161
|
+
res.end(content);
|
|
162
|
+
} catch (e) {
|
|
163
|
+
res.statusCode = 404;
|
|
164
|
+
res.end(JSON.stringify({ error: 'File not found' }));
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (req.method === 'POST') {
|
|
170
|
+
let body = '';
|
|
171
|
+
req.on('data', chunk => body += chunk);
|
|
172
|
+
req.on('end', () => {
|
|
173
|
+
try {
|
|
174
|
+
fs.writeFileSync(filePath, body, 'utf-8');
|
|
175
|
+
res.statusCode = 200;
|
|
176
|
+
res.end(JSON.stringify({ success: true }));
|
|
177
|
+
} catch (e: any) {
|
|
178
|
+
res.statusCode = 500;
|
|
179
|
+
res.end(JSON.stringify({ error: e.message }));
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (req.url?.startsWith('/api/schema/find')) {
|
|
187
|
+
const urlObj = new URL(req.url, `http://${req.headers.host}`);
|
|
188
|
+
const objectName = urlObj.searchParams.get('object');
|
|
189
|
+
|
|
190
|
+
if (!objectName) {
|
|
191
|
+
res.statusCode = 400;
|
|
192
|
+
res.end(JSON.stringify({ error: 'Missing object parameter' }));
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
// Find all object.yml files
|
|
198
|
+
const files = await glob('**/*.object.yml', {
|
|
199
|
+
cwd: rootDir,
|
|
200
|
+
ignore: ['node_modules/**', 'dist/**', 'build/**', 'out/**', '.git/**', '.next/**']
|
|
201
|
+
});
|
|
202
|
+
let foundFile = null;
|
|
203
|
+
|
|
204
|
+
// Naive parsing to find the object definition
|
|
205
|
+
// We don't use the FULL parser, just checks if "name: objectName" is present
|
|
206
|
+
for (const file of files) {
|
|
207
|
+
const content = fs.readFileSync(path.join(rootDir, file), 'utf-8');
|
|
208
|
+
// Simple check: name: <objectName> or name: "<objectName>"
|
|
209
|
+
// This creates a regex that looks for `name:` followed by the objectName
|
|
210
|
+
// Handles spaces, quotes
|
|
211
|
+
const regex = new RegExp(`^\\s*name:\\s*["']?${objectName}["']?\\s*$`, 'm');
|
|
212
|
+
if (regex.test(content)) {
|
|
213
|
+
foundFile = file;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (foundFile) {
|
|
219
|
+
res.setHeader('Content-Type', 'application/json');
|
|
220
|
+
res.end(JSON.stringify({ file: foundFile }));
|
|
221
|
+
} else {
|
|
222
|
+
res.statusCode = 404;
|
|
223
|
+
res.end(JSON.stringify({ error: 'Object definition file not found' }));
|
|
224
|
+
}
|
|
225
|
+
} catch (e: any) {
|
|
226
|
+
res.statusCode = 500;
|
|
227
|
+
res.end(JSON.stringify({ error: e.message }));
|
|
228
|
+
}
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (req.url?.startsWith('/api/metadata')) {
|
|
233
|
+
return metadataHandler(req, res);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (req.url?.startsWith('/api')) {
|
|
237
|
+
// Strip /api prefix if needed by the handler,
|
|
238
|
+
// but ObjectQL node handler usually expects full path or depends on internal routing.
|
|
239
|
+
// Actually createNodeHandler handles /objectql/v1/ etc?
|
|
240
|
+
// Let's assume standard behavior: pass to handler
|
|
241
|
+
return nodeHandler(req, res);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Redirect root to studio
|
|
245
|
+
if (req.url === '/') {
|
|
246
|
+
res.writeHead(302, { 'Location': '/studio' });
|
|
247
|
+
res.end();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
res.statusCode = 404;
|
|
252
|
+
res.end('Not Found');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
server.listen(port, () => {
|
|
256
|
+
const url = `http://localhost:${port}/studio`;
|
|
257
|
+
console.log(chalk.green(`\n🚀 Studio running at: ${chalk.bold(url)}`));
|
|
258
|
+
console.log(chalk.gray(` API endpoint: http://localhost:${port}/api`));
|
|
259
|
+
|
|
260
|
+
if (options.open) {
|
|
261
|
+
openBrowser(url);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Command } from 'commander';
|
|
|
2
2
|
import { generateTypes } from './commands/generate';
|
|
3
3
|
import { startRepl } from './commands/repl';
|
|
4
4
|
import { serve } from './commands/serve';
|
|
5
|
+
import { startStudio } from './commands/studio';
|
|
5
6
|
|
|
6
7
|
const program = new Command();
|
|
7
8
|
|
|
@@ -44,4 +45,19 @@ program
|
|
|
44
45
|
await serve({ port: parseInt(options.port), dir: options.dir });
|
|
45
46
|
});
|
|
46
47
|
|
|
48
|
+
program
|
|
49
|
+
.command('studio')
|
|
50
|
+
.alias('ui')
|
|
51
|
+
.description('Start the ObjectQL Studio')
|
|
52
|
+
.option('-p, --port <number>', 'Port to listen on', '3000')
|
|
53
|
+
.option('-d, --dir <path>', 'Directory containing schema', '.')
|
|
54
|
+
.option('--no-open', 'Do not open browser automatically')
|
|
55
|
+
.action(async (options) => {
|
|
56
|
+
await startStudio({
|
|
57
|
+
port: parseInt(options.port),
|
|
58
|
+
dir: options.dir,
|
|
59
|
+
open: options.open
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
47
63
|
program.parse();
|