@mastra/deployer-netlify 0.0.1-alpha.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.
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@mastra/deployer-netlify",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/deployer-netlify.esm.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/deployer-netlify.esm.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "keywords": [],
23
+ "author": "",
24
+ "license": "ISC",
25
+ "dependencies": {
26
+ "date-fns": "^4.1.0",
27
+ "dotenv": "^16.3.1",
28
+ "execa": "^9.3.1",
29
+ "netlify-cli": "^18.0.1",
30
+ "zod": "^3.24.1",
31
+ "@mastra/core": "0.1.27-alpha.66",
32
+ "@mastra/deployer": "0.0.1-alpha.0"
33
+ },
34
+ "devDependencies": {
35
+ "@babel/preset-env": "^7.26.0",
36
+ "@babel/preset-typescript": "^7.26.0",
37
+ "@tsconfig/recommended": "^1.0.7",
38
+ "@types/jsdom": "^21.1.7",
39
+ "@types/node": "^22.9.0",
40
+ "@types/pg": "^8.11.10",
41
+ "dts-cli": "^2.0.5",
42
+ "vitest": "^2.1.8"
43
+ },
44
+ "scripts": {
45
+ "build": "dts build",
46
+ "build:dev": "dts watch",
47
+ "test": "vitest run"
48
+ }
49
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,60 @@
1
+ async function createNetlifySite({ token, name, scope }: { token: string; name: string; scope?: string }) {
2
+ console.log(token, name, scope);
3
+ const response = await fetch('https://api.netlify.com/api/v1/sites', {
4
+ method: 'POST',
5
+ headers: {
6
+ Authorization: `Bearer ${token}`,
7
+ 'Content-Type': 'application/json',
8
+ },
9
+ body: JSON.stringify({
10
+ name: name,
11
+ account_slug: scope, // Optional - if not provided, creates in user's default account
12
+ force_ssl: true, // Enable HTTPS
13
+ }),
14
+ });
15
+
16
+ const data = await response.json();
17
+
18
+ if (!response.ok) {
19
+ console.error(JSON.stringify(data));
20
+ throw new Error(`Failed to create site: ${data.message || 'Unknown error'}`);
21
+ }
22
+
23
+ return {
24
+ id: data.id,
25
+ name: data.name,
26
+ url: data.ssl_url || data.url,
27
+ adminUrl: data.admin_url,
28
+ };
29
+ }
30
+
31
+ async function findNetlifySite({ token, name, scope }: { token: string; name: string; scope: string }) {
32
+ const response = await fetch(`https://api.netlify.com/api/v1/${scope}/sites?filter=all&name=${name}`, {
33
+ headers: {
34
+ Authorization: `Bearer ${token}`,
35
+ 'Content-Type': 'application/json',
36
+ },
37
+ });
38
+
39
+ const data = await response.json();
40
+
41
+ if (!response.ok) {
42
+ throw new Error(`Failed to search sites: ${data.message || 'Unknown error'}`);
43
+ }
44
+
45
+ // Find exact match (filter can return partial matches)
46
+ return data.find((site: any) => site.name === name);
47
+ }
48
+
49
+ export async function getOrCreateSite({ token, name, scope }: { token: string; name: string; scope: string }) {
50
+ let existingSite;
51
+ try {
52
+ existingSite = await findNetlifySite({ token, name, scope });
53
+ } catch (e) {}
54
+
55
+ if (existingSite) {
56
+ return existingSite;
57
+ }
58
+
59
+ return createNetlifySite({ token, name, scope });
60
+ }
package/src/index.ts ADDED
@@ -0,0 +1,71 @@
1
+ import { MastraDeployer } from '@mastra/core';
2
+ import { execa } from 'execa';
3
+ import { existsSync, mkdirSync, renameSync, writeFileSync } from 'fs';
4
+ import { join } from 'path';
5
+
6
+ import { getOrCreateSite } from './helpers.js';
7
+
8
+ export class NetlifyDeployer extends MastraDeployer {
9
+ constructor({ scope, env, projectName }: { projectName: string; env?: Record<string, any>; scope: string }) {
10
+ super({ scope, env, projectName });
11
+ }
12
+
13
+ writeFiles({ dir }: { dir: string }): void {
14
+ if (!existsSync(join(dir, 'netlify/functions/api'))) {
15
+ mkdirSync(join(dir, 'netlify/functions/api'), { recursive: true });
16
+ }
17
+
18
+ // TODO ENV KEYS
19
+ writeFileSync(
20
+ join(dir, 'netlify.toml'),
21
+ `
22
+ [functions]
23
+ node_bundler = "esbuild"
24
+ directory = "/netlify/functions"
25
+
26
+ [[redirects]]
27
+ force = true
28
+ from = "/*"
29
+ status = 200
30
+ to = "/.netlify/functions/api/:splat"
31
+ `,
32
+ );
33
+
34
+ this.writeIndex({ dir });
35
+ }
36
+
37
+ async deploy({ dir, token }: { dir: string; token: string }): Promise<void> {
38
+ const site = await getOrCreateSite({ token, name: this.projectName || `mastra`, scope: this.scope });
39
+
40
+ const p2 = execa(
41
+ 'netlify',
42
+ ['deploy', '--site', site.id, '--auth', token, '--dir', '.', '--functions', './netlify/functions'],
43
+ {
44
+ cwd: dir,
45
+ },
46
+ );
47
+
48
+ p2.stdout.pipe(process.stdout);
49
+ await p2;
50
+ }
51
+
52
+ writeIndex({ dir }: { dir: string }): void {
53
+ ['mastra.mjs', 'hono.mjs', 'server.mjs'].forEach(file => {
54
+ renameSync(join(dir, file), join(dir, `netlify/functions/api/${file}`));
55
+ });
56
+
57
+ writeFileSync(
58
+ join(dir, 'netlify/functions/api/api.mts'),
59
+ `
60
+ export default async (req, context) => {
61
+ const { app } = await import('./hono.mjs');
62
+ // Pass the request directly to Hono
63
+ return app.fetch(req, {
64
+ // Optional context passing if needed
65
+ env: { context }
66
+ })
67
+ }
68
+ `,
69
+ );
70
+ }
71
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "moduleResolution": "bundler",
5
+ "outDir": "./dist",
6
+ "rootDir": "./src"
7
+ },
8
+ "include": ["src/**/*"],
9
+ "exclude": ["node_modules", "**/*.test.ts"]
10
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ include: ['src/**/*.test.ts'],
7
+ },
8
+ });