@objectql/cli 1.8.3 → 1.9.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.
Files changed (94) hide show
  1. package/README.md +157 -1
  2. package/dist/commands/ai.js +4 -3
  3. package/dist/commands/ai.js.map +1 -1
  4. package/dist/commands/build.d.ts +12 -0
  5. package/dist/commands/build.js +119 -0
  6. package/dist/commands/build.js.map +1 -0
  7. package/dist/commands/database-push.d.ts +5 -0
  8. package/dist/commands/database-push.js +15 -0
  9. package/dist/commands/database-push.js.map +1 -0
  10. package/dist/commands/dev.d.ts +11 -0
  11. package/dist/commands/dev.js +111 -0
  12. package/dist/commands/dev.js.map +1 -0
  13. package/dist/commands/doctor.d.ts +4 -0
  14. package/dist/commands/doctor.js +37 -0
  15. package/dist/commands/doctor.js.map +1 -0
  16. package/dist/commands/format.d.ts +9 -0
  17. package/dist/commands/format.js +137 -0
  18. package/dist/commands/format.js.map +1 -0
  19. package/dist/commands/init.js +31 -30
  20. package/dist/commands/init.js.map +1 -1
  21. package/dist/commands/lint.d.ts +9 -0
  22. package/dist/commands/lint.js +120 -0
  23. package/dist/commands/lint.js.map +1 -0
  24. package/dist/commands/new.js +0 -52
  25. package/dist/commands/new.js.map +1 -1
  26. package/dist/commands/serve.d.ts +2 -0
  27. package/dist/commands/serve.js +122 -46
  28. package/dist/commands/serve.js.map +1 -1
  29. package/dist/commands/start.d.ts +12 -0
  30. package/dist/commands/start.js +134 -0
  31. package/dist/commands/start.js.map +1 -0
  32. package/dist/commands/test.d.ts +10 -0
  33. package/dist/commands/test.js +120 -0
  34. package/dist/commands/test.js.map +1 -0
  35. package/dist/index.js +189 -149
  36. package/dist/index.js.map +1 -1
  37. package/package.json +13 -8
  38. package/templates/hello-world/.vscode/extensions.json +7 -0
  39. package/templates/hello-world/CHANGELOG.md +41 -0
  40. package/templates/hello-world/README.md +29 -0
  41. package/templates/hello-world/package.json +24 -0
  42. package/templates/hello-world/src/index.ts +58 -0
  43. package/templates/hello-world/tsconfig.json +10 -0
  44. package/templates/starter/.vscode/extensions.json +7 -0
  45. package/{CHANGELOG.md → templates/starter/CHANGELOG.md} +47 -41
  46. package/templates/starter/README.md +17 -0
  47. package/templates/starter/__tests__/projects-hooks-actions.test.ts +490 -0
  48. package/templates/starter/jest.config.js +16 -0
  49. package/templates/starter/package.json +52 -0
  50. package/templates/starter/src/README.pages.md +110 -0
  51. package/templates/starter/src/demo.app.yml +4 -0
  52. package/templates/starter/src/i18n/zh-CN/projects.json +22 -0
  53. package/templates/starter/src/modules/kitchen-sink/kitchen_sink.data.yml +18 -0
  54. package/templates/starter/src/modules/kitchen-sink/kitchen_sink.object.yml +156 -0
  55. package/templates/starter/src/modules/projects/project_approval.workflow.yml +51 -0
  56. package/templates/starter/src/modules/projects/projects.action.ts +472 -0
  57. package/templates/starter/src/modules/projects/projects.data.yml +13 -0
  58. package/templates/starter/src/modules/projects/projects.hook.ts +339 -0
  59. package/templates/starter/src/modules/projects/projects.object.yml +148 -0
  60. package/templates/starter/src/modules/projects/projects.permission.yml +141 -0
  61. package/templates/starter/src/modules/projects/projects.validation.yml +37 -0
  62. package/templates/starter/src/modules/tasks/tasks.data.yml +23 -0
  63. package/templates/starter/src/modules/tasks/tasks.object.yml +34 -0
  64. package/templates/starter/src/modules/tasks/tasks.permission.yml +167 -0
  65. package/templates/starter/src/seed.ts +55 -0
  66. package/templates/starter/src/types/index.ts +3 -0
  67. package/templates/starter/src/types/kitchen_sink.ts +101 -0
  68. package/templates/starter/src/types/projects.ts +49 -0
  69. package/templates/starter/src/types/tasks.ts +33 -0
  70. package/templates/starter/tsconfig.json +11 -0
  71. package/templates/starter/tsconfig.tsbuildinfo +1 -0
  72. package/AI_EXAMPLES.md +0 -154
  73. package/AI_IMPLEMENTATION_SUMMARY.md +0 -509
  74. package/AI_TUTORIAL.md +0 -144
  75. package/IMPLEMENTATION_SUMMARY.md +0 -437
  76. package/USAGE_EXAMPLES.md +0 -951
  77. package/__tests__/commands.test.ts +0 -316
  78. package/dist/commands/studio.d.ts +0 -5
  79. package/dist/commands/studio.js +0 -364
  80. package/dist/commands/studio.js.map +0 -1
  81. package/jest.config.js +0 -19
  82. package/src/commands/ai.ts +0 -508
  83. package/src/commands/generate.ts +0 -135
  84. package/src/commands/i18n.ts +0 -303
  85. package/src/commands/init.ts +0 -191
  86. package/src/commands/migrate.ts +0 -314
  87. package/src/commands/new.ts +0 -273
  88. package/src/commands/repl.ts +0 -120
  89. package/src/commands/serve.ts +0 -96
  90. package/src/commands/studio.ts +0 -354
  91. package/src/commands/sync.ts +0 -328
  92. package/src/index.ts +0 -274
  93. package/tsconfig.json +0 -15
  94. package/tsconfig.tsbuildinfo +0 -1
@@ -1,316 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import { newMetadata } from '../src/commands/new';
4
- import { i18nExtract, i18nInit, i18nValidate } from '../src/commands/i18n';
5
- import { syncDatabase } from '../src/commands/sync';
6
- import { ObjectQL } from '@objectql/core';
7
- import { SqlDriver } from '@objectql/driver-sql';
8
- import * as yaml from 'js-yaml';
9
-
10
- describe('CLI Commands', () => {
11
- const testDir = path.join(__dirname, '__test_output__');
12
-
13
- beforeEach(() => {
14
- // Create test directory
15
- if (!fs.existsSync(testDir)) {
16
- fs.mkdirSync(testDir, { recursive: true });
17
- }
18
- });
19
-
20
- afterEach(() => {
21
- // Clean up test directory
22
- if (fs.existsSync(testDir)) {
23
- fs.rmSync(testDir, { recursive: true, force: true });
24
- }
25
- });
26
-
27
- describe('new command', () => {
28
- it('should create an object file', async () => {
29
- await newMetadata({
30
- type: 'object',
31
- name: 'test_users',
32
- dir: testDir
33
- });
34
-
35
- const filePath = path.join(testDir, 'test_users.object.yml');
36
- expect(fs.existsSync(filePath)).toBe(true);
37
-
38
- const content = fs.readFileSync(filePath, 'utf-8');
39
- expect(content).toContain('label: Test Users');
40
- expect(content).toContain('type: text');
41
- });
42
-
43
- it('should create action with both yml and ts files', async () => {
44
- await newMetadata({
45
- type: 'action',
46
- name: 'test_action',
47
- dir: testDir
48
- });
49
-
50
- const ymlPath = path.join(testDir, 'test_action.action.yml');
51
- const tsPath = path.join(testDir, 'test_action.action.ts');
52
-
53
- expect(fs.existsSync(ymlPath)).toBe(true);
54
- expect(fs.existsSync(tsPath)).toBe(true);
55
-
56
- const tsContent = fs.readFileSync(tsPath, 'utf-8');
57
- expect(tsContent).toContain('action_test_action');
58
- expect(tsContent).toContain('ActionContext');
59
- });
60
-
61
- it('should create hook with both yml and ts files', async () => {
62
- await newMetadata({
63
- type: 'hook',
64
- name: 'test_hook',
65
- dir: testDir
66
- });
67
-
68
- const ymlPath = path.join(testDir, 'test_hook.hook.yml');
69
- const tsPath = path.join(testDir, 'test_hook.hook.ts');
70
-
71
- expect(fs.existsSync(ymlPath)).toBe(true);
72
- expect(fs.existsSync(tsPath)).toBe(true);
73
-
74
- const tsContent = fs.readFileSync(tsPath, 'utf-8');
75
- expect(tsContent).toContain('beforeInsert');
76
- expect(tsContent).toContain('afterInsert');
77
- });
78
-
79
- // Skip this test as it calls process.exit which causes test failures
80
- it.skip('should validate object name format', async () => {
81
- await expect(
82
- newMetadata({
83
- type: 'object',
84
- name: 'InvalidName', // Should be lowercase
85
- dir: testDir
86
- })
87
- ).rejects.toThrow();
88
- });
89
- });
90
-
91
- describe('i18n commands', () => {
92
- beforeEach(async () => {
93
- // Create a test object file
94
- await newMetadata({
95
- type: 'object',
96
- name: 'test_users',
97
- dir: testDir
98
- });
99
- });
100
-
101
- it('should extract translatable strings', async () => {
102
- await i18nExtract({
103
- source: testDir,
104
- output: path.join(testDir, 'i18n'),
105
- lang: 'en'
106
- });
107
-
108
- const i18nFile = path.join(testDir, 'i18n/en/test_users.json');
109
- expect(fs.existsSync(i18nFile)).toBe(true);
110
-
111
- const content = JSON.parse(fs.readFileSync(i18nFile, 'utf-8'));
112
- expect(content.label).toBe('Test Users');
113
- expect(content.fields.name.label).toBe('Name');
114
- });
115
-
116
- it('should initialize new language', async () => {
117
- await i18nInit({
118
- lang: 'zh-CN',
119
- baseDir: path.join(testDir, 'i18n')
120
- });
121
-
122
- const langDir = path.join(testDir, 'i18n/zh-CN');
123
- expect(fs.existsSync(langDir)).toBe(true);
124
-
125
- const commonFile = path.join(langDir, 'common.json');
126
- expect(fs.existsSync(commonFile)).toBe(true);
127
- });
128
-
129
- it('should validate translations', async () => {
130
- // Extract for base language
131
- await i18nExtract({
132
- source: testDir,
133
- output: path.join(testDir, 'i18n'),
134
- lang: 'en'
135
- });
136
-
137
- // Extract for target language
138
- await i18nInit({
139
- lang: 'zh-CN',
140
- baseDir: path.join(testDir, 'i18n')
141
- });
142
- await i18nExtract({
143
- source: testDir,
144
- output: path.join(testDir, 'i18n'),
145
- lang: 'zh-CN'
146
- });
147
-
148
- // Should not throw - validation passes
149
- await expect(
150
- i18nValidate({
151
- lang: 'zh-CN',
152
- baseDir: path.join(testDir, 'i18n'),
153
- baseLang: 'en'
154
- })
155
- ).resolves.not.toThrow();
156
- });
157
- });
158
-
159
- describe('sync command', () => {
160
- let app: ObjectQL;
161
- let configPath: string;
162
-
163
- beforeEach(async () => {
164
- // Create a test SQLite database with sample schema
165
- const dbPath = path.join(testDir, `test_${Date.now()}.db`);
166
- const driver = new SqlDriver({
167
- client: 'sqlite3',
168
- connection: { filename: dbPath },
169
- useNullAsDefault: true,
170
- pool: {
171
- min: 1,
172
- max: 1 // Single connection for test
173
- }
174
- });
175
-
176
- app = new ObjectQL({
177
- datasources: { default: driver }
178
- });
179
-
180
- // Register sample objects
181
- app.registerObject({
182
- name: 'users',
183
- label: 'Users',
184
- fields: {
185
- username: { type: 'string', required: true, unique: true },
186
- email: { type: 'email', required: true },
187
- is_active: { type: 'boolean', defaultValue: true }
188
- }
189
- });
190
-
191
- app.registerObject({
192
- name: 'posts',
193
- label: 'Posts',
194
- fields: {
195
- title: { type: 'text', required: true },
196
- content: { type: 'textarea' },
197
- author_id: { type: 'lookup', reference_to: 'users' },
198
- published_at: { type: 'datetime' }
199
- }
200
- });
201
-
202
- await app.init();
203
- });
204
-
205
- afterEach(async () => {
206
- try {
207
- if (app) await app.close();
208
- } catch (e) {
209
- // Ignore if already closed
210
- }
211
- });
212
-
213
- it('should introspect database and generate .object.yml files', async () => {
214
- const outputDir = path.join(testDir, 'objects');
215
-
216
- await syncDatabase({
217
- app: app,
218
- output: outputDir
219
- });
220
-
221
- // Check that files were created
222
- expect(fs.existsSync(path.join(outputDir, 'users.object.yml'))).toBe(true);
223
- expect(fs.existsSync(path.join(outputDir, 'posts.object.yml'))).toBe(true);
224
-
225
- // Verify users.object.yml content
226
- const usersContent = fs.readFileSync(path.join(outputDir, 'users.object.yml'), 'utf-8');
227
- const usersObj = yaml.load(usersContent) as any;
228
-
229
- expect(usersObj.name).toBe('users');
230
- expect(usersObj.label).toBe('Users');
231
- expect(usersObj.fields.username).toBeDefined();
232
- expect(usersObj.fields.username.type).toBe('text');
233
- expect(usersObj.fields.username.required).toBe(true);
234
- expect(usersObj.fields.username.unique).toBe(true);
235
- expect(usersObj.fields.email).toBeDefined();
236
- expect(usersObj.fields.email.type).toBe('text');
237
-
238
- // Verify posts.object.yml content
239
- const postsContent = fs.readFileSync(path.join(outputDir, 'posts.object.yml'), 'utf-8');
240
- const postsObj = yaml.load(postsContent) as any;
241
-
242
- expect(postsObj.name).toBe('posts');
243
- expect(postsObj.label).toBe('Posts');
244
- expect(postsObj.fields.title).toBeDefined();
245
- expect(postsObj.fields.content).toBeDefined();
246
- // Foreign key should be detected as lookup
247
- expect(postsObj.fields.author_id).toBeDefined();
248
- expect(postsObj.fields.author_id.type).toBe('lookup');
249
- expect(postsObj.fields.author_id.reference_to).toBe('users');
250
- });
251
-
252
- it('should support selective table syncing', async () => {
253
- const outputDir = path.join(testDir, 'objects_selective');
254
-
255
- await syncDatabase({
256
- app: app,
257
- output: outputDir,
258
- tables: ['users']
259
- });
260
-
261
- // Only users.object.yml should be created
262
- expect(fs.existsSync(path.join(outputDir, 'users.object.yml'))).toBe(true);
263
- expect(fs.existsSync(path.join(outputDir, 'posts.object.yml'))).toBe(false);
264
- });
265
-
266
- it('should skip existing files without --force flag', async () => {
267
- const outputDir = path.join(testDir, 'objects_skip');
268
-
269
- // First sync
270
- await syncDatabase({
271
- app: app,
272
- output: outputDir
273
- });
274
-
275
- // Modify an existing file
276
- const usersPath = path.join(outputDir, 'users.object.yml');
277
- const originalContent = fs.readFileSync(usersPath, 'utf-8');
278
- fs.writeFileSync(usersPath, '# Modified content\n' + originalContent, 'utf-8');
279
-
280
- // Second sync without force - should skip
281
- await syncDatabase({
282
- app: app,
283
- output: outputDir
284
- });
285
-
286
- const modifiedContent = fs.readFileSync(usersPath, 'utf-8');
287
- expect(modifiedContent).toContain('# Modified content');
288
- });
289
-
290
- it('should overwrite files with --force flag', async () => {
291
- const outputDir = path.join(testDir, 'objects_force');
292
-
293
- // First sync
294
- await syncDatabase({
295
- app: app,
296
- output: outputDir
297
- });
298
-
299
- // Modify an existing file
300
- const usersPath = path.join(outputDir, 'users.object.yml');
301
- fs.writeFileSync(usersPath, '# Modified content\nname: users', 'utf-8');
302
-
303
- // Second sync with force - should overwrite
304
- await syncDatabase({
305
- app: app,
306
- output: outputDir,
307
- force: true
308
- });
309
-
310
- const newContent = fs.readFileSync(usersPath, 'utf-8');
311
- expect(newContent).not.toContain('# Modified content');
312
- expect(newContent).toContain('name: users');
313
- expect(newContent).toContain('fields:');
314
- });
315
- });
316
- });
@@ -1,5 +0,0 @@
1
- export declare function startStudio(options: {
2
- port: number;
3
- dir: string;
4
- open?: boolean;
5
- }): Promise<void>;
@@ -1,364 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.startStudio = startStudio;
40
- const core_1 = require("@objectql/core");
41
- const platform_node_1 = require("@objectql/platform-node");
42
- const server_1 = require("@objectql/server");
43
- const http_1 = require("http");
44
- const path = __importStar(require("path"));
45
- const fs = __importStar(require("fs"));
46
- const chalk_1 = __importDefault(require("chalk"));
47
- const child_process_1 = require("child_process");
48
- const ts_node_1 = require("ts-node");
49
- const fast_glob_1 = __importDefault(require("fast-glob"));
50
- const SWAGGER_HTML = `
51
- <!DOCTYPE html>
52
- <html lang="en">
53
- <head>
54
- <meta charset="utf-8" />
55
- <meta name="viewport" content="width=device-width, initial-scale=1" />
56
- <title>ObjectQL Swagger UI</title>
57
- <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css" />
58
- <style>
59
- body { margin: 0; padding: 0; }
60
- </style>
61
- </head>
62
- <body>
63
- <div id="swagger-ui"></div>
64
- <script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js" crossorigin></script>
65
- <script>
66
- window.onload = () => {
67
- window.ui = SwaggerUIBundle({
68
- url: '/openapi.json',
69
- dom_id: '#swagger-ui',
70
- });
71
- };
72
- </script>
73
- </body>
74
- </html>
75
- `;
76
- function openBrowser(url) {
77
- const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
78
- (0, child_process_1.exec)(`${start} ${url}`);
79
- }
80
- async function startStudio(options) {
81
- const startPort = options.port || 5555;
82
- const rootDir = path.resolve(process.cwd(), options.dir || '.');
83
- console.log(chalk_1.default.blue('Starting ObjectQL Studio...'));
84
- console.log(chalk_1.default.gray(`Project Root: ${rootDir}`));
85
- // Register ts-node
86
- (0, ts_node_1.register)({
87
- transpileOnly: true,
88
- compilerOptions: {
89
- module: "commonjs"
90
- }
91
- });
92
- let app;
93
- const configTs = path.join(rootDir, 'objectql.config.ts');
94
- const configJs = path.join(rootDir, 'objectql.config.js');
95
- if (fs.existsSync(configTs)) {
96
- console.log(chalk_1.default.gray(`Loading config from ${configTs}`));
97
- const mod = require(configTs);
98
- app = mod.default || mod;
99
- }
100
- else if (fs.existsSync(configJs)) {
101
- console.log(chalk_1.default.gray(`Loading config from ${configJs}`));
102
- const mod = require(configJs);
103
- app = mod.default || mod;
104
- }
105
- else {
106
- console.error(chalk_1.default.red('\n❌ Error: Configuration file (objectql.config.ts) not found.'));
107
- process.exit(1);
108
- }
109
- if (!app) {
110
- console.error(chalk_1.default.red('\n❌ Error: No default export found in configuration file.'));
111
- process.exit(1);
112
- }
113
- // Initialize App if it's a configuration object
114
- if (typeof app.init !== 'function') {
115
- const config = app;
116
- console.log(chalk_1.default.gray('Configuration object detected. Initializing ObjectQL instance...'));
117
- const datasources = {};
118
- if (config.datasource && config.datasource.default) {
119
- const dbConfig = config.datasource.default;
120
- if (dbConfig.type === 'sqlite') {
121
- try {
122
- const { SqlDriver } = require('@objectql/driver-sql');
123
- datasources.default = new SqlDriver({
124
- client: 'sqlite3',
125
- connection: {
126
- filename: dbConfig.filename ? path.resolve(rootDir, dbConfig.filename) : ':memory:'
127
- },
128
- useNullAsDefault: true
129
- });
130
- }
131
- catch (e) {
132
- console.warn(chalk_1.default.yellow('Failed to load @objectql/driver-sql. Ensure it is installed.'));
133
- }
134
- }
135
- }
136
- // Fallback to memory if no datasource
137
- if (!datasources.default) {
138
- console.warn(chalk_1.default.yellow('No valid datasource found. Using in-memory SQLite.'));
139
- const { SqlDriver } = require('@objectql/driver-sql');
140
- datasources.default = new SqlDriver({
141
- client: 'sqlite3',
142
- connection: { filename: ':memory:' },
143
- useNullAsDefault: true
144
- });
145
- }
146
- app = new core_1.ObjectQL({ datasources });
147
- // Load Schema
148
- const loader = new platform_node_1.ObjectLoader(app.metadata);
149
- // Load Presets
150
- if (Array.isArray(config.presets)) {
151
- console.log(chalk_1.default.gray(`Loading ${config.presets.length} presets...`));
152
- for (const preset of config.presets) {
153
- try {
154
- loader.loadPackage(preset);
155
- }
156
- catch (e) {
157
- console.warn(chalk_1.default.yellow(`Failed to load preset ${preset}:`), e.message);
158
- }
159
- }
160
- }
161
- // Load Schema from Directory
162
- // In a monorepo root with presets, scanning everything is dangerous.
163
- // We check if we are in a monorepo-like environment.
164
- const isMonorepoRoot = fs.existsSync(path.join(rootDir, 'pnpm-workspace.yaml')) || fs.existsSync(path.join(rootDir, 'pnpm-lock.yaml'));
165
- // If we are in a likely monorepo root AND have presets, skip recursive scan
166
- if (isMonorepoRoot && config.presets && config.presets.length > 0) {
167
- console.log(chalk_1.default.yellow('Monorepo root detected with presets. Skipping recursive file scan of root directory to avoid conflicts.'));
168
- }
169
- else {
170
- loader.load(rootDir);
171
- }
172
- }
173
- // 2. Load Schema & Init
174
- try {
175
- await app.init();
176
- }
177
- catch (e) {
178
- console.error(chalk_1.default.red('❌ Failed to initialize application:'), e.message);
179
- process.exit(1);
180
- }
181
- // 3. Setup HTTP Server
182
- const nodeHandler = (0, server_1.createNodeHandler)(app);
183
- const studioHandler = (0, server_1.createStudioHandler)();
184
- const metadataHandler = (0, server_1.createMetadataHandler)(app);
185
- const server = (0, http_1.createServer)(async (req, res) => {
186
- var _a, _b, _c, _d, _e, _f;
187
- // CORS
188
- res.setHeader('Access-Control-Allow-Origin', '*');
189
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
190
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
191
- if (req.method === 'OPTIONS') {
192
- res.writeHead(200);
193
- res.end();
194
- return;
195
- }
196
- if (req.url === '/openapi.json') {
197
- return nodeHandler(req, res);
198
- }
199
- if (req.url === '/swagger') {
200
- res.writeHead(200, { 'Content-Type': 'text/html' });
201
- res.end(SWAGGER_HTML);
202
- return;
203
- }
204
- // Routing
205
- if ((_a = req.url) === null || _a === void 0 ? void 0 : _a.startsWith('/studio')) {
206
- return studioHandler(req, res);
207
- }
208
- if ((_b = req.url) === null || _b === void 0 ? void 0 : _b.startsWith('/api/schema/files')) {
209
- // List all metadata files
210
- try {
211
- // Find all *.*.yml files relative to rootDir
212
- // Note: User might have configured objectql with specific source paths.
213
- // We ignore common build folders to avoid duplicates/editing compiled files.
214
- // We broadly match all objectql-like files: *.object.yml, *.view.yml, etc.
215
- const files = await (0, fast_glob_1.default)('**/*.{object,app,view,permission,report,validation,workflow,form,data,hook,action}.yml', {
216
- cwd: rootDir,
217
- ignore: ['node_modules/**', 'dist/**', 'build/**', 'out/**', '.git/**', '.next/**']
218
- });
219
- res.setHeader('Content-Type', 'application/json');
220
- res.end(JSON.stringify({ files }));
221
- }
222
- catch (e) {
223
- res.statusCode = 500;
224
- res.end(JSON.stringify({ error: e.message }));
225
- }
226
- return;
227
- }
228
- if ((_c = req.url) === null || _c === void 0 ? void 0 : _c.startsWith('/api/schema/content')) {
229
- const urlObj = new URL(req.url, `http://${req.headers.host}`);
230
- const file = urlObj.searchParams.get('file');
231
- if (!file) {
232
- res.statusCode = 400;
233
- res.end(JSON.stringify({ error: 'Missing file parameter' }));
234
- return;
235
- }
236
- const filePath = path.join(rootDir, file);
237
- // Security check
238
- if (!filePath.startsWith(rootDir)) {
239
- res.statusCode = 403;
240
- res.end(JSON.stringify({ error: 'Access denied' }));
241
- return;
242
- }
243
- if (req.method === 'GET') {
244
- try {
245
- const content = fs.readFileSync(filePath, 'utf-8');
246
- res.setHeader('Content-Type', 'text/plain'); // Plain text (YAML)
247
- res.end(content);
248
- }
249
- catch (e) {
250
- res.statusCode = 404;
251
- res.end(JSON.stringify({ error: 'File not found' }));
252
- }
253
- return;
254
- }
255
- if (req.method === 'POST') {
256
- let body = '';
257
- req.on('data', chunk => body += chunk);
258
- req.on('end', () => {
259
- try {
260
- fs.writeFileSync(filePath, body, 'utf-8');
261
- res.statusCode = 200;
262
- res.end(JSON.stringify({ success: true }));
263
- }
264
- catch (e) {
265
- res.statusCode = 500;
266
- res.end(JSON.stringify({ error: e.message }));
267
- }
268
- });
269
- return;
270
- }
271
- }
272
- if ((_d = req.url) === null || _d === void 0 ? void 0 : _d.startsWith('/api/schema/find')) {
273
- const urlObj = new URL(req.url, `http://${req.headers.host}`);
274
- const objectName = urlObj.searchParams.get('object');
275
- if (!objectName) {
276
- res.statusCode = 400;
277
- res.end(JSON.stringify({ error: 'Missing object parameter' }));
278
- return;
279
- }
280
- try {
281
- // Find all object.yml files
282
- const files = await (0, fast_glob_1.default)('**/*.object.yml', {
283
- cwd: rootDir,
284
- ignore: ['node_modules/**', 'dist/**', 'build/**', 'out/**', '.git/**', '.next/**']
285
- });
286
- let foundFile = null;
287
- // Naive parsing to find the object definition
288
- // We don't use the FULL parser, just checks if "name: objectName" is present
289
- for (const file of files) {
290
- const content = fs.readFileSync(path.join(rootDir, file), 'utf-8');
291
- // Simple check: name: <objectName> or name: "<objectName>"
292
- // This creates a regex that looks for `name:` followed by the objectName
293
- // Handles spaces, quotes
294
- const regex = new RegExp(`^\\s*name:\\s*["']?${objectName}["']?\\s*$`, 'm');
295
- if (regex.test(content)) {
296
- foundFile = file;
297
- break;
298
- }
299
- }
300
- if (foundFile) {
301
- res.setHeader('Content-Type', 'application/json');
302
- res.end(JSON.stringify({ file: foundFile }));
303
- }
304
- else {
305
- res.statusCode = 404;
306
- res.end(JSON.stringify({ error: 'Object definition file not found' }));
307
- }
308
- }
309
- catch (e) {
310
- res.statusCode = 500;
311
- res.end(JSON.stringify({ error: e.message }));
312
- }
313
- return;
314
- }
315
- if ((_e = req.url) === null || _e === void 0 ? void 0 : _e.startsWith('/api/metadata')) {
316
- return metadataHandler(req, res);
317
- }
318
- if ((_f = req.url) === null || _f === void 0 ? void 0 : _f.startsWith('/api')) {
319
- // Strip /api prefix if needed by the handler,
320
- // but ObjectQL node handler usually expects full path or depends on internal routing.
321
- // Actually createNodeHandler handles /objectql/v1/ etc?
322
- // Let's assume standard behavior: pass to handler
323
- return nodeHandler(req, res);
324
- }
325
- // Redirect root to studio
326
- if (req.url === '/') {
327
- res.writeHead(302, { 'Location': '/studio' });
328
- res.end();
329
- return;
330
- }
331
- res.statusCode = 404;
332
- res.end('Not Found');
333
- });
334
- const tryListen = (port) => {
335
- server.removeAllListeners('error');
336
- server.removeAllListeners('listening'); // Prevent stacking callbacks
337
- server.on('error', (e) => {
338
- if (e.code === 'EADDRINUSE') {
339
- if (port - startPort < 10) {
340
- console.log(chalk_1.default.yellow(`Port ${port} is in use, trying ${port + 1}...`));
341
- server.close();
342
- tryListen(port + 1);
343
- }
344
- else {
345
- console.error(chalk_1.default.red(`❌ Unable to find a free port.`));
346
- process.exit(1);
347
- }
348
- }
349
- else {
350
- console.error(chalk_1.default.red('❌ Server error:'), e);
351
- }
352
- });
353
- server.listen(port, () => {
354
- const url = `http://localhost:${port}/studio`;
355
- console.log(chalk_1.default.green(`\n🚀 Studio running at: ${chalk_1.default.bold(url)}`));
356
- console.log(chalk_1.default.gray(` API endpoint: http://localhost:${port}/api`));
357
- if (options.open) {
358
- openBrowser(url);
359
- }
360
- });
361
- };
362
- tryListen(startPort);
363
- }
364
- //# sourceMappingURL=studio.js.map