@opensaas/stack-cli 0.1.0 → 0.1.2

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 (49) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +21 -0
  3. package/CLAUDE.md +249 -0
  4. package/LICENSE +21 -0
  5. package/README.md +65 -5
  6. package/dist/commands/generate.d.ts.map +1 -1
  7. package/dist/commands/generate.js +6 -1
  8. package/dist/commands/generate.js.map +1 -1
  9. package/dist/commands/init.d.ts +9 -1
  10. package/dist/commands/init.d.ts.map +1 -1
  11. package/dist/commands/init.js +27 -338
  12. package/dist/commands/init.js.map +1 -1
  13. package/dist/generator/context.d.ts.map +1 -1
  14. package/dist/generator/context.js +78 -3
  15. package/dist/generator/context.js.map +1 -1
  16. package/dist/generator/index.d.ts +1 -0
  17. package/dist/generator/index.d.ts.map +1 -1
  18. package/dist/generator/index.js +1 -0
  19. package/dist/generator/index.js.map +1 -1
  20. package/dist/generator/mcp.d.ts +14 -0
  21. package/dist/generator/mcp.d.ts.map +1 -0
  22. package/dist/generator/mcp.js +193 -0
  23. package/dist/generator/mcp.js.map +1 -0
  24. package/dist/generator/types.d.ts.map +1 -1
  25. package/dist/generator/types.js +11 -33
  26. package/dist/generator/types.js.map +1 -1
  27. package/dist/index.js +10 -4
  28. package/dist/index.js.map +1 -1
  29. package/package.json +9 -3
  30. package/src/commands/__snapshots__/generate.test.ts.snap +265 -0
  31. package/src/commands/dev.test.ts +216 -0
  32. package/src/commands/generate.test.ts +272 -0
  33. package/src/commands/generate.ts +7 -0
  34. package/src/commands/init.ts +28 -361
  35. package/src/generator/__snapshots__/context.test.ts.snap +137 -0
  36. package/src/generator/__snapshots__/prisma.test.ts.snap +182 -0
  37. package/src/generator/__snapshots__/types.test.ts.snap +512 -0
  38. package/src/generator/context.test.ts +145 -0
  39. package/src/generator/context.ts +80 -3
  40. package/src/generator/index.ts +1 -0
  41. package/src/generator/mcp.test.ts +393 -0
  42. package/src/generator/mcp.ts +221 -0
  43. package/src/generator/prisma.test.ts +221 -0
  44. package/src/generator/types.test.ts +280 -0
  45. package/src/generator/types.ts +14 -36
  46. package/src/index.ts +8 -4
  47. package/tsconfig.json +1 -1
  48. package/tsconfig.tsbuildinfo +1 -1
  49. package/vitest.config.ts +26 -0
@@ -1,343 +1,32 @@
1
- import * as path from 'path';
2
- import * as fs from 'fs';
1
+ import { spawn } from 'child_process';
3
2
  import chalk from 'chalk';
4
- import ora from 'ora';
5
- import prompts from 'prompts';
6
- export async function initCommand(projectName) {
7
- console.log(chalk.bold.cyan('\nšŸš€ Create OpenSaas Project\n'));
8
- // Prompt for project name if not provided
9
- if (!projectName) {
10
- const response = await prompts({
11
- type: 'text',
12
- name: 'name',
13
- message: 'Project name:',
14
- initial: 'my-opensaas-app',
15
- validate: (value) => {
16
- if (!value)
17
- return 'Project name is required';
18
- if (!/^[a-z0-9-]+$/.test(value)) {
19
- return 'Project name must contain only lowercase letters, numbers, and hyphens';
20
- }
21
- return true;
22
- },
23
- });
24
- if (!response.name) {
25
- console.log(chalk.yellow('\nāŒ Cancelled'));
26
- process.exit(0);
27
- }
28
- projectName = response.name;
29
- }
30
- // Type guard to ensure projectName is defined
31
- if (!projectName) {
32
- console.error(chalk.red('\nāŒ Project name is required'));
33
- process.exit(1);
34
- }
35
- const projectPath = path.join(process.cwd(), projectName);
36
- // Check if directory already exists
37
- if (fs.existsSync(projectPath)) {
38
- console.error(chalk.red(`\nāŒ Directory "${projectName}" already exists`));
39
- process.exit(1);
40
- }
41
- const spinner = ora('Creating project structure...').start();
42
- try {
43
- // Create project directory
44
- fs.mkdirSync(projectPath, { recursive: true });
45
- // Create basic structure
46
- fs.mkdirSync(path.join(projectPath, 'app'), { recursive: true });
47
- fs.mkdirSync(path.join(projectPath, 'lib'), { recursive: true });
48
- fs.mkdirSync(path.join(projectPath, 'prisma'), { recursive: true });
49
- spinner.text = 'Writing configuration files...';
50
- // Create package.json
51
- const packageJson = {
52
- name: projectName,
53
- version: '0.1.0',
54
- private: true,
55
- scripts: {
56
- dev: 'next dev',
57
- build: 'next build',
58
- start: 'next start',
59
- generate: 'opensaas generate',
60
- 'db:push': 'prisma db push',
61
- 'db:studio': 'prisma studio',
62
- },
63
- dependencies: {
64
- '@opensaas/stack-core': '^0.1.0',
65
- '@prisma/client': '^5.7.1',
66
- next: '^14.0.4',
67
- react: '^18.2.0',
68
- 'react-dom': '^18.2.0',
69
- },
70
- devDependencies: {
71
- '@opensaas/stack-cli': '^0.1.0',
72
- '@types/node': '^20.10.0',
73
- '@types/react': '^18.2.45',
74
- '@types/react-dom': '^18.2.18',
75
- prisma: '^5.7.1',
76
- tsx: '^4.7.0',
77
- typescript: '^5.3.3',
78
- },
79
- };
80
- fs.writeFileSync(path.join(projectPath, 'package.json'), JSON.stringify(packageJson, null, 2));
81
- // Create tsconfig.json
82
- const tsConfig = {
83
- compilerOptions: {
84
- target: 'ES2022',
85
- lib: ['dom', 'dom.iterable', 'esnext'],
86
- allowJs: true,
87
- skipLibCheck: true,
88
- strict: true,
89
- noEmit: true,
90
- esModuleInterop: true,
91
- module: 'esnext',
92
- moduleResolution: 'bundler',
93
- resolveJsonModule: true,
94
- isolatedModules: true,
95
- jsx: 'preserve',
96
- incremental: true,
97
- plugins: [{ name: 'next' }],
98
- paths: {
99
- '@/*': ['./*'],
100
- },
101
- },
102
- include: ['next-env.d.ts', '**/*.ts', '**/*.tsx', '.next/types/**/*.ts'],
103
- exclude: ['node_modules'],
104
- };
105
- fs.writeFileSync(path.join(projectPath, 'tsconfig.json'), JSON.stringify(tsConfig, null, 2));
106
- // Create next.config.js
107
- const nextConfig = `/** @type {import('next').NextConfig} */
108
- const nextConfig = {
109
- experimental: {
110
- serverComponentsExternalPackages: ['@prisma/client', '@opensaas/stack-core'],
111
- },
112
- }
113
-
114
- module.exports = nextConfig
115
- `;
116
- fs.writeFileSync(path.join(projectPath, 'next.config.js'), nextConfig);
117
- // Create .env
118
- const env = `DATABASE_URL="file:./dev.db"
119
- `;
120
- fs.writeFileSync(path.join(projectPath, '.env'), env);
121
- // Create .gitignore
122
- const gitignore = `# Dependencies
123
- node_modules
124
- .pnp
125
- .pnp.js
126
-
127
- # Testing
128
- coverage
129
-
130
- # Next.js
131
- .next
132
- out
133
-
134
- # Production
135
- build
136
- dist
137
-
138
- # Misc
139
- .DS_Store
140
- *.pem
141
-
142
- # Debug
143
- npm-debug.log*
144
- yarn-debug.log*
145
- yarn-error.log*
146
-
147
- # Local env files
148
- .env
149
- .env.local
150
- .env.development.local
151
- .env.test.local
152
- .env.production.local
153
-
154
- # Vercel
155
- .vercel
156
-
157
- # TypeScript
158
- *.tsbuildinfo
159
-
160
- # OpenSaas generated
161
- .opensaas
162
-
163
- # Prisma
164
- prisma/dev.db
165
- prisma/dev.db-journal
166
- `;
167
- fs.writeFileSync(path.join(projectPath, '.gitignore'), gitignore);
168
- // Create opensaas.config.ts
169
- const config = `import { config, list } from '@opensaas/stack-core'
170
- import { text, relationship, password } from '@opensaas/stack-core/fields'
171
- import type { AccessControl } from '@opensaas/stack-core'
172
-
173
- // Access control helpers
174
- const isSignedIn: AccessControl = ({ session }) => {
175
- return !!session
176
- }
177
-
178
- export default config({
179
- db: {
180
- provider: 'sqlite',
181
- url: process.env.DATABASE_URL || 'file:./dev.db',
182
- },
183
-
184
- lists: {
185
- User: list({
186
- fields: {
187
- name: text({ validation: { isRequired: true } }),
188
- email: text({
189
- validation: { isRequired: true },
190
- isIndexed: 'unique',
191
- }),
192
- password: password({ validation: { isRequired: true } }),
193
- },
194
- }),
195
- },
196
-
197
- session: {
198
- getSession: async () => {
199
- // TODO: Integrate with your auth system
200
- return null
201
- }
202
- },
203
-
204
- ui: {
205
- basePath: '/admin',
206
- }
207
- })
208
- `;
209
- fs.writeFileSync(path.join(projectPath, 'opensaas.config.ts'), config);
210
- // Create lib/context.ts
211
- const contextFile = `import { PrismaClient } from '@prisma/client'
212
- import { getContext as createContext } from '@opensaas/stack-core'
213
- import config from '../opensaas.config'
214
- import type { Context } from '../.opensaas/types'
215
-
216
- // Singleton Prisma client
217
- const globalForPrisma = globalThis as unknown as {
218
- prisma: PrismaClient | undefined
219
- }
220
-
221
- export const prisma = globalForPrisma.prisma ?? new PrismaClient()
222
-
223
- if (process.env.NODE_ENV !== 'production') {
224
- globalForPrisma.prisma = prisma
225
- }
226
-
227
3
  /**
228
- * Get an access-controlled context for the current session
4
+ * Initialize a new OpenSaas Stack project.
5
+ *
6
+ * This command delegates to create-opensaas-app for the actual scaffolding.
7
+ * It's kept here for backwards compatibility with `opensaas init`.
8
+ *
9
+ * @param args - Command line arguments (project name and flags)
229
10
  */
230
- export async function getContext(): Promise<Context> {
231
- const session = config.session ? await config.session.getSession() : null
232
- const context = await createContext<PrismaClient>(config, prisma, session)
233
- return context as Context
234
- }
235
- `;
236
- fs.writeFileSync(path.join(projectPath, 'lib', 'context.ts'), contextFile);
237
- // Create app/page.tsx
238
- const page = `export default function Home() {
239
- return (
240
- <main className="flex min-h-screen flex-col items-center justify-center p-24">
241
- <h1 className="text-4xl font-bold mb-4">Welcome to OpenSaas</h1>
242
- <p className="text-gray-600">Your project is ready to go!</p>
243
-
244
- <div className="mt-8 space-y-2">
245
- <p className="text-sm">Next steps:</p>
246
- <ol className="text-sm text-gray-600 list-decimal list-inside space-y-1">
247
- <li>Run <code className="bg-gray-100 px-2 py-1 rounded">npm run generate</code></li>
248
- <li>Run <code className="bg-gray-100 px-2 py-1 rounded">npm run db:push</code></li>
249
- <li>Edit <code className="bg-gray-100 px-2 py-1 rounded">opensaas.config.ts</code> to define your schema</li>
250
- </ol>
251
- </div>
252
- </main>
253
- )
254
- }
255
- `;
256
- fs.writeFileSync(path.join(projectPath, 'app', 'page.tsx'), page);
257
- // Create app/layout.tsx
258
- const layout = `export const metadata = {
259
- title: '${projectName}',
260
- description: 'Built with OpenSaas',
261
- }
262
-
263
- export default function RootLayout({
264
- children,
265
- }: {
266
- children: React.ReactNode
267
- }) {
268
- return (
269
- <html lang="en">
270
- <body>{children}</body>
271
- </html>
272
- )
273
- }
274
- `;
275
- fs.writeFileSync(path.join(projectPath, 'app', 'layout.tsx'), layout);
276
- // Create README.md
277
- const readme = `# ${projectName}
278
-
279
- Built with [OpenSaas Stack](https://github.com/your-org/opensaas-stack)
280
-
281
- ## Getting Started
282
-
283
- 1. Install dependencies:
284
- \`\`\`bash
285
- npm install
286
- # or
287
- pnpm install
288
- \`\`\`
289
-
290
- 2. Generate Prisma schema and types:
291
- \`\`\`bash
292
- npm run generate
293
- \`\`\`
294
-
295
- 3. Push schema to database:
296
- \`\`\`bash
297
- npm run db:push
298
- \`\`\`
299
-
300
- 4. Run the development server:
301
- \`\`\`bash
302
- npm run dev
303
- \`\`\`
304
-
305
- Open [http://localhost:3000](http://localhost:3000) to see your app.
306
-
307
- ## Project Structure
308
-
309
- - \`opensaas.config.ts\` - Your schema definition with access control
310
- - \`lib/context.ts\` - Database context with access control
311
- - \`app/\` - Next.js app router pages
312
- - \`prisma/\` - Generated Prisma schema
313
- - \`.opensaas/\` - Generated TypeScript types
314
-
315
- ## Learn More
316
-
317
- - [OpenSaas Documentation](https://github.com/your-org/opensaas-stack)
318
- - [Next.js Documentation](https://nextjs.org/docs)
319
- - [Prisma Documentation](https://www.prisma.io/docs)
320
- `;
321
- fs.writeFileSync(path.join(projectPath, 'README.md'), readme);
322
- spinner.succeed(chalk.green('Project created successfully!'));
323
- console.log(chalk.bold.green(`\n✨ Created ${projectName}\n`));
324
- console.log(chalk.gray('Next steps:\n'));
325
- console.log(chalk.cyan(` cd ${projectName}`));
326
- console.log(chalk.cyan(' npm install'));
327
- console.log(chalk.cyan(' npm run generate'));
328
- console.log(chalk.cyan(' npm run db:push'));
329
- console.log(chalk.cyan(' npm run dev'));
330
- console.log();
331
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
332
- }
333
- catch (error) {
334
- spinner.fail(chalk.red('Failed to create project'));
335
- console.error(chalk.red('\nāŒ Error:'), error.message);
336
- // Cleanup on failure
337
- if (fs.existsSync(projectPath)) {
338
- fs.rmSync(projectPath, { recursive: true, force: true });
339
- }
340
- process.exit(1);
341
- }
11
+ export async function initCommand(args) {
12
+ console.log(chalk.dim('Delegating to create-opensaas-app...\n'));
13
+ // Forward all arguments to create-opensaas-app
14
+ const child = spawn('npx', ['create-opensaas-app@latest', ...args], {
15
+ stdio: 'inherit',
16
+ shell: true,
17
+ });
18
+ return new Promise((resolve, reject) => {
19
+ child.on('close', (code) => {
20
+ if (code !== 0) {
21
+ reject(new Error(`create-opensaas-app exited with code ${code}`));
22
+ }
23
+ else {
24
+ resolve();
25
+ }
26
+ });
27
+ child.on('error', (err) => {
28
+ reject(err);
29
+ });
30
+ });
342
31
  }
343
32
  //# sourceMappingURL=init.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAA;AAC5B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAA;AACxB,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,OAAO,MAAM,SAAS,CAAA;AAE7B,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,WAA+B;IAC/D,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAC,CAAA;IAE9D,0CAA0C;IAC1C,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC;YAC7B,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,MAAM;YACZ,OAAO,EAAE,eAAe;YACxB,OAAO,EAAE,iBAAiB;YAC1B,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE;gBAClB,IAAI,CAAC,KAAK;oBAAE,OAAO,0BAA0B,CAAA;gBAC7C,IAAI,CAAC,cAAc,CAAC,IAAI,C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C,KAAK,CAAC,GAAG,CAAC,wCAAwC,CAAC,CAAC,CAAA;IAEhE,+CAA+C;IAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,4BAA4B,EAAE,GAAG,IAAI,CAAC,EAAE;QAClE,KAAK,EAAE,SAAS;QAChB,KAAK,EAAE,IAAI;KACZ,CAAC,CAAA;IAEF,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;YACzB,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;gBACf,MAAM,CAAC,IAAI,KAAK,CAAC,wCAAwC,IAAI,EAAE,CAAC,CAAC,CAAA;YACnE,CAAC;iBAAM,CAAC;gBACN,OAAO,EAAE,CAAA;YACX,CAAC;QACH,CAAC,CAAC,CAAA;QAEF,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;YACxB,MAAM,CAAC,GAAG,CAAC,CAAA;QACb,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/generator/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAI1D;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,CAiD9D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAU7E"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/generator/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAI1D;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,CA8H9D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAU7E"}
@@ -9,10 +9,78 @@ import * as path from 'path';
9
9
  export function generateContext(config) {
10
10
  // Check if custom Prisma client constructor is provided
11
11
  const hasCustomConstructor = !!config.db.prismaClientConstructor;
12
+ // Check if storage is configured
13
+ const hasStorage = !!config.storage && Object.keys(config.storage).length > 0;
12
14
  // Generate the Prisma client instantiation code
13
15
  const prismaInstantiation = hasCustomConstructor
14
16
  ? `config.db.prismaClientConstructor!(PrismaClient)`
15
17
  : `new PrismaClient()`;
18
+ // Generate storage utilities if storage is configured
19
+ const storageUtilities = hasStorage
20
+ ? `
21
+ /**
22
+ * Lazy-loaded storage runtime functions
23
+ * Prevents sharp and other storage dependencies from being bundled in client code
24
+ */
25
+ let storageRuntime: typeof import('@opensaas/stack-storage/runtime') | null = null
26
+
27
+ async function getStorageRuntime() {
28
+ if (!storageRuntime) {
29
+ try {
30
+ storageRuntime = await import('@opensaas/stack-storage/runtime')
31
+ } catch (error) {
32
+ throw new Error(
33
+ 'Failed to load @opensaas/stack-storage/runtime. Make sure @opensaas/stack-storage is installed.'
34
+ )
35
+ }
36
+ }
37
+ return storageRuntime
38
+ }
39
+
40
+ /**
41
+ * Storage utilities for file/image uploads
42
+ */
43
+ const storage = {
44
+ uploadFile: async (providerName: string, file: File, buffer: Buffer, options?: unknown) => {
45
+ const runtime = await getStorageRuntime()
46
+ return runtime.uploadFile(config, providerName, { file, buffer }, options as any)
47
+ },
48
+
49
+ uploadImage: async (providerName: string, file: File, buffer: Buffer, options?: unknown) => {
50
+ const runtime = await getStorageRuntime()
51
+ return runtime.uploadImage(config, providerName, { file, buffer }, options as any)
52
+ },
53
+
54
+ deleteFile: async (providerName: string, filename: string) => {
55
+ const runtime = await getStorageRuntime()
56
+ return runtime.deleteFile(config, providerName, filename)
57
+ },
58
+
59
+ deleteImage: async (metadata: unknown) => {
60
+ const runtime = await getStorageRuntime()
61
+ return runtime.deleteImage(config, metadata as any)
62
+ },
63
+ }
64
+ `
65
+ : `
66
+ /**
67
+ * Storage utilities (not configured)
68
+ */
69
+ const storage = {
70
+ uploadFile: async () => {
71
+ throw new Error('Storage is not configured. Add storage providers to your opensaas.config.ts')
72
+ },
73
+ uploadImage: async () => {
74
+ throw new Error('Storage is not configured. Add storage providers to your opensaas.config.ts')
75
+ },
76
+ deleteFile: async () => {
77
+ throw new Error('Storage is not configured. Add storage providers to your opensaas.config.ts')
78
+ },
79
+ deleteImage: async () => {
80
+ throw new Error('Storage is not configured. Add storage providers to your opensaas.config.ts')
81
+ },
82
+ }
83
+ `;
16
84
  return `/**
17
85
  * Auto-generated context factory
18
86
  *
@@ -23,14 +91,16 @@ export function generateContext(config) {
23
91
  */
24
92
 
25
93
  import { getContext as getOpensaasContext } from '@opensaas/stack-core'
94
+ import type { Session as OpensaasSession } from '@opensaas/stack-core'
26
95
  import { PrismaClient } from './prisma-client'
96
+ import type { Context } from './types'
27
97
  import config from '../opensaas.config'
28
98
 
29
99
  // Internal Prisma singleton - managed automatically
30
100
  const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined }
31
101
  const prisma = globalForPrisma.prisma ?? ${prismaInstantiation}
32
102
  if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
33
-
103
+ ${storageUtilities}
34
104
  /**
35
105
  * Get OpenSaas context with optional session
36
106
  *
@@ -45,10 +115,15 @@ if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
45
115
  * // Authenticated access
46
116
  * const context = getContext({ userId: 'user-123' })
47
117
  * const myPosts = await context.db.post.findMany()
118
+ *
119
+ * // With custom session type
120
+ * type CustomSession = { userId: string; email: string; role: string } | null
121
+ * const context = getContext<CustomSession>({ userId: '123', email: 'user@example.com', role: 'admin' })
122
+ * // context.session is now typed as CustomSession
48
123
  * \`\`\`
49
124
  */
50
- export function getContext(session?: { userId?: string; [key: string]: unknown } | null) {
51
- return getOpensaasContext(config, prisma, session ?? null)
125
+ export function getContext<TSession extends OpensaasSession = OpensaasSession>(session?: TSession): Context<TSession> {
126
+ return getOpensaasContext(config, prisma, session ?? null, storage) as Context<TSession>
52
127
  }
53
128
 
54
129
  export const rawOpensaasContext = getContext()
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/generator/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAA;AACxB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAA;AAE5B;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,MAAsB;IACpD,wDAAwD;IACxD,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,uBAAuB,CAAA;IAEhE,gDAAgD;IAChD,MAAM,mBAAmB,GAAG,oBAAoB;QAC9C,CAAC,CAAC,kDAAkD;QACpD,CAAC,CAAC,oBAAoB,CAAA;IAExB,OAAO;;;;;;;;;;;;;;;2CAekC,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;CAwB7D,CAAA;AACD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,MAAsB,EAAE,UAAkB;IACrE,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,CAAA;IAEvC,0BAA0B;IAC1B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IACpC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACxB,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;IACxC,CAAC;IAED,EAAE,CAAC,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;AAChD,CAAC"}
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/generator/context.ts"],"names":[],"mappings":"AACA,OAA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-2,4 +2,5 @@ export { generatePrismaSchema, writePrismaSchema } from './prisma.js';
2
2
  export { generateTypes, writeTypes } from './types.js';
3
3
  export { patchPrismaTypes } from './type-patcher.js';
4
4
  export { generateContext, writeContext } from './context.js';
5
+ export { generateMcp, writeMcp } from './mcp.js';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/generator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/generator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA"}
@@ -2,4 +2,5 @@ export { generatePrismaSchema, writePrismaSchema } from './prisma.js';
2
2
  export { generateTypes, writeTypes } from './types.js';
3
3
  export { patchPrismaTypes } from './type-patcher.js';
4
4
  export { generateContext, writeContext } from './context.js';
5
+ export { generateMcp, writeMcp } from './mcp.js';
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * MCP metadata generation
3
+ * Generates reference files for MCP configuration
4
+ */
5
+ import type { OpenSaasConfig } from '@opensaas/stack-core';
6
+ /**
7
+ * Generate MCP metadata if MCP is enabled
8
+ */
9
+ export declare function generateMcp(config: OpenSaasConfig, outputPath: string): boolean;
10
+ /**
11
+ * Write MCP metadata to disk
12
+ */
13
+ export declare function writeMcp(config: OpenSaasConfig, outputPath: string): boolean;
14
+ //# sourceMappingURL=mcp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../src/generator/mcp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG1D;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CA8L/E;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,WAYlE"}