@mastra/deployer-vercel 0.0.1-alpha.8 → 0.1.0-alpha.37

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.
package/src/index.ts CHANGED
@@ -1,7 +1,10 @@
1
- import { MastraDeployer } from '@mastra/core';
1
+ import { Deployer } from '@mastra/deployer';
2
+ import { getBundler } from '@mastra/deployer/build';
3
+ import virtual from '@rollup/plugin-virtual';
2
4
  import * as child_process from 'child_process';
3
5
  import { readFileSync, writeFileSync } from 'fs';
4
6
  import { join } from 'path';
7
+ import process from 'process';
5
8
 
6
9
  interface EnvVar {
7
10
  key: string;
@@ -15,15 +18,22 @@ interface VercelError {
15
18
  code: string;
16
19
  }
17
20
 
18
- export class VercelDeployer extends MastraDeployer {
19
- constructor({ scope, env, projectName }: { env?: Record<string, any>; scope: string; projectName: string }) {
20
- super({ scope, env, projectName });
21
+ export class VercelDeployer extends Deployer {
22
+ private teamId: string;
23
+ private projectName: string;
24
+ private token: string;
25
+
26
+ constructor({ teamId, projectName, token }: { teamId: string; projectName: string; token: string }) {
27
+ super({ name: 'VERCEL' });
28
+
29
+ this.teamId = teamId;
30
+ this.projectName = projectName;
31
+ this.token = token;
21
32
  }
22
- writeFiles({ dir }: { dir: string }): void {
23
- this.writeIndex({ dir });
24
33
 
34
+ writeFiles(outputDirectory: string): void {
25
35
  writeFileSync(
26
- join(dir, 'vercel.json'),
36
+ join(outputDirectory, 'vercel.json'),
27
37
  JSON.stringify(
28
38
  {
29
39
  version: 2,
@@ -58,23 +68,15 @@ export class VercelDeployer extends MastraDeployer {
58
68
  }
59
69
  }
60
70
 
61
- async syncEnv({ scope, dir, token }: { token: string; dir: string; scope: string }) {
62
- const envFiles = this.getEnvFiles();
63
- const envVars: string[] = [];
64
-
65
- for (const file of envFiles) {
66
- const vars = this.parseEnvFile(file);
67
- envVars.push(...vars);
68
- }
69
-
71
+ private async syncEnv(envVars: Map<string, string>) {
70
72
  console.log('Syncing environment variables...');
71
73
 
72
74
  // Transform env vars into the format expected by Vercel API
73
- const vercelEnvVars: EnvVar[] = envVars.map(envVar => {
74
- const [key, value] = envVar.split('=');
75
+ const vercelEnvVars: EnvVar[] = Array.from(envVars.entries()).map(([key, value]) => {
75
76
  if (!key || !value) {
76
- throw new Error(`Invalid environment variable format: ${envVar}`);
77
+ throw new Error(`Invalid environment variable format: ${key || value}`);
77
78
  }
79
+
78
80
  return {
79
81
  key,
80
82
  value,
@@ -84,16 +86,19 @@ export class VercelDeployer extends MastraDeployer {
84
86
  });
85
87
 
86
88
  try {
87
- const projectId = this.getProjectId({ dir });
89
+ const projectId = this.getProjectId({ dir: process.cwd() });
88
90
 
89
- const response = await fetch(`https://api.vercel.com/v10/projects/${projectId}/env?teamId=${scope}&upsert=true`, {
90
- method: 'POST',
91
- headers: {
92
- Authorization: `Bearer ${token}`,
93
- 'Content-Type': 'application/json',
91
+ const response = await fetch(
92
+ `https://api.vercel.com/v10/projects/${projectId}/env?teamId=${this.teamId}&upsert=true`,
93
+ {
94
+ method: 'POST',
95
+ headers: {
96
+ Authorization: `Bearer ${this.token}`,
97
+ 'Content-Type': 'application/json',
98
+ },
99
+ body: JSON.stringify(vercelEnvVars),
94
100
  },
95
- body: JSON.stringify(vercelEnvVars),
96
- });
101
+ );
97
102
 
98
103
  if (!response.ok) {
99
104
  const error = (await response.json()) as VercelError;
@@ -111,63 +116,70 @@ export class VercelDeployer extends MastraDeployer {
111
116
  }
112
117
  }
113
118
 
114
- async deploy({ dir, token }: { dir: string; token: string }): Promise<void> {
115
- // Get env vars for initial deployment
116
- const envFiles = this.getEnvFiles();
117
- const envVars: string[] = [];
119
+ async prepare(outputDirectory: string): Promise<void> {
120
+ await super.prepare(outputDirectory);
121
+ await this.writeFiles(outputDirectory);
122
+ }
118
123
 
119
- for (const file of envFiles) {
120
- const vars = this.parseEnvFile(file);
121
- envVars.push(...vars);
122
- }
124
+ private getEntry(): string {
125
+ return `
126
+ import { handle } from 'hono/vercel'
127
+ import { mastra } from '#mastra';
128
+ import { createHonoServer } from '#server';
129
+
130
+ const app = await createHonoServer(mastra);
131
+
132
+ export const GET = handle(app);
133
+ export const POST = handle(app);
134
+ `;
135
+ }
136
+
137
+ async bundle(mastraDir: string, outputDirectory: string): Promise<void> {
138
+ const bundler = await getBundler({
139
+ input: '#entry',
140
+ plugins: [virtual({ '#entry': this.getEntry() })],
141
+ });
142
+
143
+ await bundler.write({
144
+ inlineDynamicImports: true,
145
+ file: `${outputDirectory}/index.mjs`,
146
+ format: 'es',
147
+ });
148
+ }
149
+
150
+ async deploy(outputDirectory: string): Promise<void> {
151
+ const envVars = await this.loadEnvVars();
123
152
 
124
153
  // Create the command array with base arguments
125
154
  const commandArgs = [
126
155
  '--scope',
127
- this.scope as string,
156
+ this.teamId as string,
128
157
  '--cwd',
129
- dir,
130
- 'deploy',
158
+ outputDirectory,
131
159
  '--token',
132
- token,
160
+ this.token,
161
+ 'deploy',
133
162
  '--yes',
134
163
  ...(this.projectName ? ['--name', this.projectName] : []),
135
164
  ];
136
165
 
137
- // Add env vars to initial deployment
138
- for (const envVar of envVars) {
139
- commandArgs.push('--env', envVar);
140
- }
141
-
142
166
  // Run the Vercel deploy command
143
- child_process.execSync(`vercel ${commandArgs.join(' ')}`, {
144
- cwd: dir,
167
+ child_process.execSync(`npx vercel ${commandArgs.join(' ')}`, {
168
+ cwd: outputDirectory,
145
169
  env: {
146
- ...this.env,
170
+ // ...this.env,
147
171
  PATH: process.env.PATH,
148
172
  },
149
173
  stdio: 'inherit',
150
174
  });
151
175
 
152
- console.log('Deployment started on Vercel. You can wait for it to finish or exit this command.');
176
+ this.logger.info('Deployment started on Vercel. You can wait for it to finish or exit this command.');
153
177
 
154
- if (envVars.length > 0) {
178
+ if (envVars.size > 0) {
155
179
  // Sync environment variables for future deployments
156
- await this.syncEnv({ scope: this.scope, dir, token });
180
+ await this.syncEnv(envVars);
157
181
  } else {
158
- console.log('\nAdd your ENV vars to .env or your vercel dashboard.\n');
182
+ this.logger.info('\nAdd your ENV vars to .env or your vercel dashboard.\n');
159
183
  }
160
184
  }
161
-
162
- writeIndex({ dir }: { dir: string }): void {
163
- writeFileSync(
164
- join(dir, 'index.mjs'),
165
- `
166
- import { handle } from 'hono/vercel'
167
- import { app } from './hono.mjs';
168
- export const GET = handle(app);
169
- export const POST = handle(app);
170
- `,
171
- );
172
- }
173
185
  }
package/tsconfig.json CHANGED
@@ -1,10 +1,5 @@
1
1
  {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "moduleResolution": "bundler",
5
- "outDir": "./dist",
6
- "rootDir": "./src"
7
- },
2
+ "extends": "../../tsconfig.node.json",
8
3
  "include": ["src/**/*"],
9
4
  "exclude": ["node_modules", "**/*.test.ts"]
10
5
  }
package/vitest.config.ts CHANGED
@@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config';
2
2
 
3
3
  export default defineConfig({
4
4
  test: {
5
- globals: true,
5
+ environment: 'node',
6
6
  include: ['src/**/*.test.ts'],
7
7
  },
8
8
  });