This repository was archived by the owner on Feb 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
470 lines (408 loc) · 16.9 KB
/
index.js
File metadata and controls
470 lines (408 loc) · 16.9 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import { promises as fs } from 'fs';
import path from 'path';
import yaml from 'js-yaml';
import { RouteDiscovery } from './lib/route-discovery.js';
import { SchemaDiscovery } from './lib/schema-discovery.js';
import { SpecMerger } from './lib/spec-merger.js';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
options: {
alias: 'openapi-generator'
},
/**
* Register CLI tasks.
* - openapi-generator:generate → discover, merge, and (optionally) write YAML
* - openapi-generator:validate → validate an existing spec
*/
tasks(self) {
return {
generate: {
usage: `Usage: node app openapi-generator:generate [options]
Generate OpenAPI specification for this ApostropheCMS project
Options:
--output=FILE Output file path (default: apostrophecms-openapi.yaml)
--dry-run Show what would be generated without writing files
--routes-only Only run route discovery (dry-run mode)
--schemas-only Only run schema discovery (dry-run mode)
--verbose Print full stack traces on errors
`,
/**
* Discover routes/schemas, merge with base spec, and write YAML (unless dry-run).
* Prints summaries in dry-run modes.
*
* @param {object} argv - CLI flags from Apostrophe task runner.
* Recognized flags: output, dry-run, routes-only, schemas-only, verbose
* @returns {Promise<void>}
*/
async task(argv) {
const output = argv && argv.output ? String(argv.output) : './openapi/apostrophecms-openapi.yaml';
const dryRun = !!(argv && argv['dry-run']);
const routesOnly = !!(argv && argv['routes-only']);
const schemasOnly = !!(argv && argv['schemas-only']);
const verbose = !!(argv && argv.verbose);
try {
console.log('🚀 Starting OpenAPI generation...\n');
let routes = [];
if (!schemasOnly) {
console.log('📍 Step 1: Discovering routes...');
const routeDiscovery = new RouteDiscovery(self.apos);
routes = await routeDiscovery.discoverRoutes();
console.log(` Found ${routes.length} routes`);
if (dryRun || routesOnly) {
console.log('\n📋 Route Discovery Results:');
self.printRouteSummary(routes);
}
if (routesOnly) {
console.log('\n✅ Route discovery complete (routes-only mode)');
return;
}
}
let schemas = {};
if (!routesOnly) {
console.log('\n🔍 Step 2: Discovering schemas...');
const schemaDiscovery = new SchemaDiscovery(self.apos);
schemas = await schemaDiscovery.discoverSchemas();
console.log(` Found ${Object.keys(schemas || {}).length} schemas`);
if (dryRun || schemasOnly) {
console.log('\n📋 Schema Discovery Results:');
self.printSchemaSummary(schemas);
}
if (schemasOnly) {
console.log('\n✅ Schema discovery complete (schemas-only mode)');
return;
}
}
if (dryRun) {
console.log('\n✅ Dry run complete - no files written');
return;
}
// 3) Merge with base spec and write
console.log('\n🔧 Step 3: Merging with base specification...');
const specMerger = new SpecMerger();
const baseSpecPath = new URL('./templates/base-spec.yaml', import.meta.url);
const finalSpec = await specMerger.mergeSpec(baseSpecPath, routes, schemas);
const outputPath = path.resolve(process.cwd(), output);
await fs.mkdir(path.dirname(outputPath), { recursive: true });
const yamlOutput = yaml.dump(finalSpec, { lineWidth: 120, noRefs: false });
await fs.writeFile(outputPath, yamlOutput, 'utf8');
const pathCount = finalSpec && finalSpec.paths ? Object.keys(finalSpec.paths).length : 0;
const schemaCount = finalSpec && finalSpec.components && finalSpec.components.schemas
? Object.keys(finalSpec.components.schemas).length
: 0;
console.log(`\n✅ OpenAPI specification generated: ${outputPath}`);
console.log(`📊 Final spec contains:`);
console.log(` • ${pathCount} paths`);
console.log(` • ${schemaCount} schemas`);
} catch (error) {
console.error('\n❌ Error generating OpenAPI spec:', error && error.message ? error.message : String(error));
if (verbose && error && error.stack) {
console.error(error.stack);
}
process.exit(1);
}
}
},
validate: {
usage: `Validate an existing OpenAPI specification
Options:
--spec=FILE Path to spec file (YAML or JSON). Default: apostrophecms-openapi.yaml
`,
/**
* Validate an existing OpenAPI document at the provided path.
* Exits with non-zero code on validation errors.
*
* @param {object} argv
* @returns {Promise<void>}
*/
async task(argv) {
const specPath = argv && argv.spec ? String(argv.spec) : './openapi/apostrophecms-openapi.yaml';
try {
const spec = await self.loadSpecFile(specPath);
const validation = await self.validateSpec(spec);
if (validation && validation.valid) {
console.log('✅ OpenAPI specification is valid');
} else {
const errs = (validation && validation.errors) || [];
console.log('❌ OpenAPI specification has errors:');
errs.forEach(e => {
const msg = e && e.message ? e.message : 'Unknown error';
const at = e && e.instancePath ? ` (${e.instancePath})` : '';
console.log(` - ${msg}${at}`);
});
process.exit(1);
}
} catch (error) {
console.error('❌ Failed to validate specification:', error && error.message ? error.message : String(error));
process.exit(1);
}
}
},
docs: {
usage: `Usage: node app openapi-generator:docs [options]
Serve OpenAPI specification documentation in a web browser
Options:
--open Automatically open the documentation in your default browser
`,
async task(argv) {
const scriptPath = path.join(__dirname, '/scripts/serve-docs.js');
const args = [scriptPath, '--spec', path.join(self.apos.rootDir, 'openapi/apostrophecms-openapi.yaml')];
if (argv.open) {
args.push('--open');
}
const child = spawn('node', args, {
stdio: 'inherit',
cwd: self.apos.rootDir
});
return new Promise((resolve, reject) => {
child.on('close', (code) => {
if (code !== 0) {
reject(new Error(`Process exited with code ${code}`));
} else {
resolve();
}
});
});
}
},
generateSDK: {
usage: `Usage: node app openapi-generator:generateSDK <language> [options]
Generate client SDK from OpenAPI specification
Arguments:
<language> Language/generator to use. Preset options: typescript, python, php
Or any openapi-generator-cli generator name for custom generation
Options:
--output=DIR Output directory (default: ./generated/<language>)
--props=PROPS Additional properties as comma-separated key=value pairs
--config=FILE Use configuration file for generator settings
--global-property=PROPS Global properties for the generator
--import-mappings=MAP Import mappings for the generator
--type-mappings=MAP Type mappings for the generator
Examples:
node app openapi-generator:generateSDK typescript
node app openapi-generator:generateSDK python --output ./my-python-client
node app openapi-generator:generateSDK java --props "groupId=com.example,artifactId=apostrophe-client"
node app openapi-generator:generateSDK kotlin --config ./kotlin-config.json
`,
async task(argv) {
const language = argv._[1];
const outputDir = argv.output || `./generated/${language}`;
const specPath = path.join(self.apos.rootDir, 'openapi/apostrophecms-openapi.yaml');
if (!language) {
throw new Error('Please specify a language/generator');
}
// Check if OpenAPI spec file exists
try {
await fs.access(specPath);
} catch (error) {
throw new Error(`OpenAPI spec file not found at ${specPath}. Please run 'node app openapi-generator:generate' first.`);
}
// Predefined configurations for common languages
const presets = {
typescript: {
generator: 'typescript-axios',
additionalProperties: 'npmName=apostrophecms-client,supportsES6=true'
},
python: {
generator: 'python',
additionalProperties: 'packageName=apostrophecms_client'
},
php: {
generator: 'php',
additionalProperties: 'packageName=ApostropheCMS,invokerPackage=ApostropheCMS,composerVendorName=apostrophecms,composerPackageName=api-client'
}
};
let generator, additionalProperties;
if (presets[language]) {
// Use preset configuration
generator = presets[language].generator;
additionalProperties = presets[language].additionalProperties;
console.log(`Using preset configuration for ${language}`);
} else {
// Treat language as the generator name directly
generator = language;
additionalProperties = argv['additional-properties'] || argv.props || '';
console.log(`Using custom generator: ${generator}`);
}
const finalOutputDir = path.resolve(self.apos.rootDir, outputDir.replace(/^\.\//, ''));
const args = [
'generate',
'-i', specPath,
'-g', generator,
'-o', finalOutputDir
];
if (additionalProperties) {
args.push('--additional-properties', additionalProperties);
}
// Allow other openapi-generator-cli options to be passed through
if (argv['global-property']) {
args.push('--global-property', argv['global-property']);
}
if (argv['import-mappings']) {
args.push('--import-mappings', argv['import-mappings']);
}
if (argv['type-mappings']) {
args.push('--type-mappings', argv['type-mappings']);
}
if (argv.config) {
args.push('-c', argv.config);
}
console.log(`Generating SDK with generator "${generator}" to ${outputDir}...`);
// Create output directory if it doesn't exist
const resolvedOutputDir = path.resolve(outputDir);
await fs.mkdir(resolvedOutputDir, { recursive: true });
console.log(`Created output directory: ${resolvedOutputDir}`);
// Try multiple approaches to run the generator
const approaches = [
// Approach 1: Use globally installed CLI
() => {
const child = spawn('openapi-generator-cli', args, {
stdio: 'inherit',
cwd: self.apos.rootDir
});
return child;
},
// Approach 2: Use npx with explicit package name
() => {
const npxBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
const child = spawn(npxBin, ['--yes', '@openapitools/openapi-generator-cli', ...args], {
stdio: 'inherit',
cwd: self.apos.rootDir
});
return child;
},
// Approach 3: Use Docker (if available)
() => {
const dockerArgs = [
'run', '--rm',
'-v', `${self.apos.rootDir}:/local`,
'openapitools/openapi-generator-cli',
...args.map(arg => arg.replace(self.apos.rootDir, '/local'))
];
const child = spawn('docker', dockerArgs, {
stdio: 'inherit',
cwd: self.apos.rootDir
});
return child;
}
];
for (let i = 0; i < approaches.length; i++) {
try {
const child = approaches[i]();
const result = await new Promise((resolve, reject) => {
child.on('close', (code) => {
resolve(code);
});
child.on('error', (error) => {
reject(error);
});
});
if (result === 0) {
console.log(`✅ SDK generated successfully in ${outputDir}`);
return;
}
} catch (error) {
console.log(`Approach ${i + 1} failed: ${error.message}`);
if (i === approaches.length - 1) {
// All approaches failed
throw new Error(`Failed to generate SDK. Please ensure one of the following is available:
1. Install globally: npm install -g @openapitools/openapi-generator-cli
2. Ensure npx is working properly
3. Install Docker and use the openapitools/openapi-generator-cli image
Error details: ${error.message}`);
}
}
}
}
}
}
},
/**
* Helper methods used by tasks.
*/
methods(self) {
return {
/**
* Print a grouped summary of discovered routes.
* Groups by first path segment and honors base spec order
*
* @param {Array<{ method: string, path: string, module?: string }>} routes
*/
printRouteSummary(routes) {
if (!Array.isArray(routes) || routes.length === 0) {
console.log(' (no routes)');
return;
}
const groups = new Map();
for (const route of routes) {
const key = (route && route.path ? route.path : '/')
.replace(/^\/+/, '')
.split('/')[0] || '(root)';
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(route);
}
for (const [resource, list] of groups.entries()) {
console.log(`\n ${resource}:`);
list.forEach(route => {
const method = (route && route.method ? route.method : '').toUpperCase().padEnd(6);
const path = route && route.path ? route.path : '';
console.log(` ${method} ${path}`);
});
}
},
/**
* Print a compact summary of discovered schemas and their field counts.
*
* @param {Object<string, { properties?: Object<string, any> }>} schemas
*/
printSchemaSummary(schemas) {
if (!schemas || typeof schemas !== 'object') {
console.log(' (no schemas)');
return;
}
const entries = Object.entries(schemas);
if (entries.length === 0) {
console.log(' (no schemas)');
return;
}
for (const [name, schema] of entries) {
const fields = schema && schema.properties ? Object.keys(schema.properties).length : 0;
console.log(` 📋 ${name} (${fields} fields)`);
}
},
/**
* Load an OpenAPI spec from disk (YAML or JSON) and parse it.
*
* @param {string} filePath
* @returns {Promise<any>}
*/
async loadSpecFile(filePath) {
const content = await fs.readFile(filePath, 'utf8');
return filePath.endsWith('.json') ? JSON.parse(content) : yaml.load(content);
},
/**
* Validate an OpenAPI spec object using @apidevtools/swagger-parser.
*
* @param {any} spec
* @returns {Promise<{ valid: boolean, errors: Array<{ message: string, instancePath: string }> }>}
*/
async validateSpec(spec) {
const { default: SwaggerParser } = await import('@apidevtools/swagger-parser');
try {
// Deep clone to avoid mutations by the validator.
await SwaggerParser.validate(JSON.parse(JSON.stringify(spec)));
return { valid: true, errors: [] };
} catch (e) {
const details = (e && e.details) ? e.details : [e];
const errors = details.map(d => ({
message: (d && d.message) ? d.message : (e && e.message) ? e.message : 'Validation error',
instancePath: d && d.path ? '/' + d.path.join('/') : ''
}));
return { valid: false, errors };
}
}
};
}
};