@geekmidas/cli 0.10.0 → 0.13.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 (146) hide show
  1. package/README.md +525 -0
  2. package/dist/bundler-B1qy9b-j.cjs +112 -0
  3. package/dist/bundler-B1qy9b-j.cjs.map +1 -0
  4. package/dist/bundler-DskIqW2t.mjs +111 -0
  5. package/dist/bundler-DskIqW2t.mjs.map +1 -0
  6. package/dist/{config-C9aXOHBe.cjs → config-AmInkU7k.cjs} +8 -8
  7. package/dist/config-AmInkU7k.cjs.map +1 -0
  8. package/dist/{config-BrkUalUh.mjs → config-DYULeEv8.mjs} +3 -3
  9. package/dist/config-DYULeEv8.mjs.map +1 -0
  10. package/dist/config.cjs +1 -1
  11. package/dist/config.d.cts +1 -1
  12. package/dist/config.d.mts +1 -1
  13. package/dist/config.mjs +1 -1
  14. package/dist/encryption-C8H-38Yy.mjs +42 -0
  15. package/dist/encryption-C8H-38Yy.mjs.map +1 -0
  16. package/dist/encryption-Dyf_r1h-.cjs +44 -0
  17. package/dist/encryption-Dyf_r1h-.cjs.map +1 -0
  18. package/dist/index.cjs +2123 -179
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.mjs +2141 -192
  21. package/dist/index.mjs.map +1 -1
  22. package/dist/{openapi-CZLI4QTr.mjs → openapi-BfFlOBCG.mjs} +801 -38
  23. package/dist/openapi-BfFlOBCG.mjs.map +1 -0
  24. package/dist/{openapi-BeHLKcwP.cjs → openapi-Bt_1FDpT.cjs} +794 -31
  25. package/dist/openapi-Bt_1FDpT.cjs.map +1 -0
  26. package/dist/{openapi-react-query-o5iMi8tz.cjs → openapi-react-query-B-sNWHFU.cjs} +5 -5
  27. package/dist/openapi-react-query-B-sNWHFU.cjs.map +1 -0
  28. package/dist/{openapi-react-query-CcciaVu5.mjs → openapi-react-query-B6XTeGqS.mjs} +5 -5
  29. package/dist/openapi-react-query-B6XTeGqS.mjs.map +1 -0
  30. package/dist/openapi-react-query.cjs +1 -1
  31. package/dist/openapi-react-query.d.cts.map +1 -1
  32. package/dist/openapi-react-query.d.mts.map +1 -1
  33. package/dist/openapi-react-query.mjs +1 -1
  34. package/dist/openapi.cjs +2 -2
  35. package/dist/openapi.d.cts +1 -1
  36. package/dist/openapi.d.cts.map +1 -1
  37. package/dist/openapi.d.mts +1 -1
  38. package/dist/openapi.d.mts.map +1 -1
  39. package/dist/openapi.mjs +2 -2
  40. package/dist/storage-BOOpAF8N.cjs +5 -0
  41. package/dist/storage-Bj1E26lU.cjs +187 -0
  42. package/dist/storage-Bj1E26lU.cjs.map +1 -0
  43. package/dist/storage-kSxTjkNb.mjs +133 -0
  44. package/dist/storage-kSxTjkNb.mjs.map +1 -0
  45. package/dist/storage-tgZSUnKl.mjs +3 -0
  46. package/dist/{types-b-vwGpqc.d.cts → types-BR0M2v_c.d.mts} +100 -1
  47. package/dist/types-BR0M2v_c.d.mts.map +1 -0
  48. package/dist/{types-DXgiA1sF.d.mts → types-BhkZc-vm.d.cts} +100 -1
  49. package/dist/types-BhkZc-vm.d.cts.map +1 -0
  50. package/examples/cron-example.ts +27 -27
  51. package/examples/env.ts +27 -27
  52. package/examples/function-example.ts +31 -31
  53. package/examples/gkm.config.json +20 -20
  54. package/examples/gkm.config.ts +8 -8
  55. package/examples/gkm.minimal.config.json +5 -5
  56. package/examples/gkm.production.config.json +25 -25
  57. package/examples/logger.ts +2 -2
  58. package/package.json +6 -6
  59. package/src/__tests__/EndpointGenerator.hooks.spec.ts +191 -191
  60. package/src/__tests__/config.spec.ts +55 -55
  61. package/src/__tests__/loadEnvFiles.spec.ts +93 -93
  62. package/src/__tests__/normalizeHooksConfig.spec.ts +58 -58
  63. package/src/__tests__/openapi-react-query.spec.ts +497 -497
  64. package/src/__tests__/openapi.spec.ts +428 -428
  65. package/src/__tests__/test-helpers.ts +76 -76
  66. package/src/auth/__tests__/credentials.spec.ts +204 -0
  67. package/src/auth/__tests__/index.spec.ts +168 -0
  68. package/src/auth/credentials.ts +187 -0
  69. package/src/auth/index.ts +226 -0
  70. package/src/build/__tests__/bundler.spec.ts +444 -0
  71. package/src/build/__tests__/index-new.spec.ts +474 -474
  72. package/src/build/__tests__/manifests.spec.ts +333 -333
  73. package/src/build/bundler.ts +210 -0
  74. package/src/build/endpoint-analyzer.ts +236 -0
  75. package/src/build/handler-templates.ts +1253 -0
  76. package/src/build/index.ts +260 -179
  77. package/src/build/manifests.ts +52 -52
  78. package/src/build/providerResolver.ts +145 -145
  79. package/src/build/types.ts +64 -43
  80. package/src/config.ts +39 -39
  81. package/src/deploy/__tests__/docker.spec.ts +111 -0
  82. package/src/deploy/__tests__/dokploy.spec.ts +245 -0
  83. package/src/deploy/__tests__/init.spec.ts +662 -0
  84. package/src/deploy/docker.ts +128 -0
  85. package/src/deploy/dokploy.ts +204 -0
  86. package/src/deploy/index.ts +136 -0
  87. package/src/deploy/init.ts +484 -0
  88. package/src/deploy/types.ts +48 -0
  89. package/src/dev/__tests__/index.spec.ts +266 -266
  90. package/src/dev/index.ts +647 -601
  91. package/src/docker/__tests__/compose.spec.ts +531 -0
  92. package/src/docker/__tests__/templates.spec.ts +280 -0
  93. package/src/docker/compose.ts +273 -0
  94. package/src/docker/index.ts +230 -0
  95. package/src/docker/templates.ts +446 -0
  96. package/src/generators/CronGenerator.ts +72 -72
  97. package/src/generators/EndpointGenerator.ts +699 -398
  98. package/src/generators/FunctionGenerator.ts +84 -84
  99. package/src/generators/Generator.ts +72 -72
  100. package/src/generators/OpenApiTsGenerator.ts +577 -577
  101. package/src/generators/SubscriberGenerator.ts +124 -124
  102. package/src/generators/__tests__/CronGenerator.spec.ts +433 -433
  103. package/src/generators/__tests__/EndpointGenerator.spec.ts +532 -382
  104. package/src/generators/__tests__/FunctionGenerator.spec.ts +244 -244
  105. package/src/generators/__tests__/SubscriberGenerator.spec.ts +397 -382
  106. package/src/generators/index.ts +4 -4
  107. package/src/index.ts +623 -201
  108. package/src/init/__tests__/generators.spec.ts +334 -334
  109. package/src/init/__tests__/init.spec.ts +332 -332
  110. package/src/init/__tests__/utils.spec.ts +89 -89
  111. package/src/init/generators/config.ts +175 -175
  112. package/src/init/generators/docker.ts +41 -41
  113. package/src/init/generators/env.ts +72 -72
  114. package/src/init/generators/index.ts +1 -1
  115. package/src/init/generators/models.ts +64 -64
  116. package/src/init/generators/monorepo.ts +161 -161
  117. package/src/init/generators/package.ts +71 -71
  118. package/src/init/generators/source.ts +6 -6
  119. package/src/init/index.ts +203 -208
  120. package/src/init/templates/api.ts +115 -115
  121. package/src/init/templates/index.ts +75 -75
  122. package/src/init/templates/minimal.ts +98 -98
  123. package/src/init/templates/serverless.ts +89 -89
  124. package/src/init/templates/worker.ts +98 -98
  125. package/src/init/utils.ts +54 -56
  126. package/src/openapi-react-query.ts +194 -194
  127. package/src/openapi.ts +63 -63
  128. package/src/secrets/__tests__/encryption.spec.ts +226 -0
  129. package/src/secrets/__tests__/generator.spec.ts +319 -0
  130. package/src/secrets/__tests__/index.spec.ts +91 -0
  131. package/src/secrets/__tests__/storage.spec.ts +611 -0
  132. package/src/secrets/encryption.ts +91 -0
  133. package/src/secrets/generator.ts +164 -0
  134. package/src/secrets/index.ts +383 -0
  135. package/src/secrets/storage.ts +192 -0
  136. package/src/secrets/types.ts +53 -0
  137. package/src/types.ts +295 -176
  138. package/tsdown.config.ts +11 -8
  139. package/dist/config-BrkUalUh.mjs.map +0 -1
  140. package/dist/config-C9aXOHBe.cjs.map +0 -1
  141. package/dist/openapi-BeHLKcwP.cjs.map +0 -1
  142. package/dist/openapi-CZLI4QTr.mjs.map +0 -1
  143. package/dist/openapi-react-query-CcciaVu5.mjs.map +0 -1
  144. package/dist/openapi-react-query-o5iMi8tz.cjs.map +0 -1
  145. package/dist/types-DXgiA1sF.d.mts.map +0 -1
  146. package/dist/types-b-vwGpqc.d.cts.map +0 -1
@@ -0,0 +1,210 @@
1
+ import { execSync } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ import { mkdir, rename, writeFile } from 'node:fs/promises';
4
+ import { join } from 'node:path';
5
+ import type { Construct } from '@geekmidas/constructs';
6
+
7
+ export interface BundleOptions {
8
+ /** Entry point file (e.g., .gkm/server/server.ts) */
9
+ entryPoint: string;
10
+ /** Output directory for bundled files */
11
+ outputDir: string;
12
+ /** Minify the output (default: true) */
13
+ minify: boolean;
14
+ /** Generate sourcemaps (default: false) */
15
+ sourcemap: boolean;
16
+ /** Packages to exclude from bundling */
17
+ external: string[];
18
+ /** Stage for secrets injection (optional) */
19
+ stage?: string;
20
+ /** Constructs to validate environment variables for */
21
+ constructs?: Construct[];
22
+ }
23
+
24
+ export interface BundleResult {
25
+ /** Path to the bundled output */
26
+ outputPath: string;
27
+ /** Ephemeral master key for deployment (only if stage was provided) */
28
+ masterKey?: string;
29
+ }
30
+
31
+ /**
32
+ * Collect all required environment variables from constructs.
33
+ * Uses the SnifferEnvironmentParser to detect which env vars each service needs.
34
+ *
35
+ * @param constructs - Array of constructs to analyze
36
+ * @returns Deduplicated array of required environment variable names
37
+ */
38
+ async function collectRequiredEnvVars(
39
+ constructs: Construct[],
40
+ ): Promise<string[]> {
41
+ const allEnvVars = new Set<string>();
42
+
43
+ for (const construct of constructs) {
44
+ const envVars = await construct.getEnvironment();
45
+ envVars.forEach((v) => allEnvVars.add(v));
46
+ }
47
+
48
+ return Array.from(allEnvVars).sort();
49
+ }
50
+
51
+ /**
52
+ * Bundle the server application using tsdown
53
+ *
54
+ * @param options - Bundle configuration options
55
+ * @returns Bundle result with output path and optional master key
56
+ */
57
+ export async function bundleServer(
58
+ options: BundleOptions,
59
+ ): Promise<BundleResult> {
60
+ const {
61
+ entryPoint,
62
+ outputDir,
63
+ minify,
64
+ sourcemap,
65
+ external,
66
+ stage,
67
+ constructs,
68
+ } = options;
69
+
70
+ // Ensure output directory exists
71
+ await mkdir(outputDir, { recursive: true });
72
+
73
+ // Build command-line arguments for tsdown
74
+ const args = [
75
+ 'npx',
76
+ 'tsdown',
77
+ entryPoint,
78
+ '--no-config', // Don't use any config file from workspace
79
+ '--out-dir',
80
+ outputDir,
81
+ '--format',
82
+ 'esm',
83
+ '--platform',
84
+ 'node',
85
+ '--target',
86
+ 'node22',
87
+ '--clean',
88
+ ];
89
+
90
+ if (minify) {
91
+ args.push('--minify');
92
+ }
93
+
94
+ if (sourcemap) {
95
+ args.push('--sourcemap');
96
+ }
97
+
98
+ // Add external packages
99
+ for (const ext of external) {
100
+ args.push('--external', ext);
101
+ }
102
+
103
+ // Always exclude node: builtins
104
+ args.push('--external', 'node:*');
105
+
106
+ // Handle secrets injection if stage is provided
107
+ let masterKey: string | undefined;
108
+
109
+ if (stage) {
110
+ const {
111
+ readStageSecrets,
112
+ toEmbeddableSecrets,
113
+ validateEnvironmentVariables,
114
+ } = await import('../secrets/storage');
115
+ const { encryptSecrets, generateDefineOptions } = await import(
116
+ '../secrets/encryption'
117
+ );
118
+
119
+ const secrets = await readStageSecrets(stage);
120
+
121
+ if (!secrets) {
122
+ throw new Error(
123
+ `No secrets found for stage "${stage}". Run "gkm secrets:init --stage ${stage}" first.`,
124
+ );
125
+ }
126
+
127
+ // Validate environment variables if constructs are provided
128
+ if (constructs && constructs.length > 0) {
129
+ console.log(' Analyzing environment variable requirements...');
130
+ const requiredVars = await collectRequiredEnvVars(constructs);
131
+
132
+ if (requiredVars.length > 0) {
133
+ const validation = validateEnvironmentVariables(requiredVars, secrets);
134
+
135
+ if (!validation.valid) {
136
+ const errorMessage = [
137
+ `Missing environment variables for stage "${stage}":`,
138
+ '',
139
+ ...validation.missing.map((v) => ` ❌ ${v}`),
140
+ '',
141
+ 'To fix this, either:',
142
+ ` 1. Add the missing variables to .gkm/secrets/${stage}.json using:`,
143
+ ` gkm secrets:set <KEY> <VALUE> --stage ${stage}`,
144
+ '',
145
+ ` 2. Or import from a JSON file:`,
146
+ ` gkm secrets:import secrets.json --stage ${stage}`,
147
+ '',
148
+ 'Required variables:',
149
+ ...validation.required.map((v) =>
150
+ validation.missing.includes(v) ? ` ❌ ${v}` : ` ✓ ${v}`,
151
+ ),
152
+ ].join('\n');
153
+
154
+ throw new Error(errorMessage);
155
+ }
156
+
157
+ console.log(
158
+ ` ✓ All ${requiredVars.length} required environment variables found`,
159
+ );
160
+ }
161
+ }
162
+
163
+ // Convert to embeddable format and encrypt
164
+ const embeddable = toEmbeddableSecrets(secrets);
165
+ const encrypted = encryptSecrets(embeddable);
166
+ masterKey = encrypted.masterKey;
167
+
168
+ // Add define options for build-time injection
169
+ const defines = generateDefineOptions(encrypted);
170
+ for (const [key, value] of Object.entries(defines)) {
171
+ args.push('--define', `${key}=${value}`);
172
+ }
173
+
174
+ console.log(` Secrets encrypted for stage "${stage}"`);
175
+ }
176
+
177
+ const mjsOutput = join(outputDir, 'server.mjs');
178
+
179
+ try {
180
+ // Run tsdown with command-line arguments
181
+ execSync(args.join(' '), {
182
+ cwd: process.cwd(),
183
+ stdio: 'inherit',
184
+ });
185
+
186
+ // Rename output to .mjs for explicit ESM
187
+ // tsdown outputs as server.js for ESM format
188
+ const jsOutput = join(outputDir, 'server.js');
189
+
190
+ if (existsSync(jsOutput)) {
191
+ await rename(jsOutput, mjsOutput);
192
+ }
193
+
194
+ // Add shebang to the bundled file
195
+ const { readFile } = await import('node:fs/promises');
196
+ const content = await readFile(mjsOutput, 'utf-8');
197
+ if (!content.startsWith('#!')) {
198
+ await writeFile(mjsOutput, `#!/usr/bin/env node\n${content}`);
199
+ }
200
+ } catch (error) {
201
+ throw new Error(
202
+ `Failed to bundle server: ${error instanceof Error ? error.message : 'Unknown error'}`,
203
+ );
204
+ }
205
+
206
+ return {
207
+ outputPath: mjsOutput,
208
+ masterKey,
209
+ };
210
+ }
@@ -0,0 +1,236 @@
1
+ /**
2
+ * Endpoint Analyzer for Build-Time Feature Detection
3
+ *
4
+ * Analyzes endpoints at build time to determine their features and assign
5
+ * optimization tiers. This enables generating specialized handler code
6
+ * for maximum performance.
7
+ */
8
+ import type { Endpoint } from '@geekmidas/constructs/endpoints';
9
+
10
+ /**
11
+ * Features detected from an endpoint configuration
12
+ */
13
+ export interface EndpointFeatures {
14
+ hasAuth: boolean;
15
+ hasServices: boolean;
16
+ hasDatabase: boolean;
17
+ hasBodyValidation: boolean;
18
+ hasQueryValidation: boolean;
19
+ hasParamValidation: boolean;
20
+ hasAudits: boolean;
21
+ hasEvents: boolean;
22
+ hasRateLimit: boolean;
23
+ hasRls: boolean;
24
+ hasOutputValidation: boolean;
25
+ }
26
+
27
+ /**
28
+ * Optimization tiers based on endpoint complexity
29
+ *
30
+ * - minimal: No auth, no services, no audits, no events - near-raw-Hono performance
31
+ * - standard: Some features enabled - uses middleware composition
32
+ * - full: Complex endpoints with many features - full handler chain
33
+ */
34
+ export type EndpointTier = 'minimal' | 'standard' | 'full';
35
+
36
+ /**
37
+ * Complete analysis of an endpoint for build-time optimization
38
+ */
39
+ export interface EndpointAnalysis {
40
+ route: string;
41
+ method: string;
42
+ exportName: string;
43
+ features: EndpointFeatures;
44
+ tier: EndpointTier;
45
+ serviceNames: string[];
46
+ databaseServiceName?: string;
47
+ }
48
+
49
+ /**
50
+ * Analyze an endpoint to extract its features
51
+ */
52
+ export function analyzeEndpointFeatures(
53
+ endpoint: Endpoint<
54
+ any,
55
+ any,
56
+ any,
57
+ any,
58
+ any,
59
+ any,
60
+ any,
61
+ any,
62
+ any,
63
+ any,
64
+ any,
65
+ any,
66
+ any,
67
+ any
68
+ >,
69
+ ): EndpointFeatures {
70
+ return {
71
+ hasAuth: !!endpoint.authorizer,
72
+ hasServices: endpoint.services.length > 0,
73
+ hasDatabase: !!endpoint.databaseService,
74
+ hasBodyValidation: !!endpoint.input?.body,
75
+ hasQueryValidation: !!endpoint.input?.query,
76
+ hasParamValidation: !!endpoint.input?.params,
77
+ // Only declarative audits (.audit([...])) require full tier with transaction wrapping
78
+ // Having auditorStorageService just makes auditor available to handler (like other services)
79
+ hasAudits: (endpoint.audits?.length ?? 0) > 0,
80
+ hasEvents: (endpoint.events?.length ?? 0) > 0,
81
+ hasRateLimit: !!endpoint.rateLimit,
82
+ hasRls: !!endpoint.rlsConfig && !endpoint.rlsBypass,
83
+ hasOutputValidation: !!endpoint.outputSchema,
84
+ };
85
+ }
86
+
87
+ /**
88
+ * Determine the optimization tier for an endpoint based on its features
89
+ */
90
+ export function determineEndpointTier(
91
+ features: EndpointFeatures,
92
+ ): EndpointTier {
93
+ const {
94
+ hasAuth,
95
+ hasServices,
96
+ hasDatabase,
97
+ hasAudits,
98
+ hasEvents,
99
+ hasRateLimit,
100
+ hasRls,
101
+ } = features;
102
+
103
+ // Minimal tier: No complex features
104
+ // These endpoints can use near-raw-Hono handlers
105
+ if (
106
+ !hasAuth &&
107
+ !hasServices &&
108
+ !hasDatabase &&
109
+ !hasAudits &&
110
+ !hasEvents &&
111
+ !hasRateLimit &&
112
+ !hasRls
113
+ ) {
114
+ return 'minimal';
115
+ }
116
+
117
+ // Full tier: Has audits, RLS, or rate limiting (complex state management)
118
+ if (hasAudits || hasRls || hasRateLimit) {
119
+ return 'full';
120
+ }
121
+
122
+ // Standard tier: Auth and/or services, but no complex state
123
+ return 'standard';
124
+ }
125
+
126
+ /**
127
+ * Perform complete analysis of an endpoint
128
+ */
129
+ export function analyzeEndpoint(
130
+ endpoint: Endpoint<
131
+ any,
132
+ any,
133
+ any,
134
+ any,
135
+ any,
136
+ any,
137
+ any,
138
+ any,
139
+ any,
140
+ any,
141
+ any,
142
+ any,
143
+ any,
144
+ any
145
+ >,
146
+ exportName: string,
147
+ ): EndpointAnalysis {
148
+ const features = analyzeEndpointFeatures(endpoint);
149
+ const tier = determineEndpointTier(features);
150
+
151
+ return {
152
+ route: endpoint.route,
153
+ method: endpoint.method,
154
+ exportName,
155
+ features,
156
+ tier,
157
+ serviceNames: endpoint.services.map(
158
+ (s: { serviceName: string }) => s.serviceName,
159
+ ),
160
+ databaseServiceName: endpoint.databaseService?.serviceName,
161
+ };
162
+ }
163
+
164
+ /**
165
+ * Analyze multiple endpoints and return analysis results
166
+ */
167
+ export function analyzeEndpoints(
168
+ endpoints: Array<{
169
+ endpoint: Endpoint<
170
+ any,
171
+ any,
172
+ any,
173
+ any,
174
+ any,
175
+ any,
176
+ any,
177
+ any,
178
+ any,
179
+ any,
180
+ any,
181
+ any,
182
+ any,
183
+ any
184
+ >;
185
+ exportName: string;
186
+ }>,
187
+ ): EndpointAnalysis[] {
188
+ return endpoints.map(({ endpoint, exportName }) =>
189
+ analyzeEndpoint(endpoint, exportName),
190
+ );
191
+ }
192
+
193
+ /**
194
+ * Generate a summary of endpoint analysis for logging
195
+ */
196
+ export function summarizeAnalysis(analyses: EndpointAnalysis[]): {
197
+ total: number;
198
+ byTier: Record<EndpointTier, number>;
199
+ byFeature: Record<keyof EndpointFeatures, number>;
200
+ } {
201
+ const byTier: Record<EndpointTier, number> = {
202
+ minimal: 0,
203
+ standard: 0,
204
+ full: 0,
205
+ };
206
+
207
+ const byFeature: Record<keyof EndpointFeatures, number> = {
208
+ hasAuth: 0,
209
+ hasServices: 0,
210
+ hasDatabase: 0,
211
+ hasBodyValidation: 0,
212
+ hasQueryValidation: 0,
213
+ hasParamValidation: 0,
214
+ hasAudits: 0,
215
+ hasEvents: 0,
216
+ hasRateLimit: 0,
217
+ hasRls: 0,
218
+ hasOutputValidation: 0,
219
+ };
220
+
221
+ for (const analysis of analyses) {
222
+ byTier[analysis.tier]++;
223
+
224
+ for (const [feature, enabled] of Object.entries(analysis.features)) {
225
+ if (enabled) {
226
+ byFeature[feature as keyof EndpointFeatures]++;
227
+ }
228
+ }
229
+ }
230
+
231
+ return {
232
+ total: analyses.length,
233
+ byTier,
234
+ byFeature,
235
+ };
236
+ }