@mastra/deployer-vercel 0.0.1-alpha.9 → 0.1.0-alpha.38
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/CHANGELOG.md +251 -0
- package/README.md +95 -0
- package/dist/index.d.ts +17 -21
- package/dist/index.js +150 -6
- package/package.json +13 -19
- package/src/index.ts +75 -63
- package/tsconfig.json +1 -6
- package/vitest.config.ts +1 -1
- package/dist/deployer-vercel.cjs.development.js +0 -588
- package/dist/deployer-vercel.cjs.development.js.map +0 -1
- package/dist/deployer-vercel.cjs.production.min.js +0 -2
- package/dist/deployer-vercel.cjs.production.min.js.map +0 -1
- package/dist/deployer-vercel.esm.js +0 -565
- package/dist/deployer-vercel.esm.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
package/src/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
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
|
|
19
|
-
|
|
20
|
-
|
|
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(
|
|
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(
|
|
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(
|
|
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: ${
|
|
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(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
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
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
119
|
+
async prepare(outputDirectory: string): Promise<void> {
|
|
120
|
+
await super.prepare(outputDirectory);
|
|
121
|
+
await this.writeFiles(outputDirectory);
|
|
122
|
+
}
|
|
118
123
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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.
|
|
156
|
+
this.teamId as string,
|
|
128
157
|
'--cwd',
|
|
129
|
-
|
|
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:
|
|
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
|
-
|
|
176
|
+
this.logger.info('Deployment started on Vercel. You can wait for it to finish or exit this command.');
|
|
153
177
|
|
|
154
|
-
if (envVars.
|
|
178
|
+
if (envVars.size > 0) {
|
|
155
179
|
// Sync environment variables for future deployments
|
|
156
|
-
await this.syncEnv(
|
|
180
|
+
await this.syncEnv(envVars);
|
|
157
181
|
} else {
|
|
158
|
-
|
|
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
|
}
|