@kosdev-code/kos-nx-plugin 2.1.26 → 2.1.28

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 (39) hide show
  1. package/migrations.json +5 -0
  2. package/package.json +2 -2
  3. package/src/migrations/dev-server-support/coordinators/migration-analyzer.d.ts +13 -0
  4. package/src/migrations/dev-server-support/coordinators/migration-analyzer.d.ts.map +1 -0
  5. package/src/migrations/dev-server-support/coordinators/migration-analyzer.js +53 -0
  6. package/src/migrations/dev-server-support/coordinators/migration-analyzer.js.map +1 -0
  7. package/src/migrations/dev-server-support/coordinators/migration-executor.d.ts +13 -0
  8. package/src/migrations/dev-server-support/coordinators/migration-executor.d.ts.map +1 -0
  9. package/src/migrations/dev-server-support/coordinators/migration-executor.js +239 -0
  10. package/src/migrations/dev-server-support/coordinators/migration-executor.js.map +1 -0
  11. package/src/migrations/dev-server-support/coordinators/migration-reporter.d.ts +16 -0
  12. package/src/migrations/dev-server-support/coordinators/migration-reporter.d.ts.map +1 -0
  13. package/src/migrations/dev-server-support/coordinators/migration-reporter.js +49 -0
  14. package/src/migrations/dev-server-support/coordinators/migration-reporter.js.map +1 -0
  15. package/src/migrations/dev-server-support/helpers/config-updater.d.ts +24 -0
  16. package/src/migrations/dev-server-support/helpers/config-updater.d.ts.map +1 -0
  17. package/src/migrations/dev-server-support/helpers/config-updater.js +54 -0
  18. package/src/migrations/dev-server-support/helpers/config-updater.js.map +1 -0
  19. package/src/migrations/dev-server-support/helpers/port-allocator.d.ts +37 -0
  20. package/src/migrations/dev-server-support/helpers/port-allocator.d.ts.map +1 -0
  21. package/src/migrations/dev-server-support/helpers/port-allocator.js +49 -0
  22. package/src/migrations/dev-server-support/helpers/port-allocator.js.map +1 -0
  23. package/src/migrations/dev-server-support/helpers/project-analyzer.d.ts +74 -0
  24. package/src/migrations/dev-server-support/helpers/project-analyzer.d.ts.map +1 -0
  25. package/src/migrations/dev-server-support/helpers/project-analyzer.js +109 -0
  26. package/src/migrations/dev-server-support/helpers/project-analyzer.js.map +1 -0
  27. package/src/migrations/dev-server-support/helpers/webpack-updater.d.ts +48 -0
  28. package/src/migrations/dev-server-support/helpers/webpack-updater.d.ts.map +1 -0
  29. package/src/migrations/dev-server-support/helpers/webpack-updater.js +144 -0
  30. package/src/migrations/dev-server-support/helpers/webpack-updater.js.map +1 -0
  31. package/src/migrations/dev-server-support/migrate.d.ts +57 -0
  32. package/src/migrations/dev-server-support/migrate.d.ts.map +1 -0
  33. package/src/migrations/dev-server-support/migrate.js +68 -0
  34. package/src/migrations/dev-server-support/migrate.js.map +1 -0
  35. package/src/migrations/dev-server-support/types.d.ts +39 -0
  36. package/src/migrations/dev-server-support/types.d.ts.map +1 -0
  37. package/src/migrations/dev-server-support/types.js +6 -0
  38. package/src/migrations/dev-server-support/types.js.map +1 -0
  39. package/src/generators/preset/tools/tools/scripts/generate-api-types-split.mjs.template +0 -403
@@ -1,403 +0,0 @@
1
- import devkit from '@nx/devkit';
2
- import { mkdirSync, writeFileSync, readFileSync } from 'fs';
3
- import openapiTS, { astToString } from 'openapi-typescript';
4
- import { join } from 'path';
5
- const { readCachedProjectGraph } = devkit;
6
- const [, , name, ...appsToInclude] = process.argv;
7
-
8
- // Core services bundled with KOS SDKs - exclude these by default for third-party apps
9
- const CORE_SDK_SERVICES = ['kos', 'vfs', 'dispense', 'freestyle', 'handle', 'kosdev.ddk'];
10
- const excludeCoreServices = !appsToInclude.includes('--include-core');
11
-
12
- let graph, project, host, src, outputPath, serviceExportAliases, defaultVersion, appVersions;
13
-
14
- try {
15
- graph = readCachedProjectGraph();
16
- project = graph.nodes[name];
17
- host = project.data.targets.api?.options?.host || 'http://127.0.0.1:8081';
18
- outputPath = project.data.targets.api?.options?.outputPath;
19
- serviceExportAliases = project.data.targets.api?.options?.serviceExportAliases || {};
20
- defaultVersion = project.data.targets.api?.options?.defaultVersion;
21
- appVersions = project.data.targets.api?.options?.appVersions || {};
22
- src = project.data.sourceRoot || project.data.root;
23
- } catch (error) {
24
- // Fallback if project graph reading fails
25
- console.warn('Warning: Could not read project graph, using defaults');
26
- host = 'http://127.0.0.1:8081';
27
- src = `libs/${name}/src`;
28
- serviceExportAliases = {};
29
- defaultVersion = undefined;
30
- appVersions = {};
31
- }
32
-
33
- // Use custom output path if specified, otherwise default to src/utils
34
- const utilsDir = outputPath ? join(src, outputPath) : join(src, 'utils');
35
- const servicesDir = join(utilsDir, 'services');
36
- mkdirSync(servicesDir, { recursive: true });
37
-
38
- console.log(`Generating API types for ${name} from ${host}`);
39
-
40
- // Fetch the full OpenAPI spec
41
- const openapiUrl = new URL(`${host}/api/kos/openapi/api`, import.meta.url);
42
- const response = await fetch(openapiUrl);
43
- const fullSpec = await response.json();
44
-
45
- // Helper function to determine app name from path
46
- function getAppFromPath(path) {
47
- const segments = path.split('/').filter(Boolean);
48
-
49
- if (segments.length < 2) {
50
- return 'other';
51
- }
52
-
53
- // /api/kos/* → "kos"
54
- if (path.startsWith('/api/kos')) {
55
- return 'kos';
56
- }
57
-
58
- // /api/app/{appName}/* → appName
59
- if (path.startsWith('/api/app/')) {
60
- return segments[2] || 'app';
61
- }
62
-
63
- // /api/ext/{extName}/* → extName
64
- if (path.startsWith('/api/ext/')) {
65
- return segments[2] || 'ext';
66
- }
67
-
68
- // /api/{other}/* → other
69
- if (path.startsWith('/api/')) {
70
- return segments[1] || 'other';
71
- }
72
-
73
- return 'other';
74
- }
75
-
76
- // Group paths by app
77
- const appGroups = {};
78
- for (const [path, pathItem] of Object.entries(fullSpec.paths || {})) {
79
- const appName = getAppFromPath(path);
80
-
81
- if (!appGroups[appName]) {
82
- appGroups[appName] = {
83
- paths: {},
84
- components: new Set()
85
- };
86
- }
87
-
88
- appGroups[appName].paths[path] = pathItem;
89
-
90
- // Collect referenced components from this path
91
- const pathJson = JSON.stringify(pathItem);
92
- const refMatches = pathJson.matchAll(/#\/components\/schemas\/([^"]+)/g);
93
- for (const match of refMatches) {
94
- appGroups[appName].components.add(match[1]);
95
- }
96
- }
97
-
98
- // Helper function to recursively collect component dependencies
99
- function collectComponentDependencies(componentId, allComponents, collected = new Set()) {
100
- if (collected.has(componentId)) {
101
- return collected;
102
- }
103
-
104
- collected.add(componentId);
105
-
106
- const component = allComponents[componentId];
107
- if (!component) {
108
- return collected;
109
- }
110
-
111
- // Find all schema references in this component
112
- const componentJson = JSON.stringify(component);
113
- const refMatches = componentJson.matchAll(/#\/components\/schemas\/([^"]+)/g);
114
-
115
- for (const match of refMatches) {
116
- const refId = match[1];
117
- if (!collected.has(refId)) {
118
- collectComponentDependencies(refId, allComponents, collected);
119
- }
120
- }
121
-
122
- return collected;
123
- }
124
-
125
- // Filter apps based on command line arguments and core service exclusion
126
- let appsToProcess = Object.entries(appGroups);
127
-
128
- // Remove --include-core flag from appsToInclude if present
129
- const filteredAppsToInclude = appsToInclude.filter(app => app !== '--include-core');
130
-
131
- // Exclude core services by default unless --include-core flag is present
132
- if (excludeCoreServices) {
133
- appsToProcess = appsToProcess.filter(([appName]) => !CORE_SDK_SERVICES.includes(appName));
134
- console.log(`Excluding core SDK services: ${CORE_SDK_SERVICES.join(', ')}`);
135
- }
136
-
137
- // If specific apps are requested, further filter to only those
138
- if (filteredAppsToInclude.length > 0) {
139
- appsToProcess = appsToProcess.filter(([appName]) => filteredAppsToInclude.includes(appName));
140
- console.log(`Filtering to include only: ${filteredAppsToInclude.join(', ')}`);
141
- }
142
-
143
- // Generate a separate OpenAPI spec file for each app
144
- const generatedFiles = [];
145
-
146
- for (const [appName, appData] of appsToProcess) {
147
- // Collect all component dependencies
148
- const allComponentIds = new Set();
149
- for (const componentId of appData.components) {
150
- collectComponentDependencies(componentId, fullSpec.components?.schemas || {}, allComponentIds);
151
- }
152
-
153
- // Build filtered components
154
- const filteredComponents = {};
155
- for (const componentId of allComponentIds) {
156
- if (fullSpec.components?.schemas?.[componentId]) {
157
- filteredComponents[componentId] = fullSpec.components.schemas[componentId];
158
- }
159
- }
160
-
161
- // Create app-specific spec
162
- const appSpec = {
163
- openapi: fullSpec.openapi,
164
- info: {
165
- ...fullSpec.info,
166
- title: `${fullSpec.info.title} - ${appName}`
167
- },
168
- paths: appData.paths,
169
- components: {
170
- schemas: filteredComponents
171
- }
172
- };
173
-
174
- // Determine version: use custom app version if provided, otherwise use OpenAPI spec version
175
- const version = appVersions[appName] || fullSpec.info.version || 'v1';
176
- let appDir, filename, serviceFilename;
177
-
178
- if (['kos', 'ext'].includes(appName) || !appName.includes('.')) {
179
- // For kos, ext, and simple names: utils/services/{appName}/{version}/
180
- appDir = join(servicesDir, appName, version);
181
- filename = 'openapi.d.ts';
182
- serviceFilename = 'service.ts';
183
- } else {
184
- // For app names with dots: utils/services/{appName}/
185
- // If custom version specified, use versioned folder structure
186
- if (appVersions[appName]) {
187
- appDir = join(servicesDir, appName, version);
188
- } else {
189
- appDir = join(servicesDir, appName);
190
- }
191
- filename = 'openapi.d.ts';
192
- serviceFilename = 'service.ts';
193
- }
194
-
195
- mkdirSync(appDir, { recursive: true });
196
- const outFile = join(appDir, filename);
197
- const relativeAppDir = appDir.replace(utilsDir + '/', '');
198
-
199
- console.log(` - Generating ${relativeAppDir}/ (${Object.keys(appData.paths).length} paths, ${allComponentIds.size} components)`);
200
-
201
- // Generate TypeScript types from the filtered spec
202
- const ast = await openapiTS(appSpec);
203
- const contents = astToString(ast);
204
-
205
- writeFileSync(outFile, contents);
206
-
207
- // Generate corresponding service file for this app
208
- const serviceFile = join(appDir, serviceFilename);
209
- const typeModule = 'openapi';
210
-
211
- const serviceContent = `import {
212
- kosServiceRequest as baseKosServiceRequest,
213
- createClient,
214
- type ClientResponse,
215
- type HttpMethod,
216
- type IKosServiceRequestParams,
217
- type KosExecutionContext,
218
- type PathsByMethod,
219
- } from "@kosdev-code/kos-ui-sdk";
220
- import type { paths } from "./${typeModule}";
221
-
222
- /**
223
- * Type aliases for ${appName} API
224
- */
225
- export type Api = paths;
226
- export type ApiPath = keyof paths;
227
- export type ValidPaths = PathsByMethod<paths>;
228
-
229
- /**
230
- * Get client response type for ${appName} API
231
- */
232
- export type ApiResponse<
233
- Path extends ApiPath,
234
- Method extends "get" | "post" | "put" | "delete" = "get"
235
- > = ClientResponse<paths, Path, Method>;
236
-
237
- /**
238
- * Get execution context type for ${appName} API
239
- */
240
- export type ExecutionContext<
241
- Path extends ApiPath = ApiPath,
242
- Method extends HttpMethod = "get"
243
- > = KosExecutionContext<paths, Path, Method>;
244
-
245
- /**
246
- * Typed decorator factory for @kosServiceRequest with ${appName} API types
247
- *
248
- * Provides full IntelliSense and type safety for path, query params, and body
249
- * based on the ${appName} OpenAPI schema.
250
- *
251
- * @example
252
- * \`\`\`typescript
253
- * import { kosServiceRequest } from '../../utils/services/${appDir.replace(servicesDir + '/', '')}/service';
254
- * import { DependencyLifecycle } from '@kosdev-code/kos-ui-sdk';
255
- *
256
- * @kosServiceRequest({
257
- * path: '/api/...',
258
- * method: 'get',
259
- * lifecycle: DependencyLifecycle.LOAD
260
- * })
261
- * private onDataLoaded(): void {
262
- * // Fully typed based on ${appName} API
263
- * }
264
- * \`\`\`
265
- */
266
- export function kosServiceRequest<
267
- Path extends ApiPath,
268
- Method extends HttpMethod = "get",
269
- Response = any,
270
- TransformedResponse = Response
271
- >(
272
- params: IKosServiceRequestParams<
273
- paths,
274
- Path,
275
- Method,
276
- Response,
277
- TransformedResponse
278
- >
279
- ) {
280
- return baseKosServiceRequest<
281
- paths,
282
- Path,
283
- Method,
284
- Response,
285
- TransformedResponse
286
- >(params);
287
- }
288
-
289
- /**
290
- * Create an API client for ${appName}
291
- */
292
- export const api = createClient<paths>();
293
-
294
- export default api;
295
- `;
296
-
297
- writeFileSync(serviceFile, serviceContent);
298
-
299
- // Store version info if it's a core service (kos, ext) or has a custom version specified
300
- const versionInfo =
301
- ['kos', 'ext'].includes(appName) || !appName.includes('.') || appVersions[appName]
302
- ? version
303
- : null;
304
-
305
- generatedFiles.push({
306
- appName,
307
- appDir: appDir.replace(servicesDir + '/', ''),
308
- version: versionInfo,
309
- pathCount: Object.keys(appData.paths).length
310
- });
311
- }
312
-
313
- // Read existing index files to preserve manual entries
314
- function mergeIndexFile(filePath, newEntries, comment) {
315
- let existingContent = '';
316
- try {
317
- existingContent = readFileSync(filePath, 'utf-8');
318
- } catch (error) {
319
- // File doesn't exist yet, that's ok
320
- }
321
-
322
- // Parse existing exports
323
- const existingExports = new Map();
324
- const manualExports = [];
325
- const lines = existingContent.split('\n');
326
-
327
- for (const line of lines) {
328
- const match = line.match(/^export \* as (\w+) from ['"](.+)['"]/);
329
- if (match) {
330
- const [, exportName, path] = match;
331
- existingExports.set(exportName, { exportName, path, line });
332
- } else if (line.trim() && !line.startsWith('//')) {
333
- // Preserve non-export lines (imports, other statements)
334
- manualExports.push(line);
335
- }
336
- }
337
-
338
- // Merge with new entries
339
- for (const { exportName, path } of newEntries) {
340
- existingExports.set(exportName, {
341
- exportName,
342
- path,
343
- line: `export * as ${exportName} from '${path}';`
344
- });
345
- }
346
-
347
- // Build final content
348
- const header = comment ? `// ${comment}\n` : '';
349
- const exportLines = Array.from(existingExports.values())
350
- .sort((a, b) => a.exportName.localeCompare(b.exportName))
351
- .map(e => e.line);
352
-
353
- return header + exportLines.join('\n') + '\n' +
354
- (manualExports.length > 0 ? '\n' + manualExports.join('\n') + '\n' : '');
355
- }
356
-
357
- // Generate index file that exports all apps (merge with existing)
358
- const typeIndexEntries = generatedFiles.map(({ appName, appDir }) => ({
359
- exportName: appName.replace(/[.-]/g, '_'),
360
- path: `./services/${appDir}/openapi`
361
- }));
362
-
363
- const indexFile = join(utilsDir, 'openapi-index.ts');
364
- const indexContent = mergeIndexFile(
365
- indexFile,
366
- typeIndexEntries,
367
- 'Auto-generated type exports - new entries added automatically'
368
- );
369
- writeFileSync(indexFile, indexContent);
370
-
371
- // Generate service index that re-exports all app services (merge with existing)
372
- const serviceIndexEntries = generatedFiles.map(({ appName, appDir, version }) => {
373
- const baseAlias = serviceExportAliases[appName] || appName.replace(/[.-]/g, '_').toUpperCase();
374
-
375
- // Check if this version is the default version
376
- const isDefaultVersion = defaultVersion && version === defaultVersion;
377
-
378
- // For default version, use base alias. For others, append version suffix
379
- const exportName = isDefaultVersion
380
- ? baseAlias
381
- : `${baseAlias}_${version.replace(/[.-]/g, '_').toUpperCase()}`;
382
-
383
- return {
384
- exportName,
385
- path: `./services/${appDir}/service`
386
- };
387
- });
388
-
389
- const serviceIndexFile = join(utilsDir, 'services-index.ts');
390
- const serviceIndexContent = mergeIndexFile(
391
- serviceIndexFile,
392
- serviceIndexEntries,
393
- 'Auto-generated service exports - new entries added automatically'
394
- );
395
- writeFileSync(serviceIndexFile, serviceIndexContent);
396
-
397
- console.log(`\nGenerated ${generatedFiles.length} app-specific type and service file pairs:`);
398
- generatedFiles.forEach(({ appName, appDir, version, pathCount }) => {
399
- const versionStr = version ? ` [${version}]` : '';
400
- console.log(` ✓ ${appDir}/${versionStr} (${appName}: ${pathCount} paths)`);
401
- });
402
- console.log(` ✓ openapi-index.ts (aggregated type exports)`);
403
- console.log(` ✓ services-index.ts (aggregated service exports)`);