@motherbase/cli 0.2.2 → 0.2.4
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/README.md +24 -0
- package/package.json +4 -5
- package/src/api.js +11 -3
- package/src/commands/functions.js +274 -112
- package/src/commands/gen.js +126 -0
- package/src/commands/index.js +10 -1
- package/src/commands/keys.js +72 -0
- package/src/commands/local.js +273 -64
- package/src/commands/mcp.js +43 -0
- package/src/commands/rls.js +93 -2
- package/src/main.js +5 -0
- package/src/mcp/protocol.js +149 -0
- package/src/mcp/tools.js +166 -0
- package/src/prompt.js +61 -14
package/README.md
CHANGED
|
@@ -84,9 +84,33 @@ Global flags: `--json`, `--yes/-y`, `--project/-p <id>`, `--url/-u <consoleUrl>`
|
|
|
84
84
|
- **Cron**: `cron list|create|delete|toggle`
|
|
85
85
|
- **Config**: `config list|get|set`
|
|
86
86
|
- **Inspect**: `inspect logs|stats|performance|requests|advisor|shield`
|
|
87
|
+
- **Codegen**: `gen types typescript [--schema public] [--local] [--output <file>] [--check]`
|
|
87
88
|
|
|
88
89
|
Run `motherbase <group> -h` for per-group usage.
|
|
89
90
|
|
|
91
|
+
## Type generation
|
|
92
|
+
|
|
93
|
+
Generate TypeScript definitions for the live schema, in the shape supabase-js
|
|
94
|
+
emits, so `createClient<Database>(url, key)` types every row, rejects unknown
|
|
95
|
+
columns and types `.rpc()` calls:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
motherbase gen types typescript > database.types.ts
|
|
99
|
+
motherbase gen types typescript --schema shop --output src/shop.types.ts
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
With no `--output` the file goes to stdout and nothing else does, so the
|
|
103
|
+
redirect above produces a compiling file. `--local` skips the Console proxy and
|
|
104
|
+
talks to a project API on this machine (`MOTHERBASE_URL`, default
|
|
105
|
+
`http://localhost:5001`, with `MOTHERBASE_ADMIN_TOKEN`).
|
|
106
|
+
|
|
107
|
+
Gate CI on schema drift with `--check`, which never writes and exits non-zero
|
|
108
|
+
when the committed file differs from a fresh generation:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
motherbase gen types typescript --check --output database.types.ts
|
|
112
|
+
```
|
|
113
|
+
|
|
90
114
|
## Supabase migration
|
|
91
115
|
|
|
92
116
|
Preview a Supabase import without writing to Motherbase:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motherbase/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Motherbase operator CLI — drives the Platform Console and linked projects.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"bin": {
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"node": ">=18"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
|
-
"test": "node --test"
|
|
13
|
+
"test": "node --test",
|
|
14
|
+
"lint": "eslint ."
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"bin",
|
|
@@ -21,7 +22,5 @@
|
|
|
21
22
|
"access": "public"
|
|
22
23
|
},
|
|
23
24
|
"license": "MIT",
|
|
24
|
-
"dependencies": {
|
|
25
|
-
"@motherbase/cli": "^0.2.1"
|
|
26
|
-
}
|
|
25
|
+
"dependencies": {}
|
|
27
26
|
}
|
package/src/api.js
CHANGED
|
@@ -143,12 +143,19 @@ function adminRequest(ctx, method, apiPath, { body, query } = {}) {
|
|
|
143
143
|
return doFetch(url, { method, token, body });
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
// The public URL of a project path (`/edge/<route>`, `/rest/v1/<table>`), for
|
|
147
|
+
// printing. Mirrors how the proxy addresses a project in each mode.
|
|
148
|
+
function projectUrl(ctx, projectPath) {
|
|
149
|
+
const target = ctx.requireProject();
|
|
150
|
+
return target.mode === 'direct'
|
|
151
|
+
? `${target.baseUrl}/api${projectPath}`
|
|
152
|
+
: `${target.consoleUrl}/api/projects/${target.projectId}${projectPath}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
146
155
|
// Edge invoke goes through the un-authenticated edge route, not /admin.
|
|
147
156
|
function edgeInvokeRequest(ctx, route, body) {
|
|
157
|
+
const url = projectUrl(ctx, `/edge/${encodeURIComponent(route)}`);
|
|
148
158
|
const target = ctx.requireProject();
|
|
149
|
-
const url = target.mode === 'direct'
|
|
150
|
-
? `${target.baseUrl}/api/edge/${encodeURIComponent(route)}`
|
|
151
|
-
: `${target.consoleUrl}/api/projects/${target.projectId}/edge/${encodeURIComponent(route)}`;
|
|
152
159
|
return doFetch(url, { method: 'POST', token: target.token, body: body || {} });
|
|
153
160
|
}
|
|
154
161
|
|
|
@@ -214,6 +221,7 @@ module.exports = {
|
|
|
214
221
|
loginRequest,
|
|
215
222
|
adminRequest,
|
|
216
223
|
edgeInvokeRequest,
|
|
224
|
+
projectUrl,
|
|
217
225
|
uploadFile,
|
|
218
226
|
downloadFile,
|
|
219
227
|
buildQuery,
|
|
@@ -1,112 +1,274 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const api = require('../api');
|
|
6
|
-
const ui = require('../ui');
|
|
7
|
-
const { output, requireArg } = require('./_util');
|
|
8
|
-
const { CliError } = require('../context');
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const api = require('../api');
|
|
6
|
+
const ui = require('../ui');
|
|
7
|
+
const { output, requireArg } = require('./_util');
|
|
8
|
+
const { CliError } = require('../context');
|
|
9
|
+
|
|
10
|
+
// Functions live as repo files, one directory per function, the layout Supabase
|
|
11
|
+
// projects already use:
|
|
12
|
+
//
|
|
13
|
+
// functions/
|
|
14
|
+
// stripe-webhook/
|
|
15
|
+
// index.ts
|
|
16
|
+
//
|
|
17
|
+
// The directory name is both the function name and its route, so deploying is
|
|
18
|
+
// `motherbase functions deploy` with nothing to remember.
|
|
19
|
+
const DEFAULT_DIR = 'functions';
|
|
20
|
+
const LEGACY_DIR = path.join('motherbase', 'functions');
|
|
21
|
+
const ENTRY_NAMES = ['index.ts', 'index.js', 'index.mjs'];
|
|
22
|
+
|
|
23
|
+
const TEMPLATE = `// Edge function: %NAME%
|
|
24
|
+
//
|
|
25
|
+
// Runs in a V8 isolate with the Fetch API available: Request, Response,
|
|
26
|
+
// Headers, URL, TextEncoder/TextDecoder and crypto (including subtle.sign, for
|
|
27
|
+
// webhook signature checks). npm imports are resolved at deploy time:
|
|
28
|
+
//
|
|
29
|
+
// import { z } from 'zod';
|
|
30
|
+
//
|
|
31
|
+
// \`env\` holds this project's edge secrets.
|
|
32
|
+
|
|
33
|
+
export default async (req: Request) => {
|
|
34
|
+
const body = req.method === 'GET' ? {} : await req.json().catch(() => ({}));
|
|
35
|
+
return Response.json({ hello: '%NAME%', received: body, at: new Date().toISOString() });
|
|
36
|
+
};
|
|
37
|
+
`;
|
|
38
|
+
|
|
39
|
+
function functionsDir(ctx) {
|
|
40
|
+
for (const candidate of [DEFAULT_DIR, LEGACY_DIR]) {
|
|
41
|
+
if (fs.existsSync(path.join(ctx.cwd, candidate))) return path.join(ctx.cwd, candidate);
|
|
42
|
+
}
|
|
43
|
+
return path.join(ctx.cwd, DEFAULT_DIR);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Entry file of a function directory, or null when it has none. */
|
|
47
|
+
function entryFile(dir) {
|
|
48
|
+
for (const name of ENTRY_NAMES) {
|
|
49
|
+
const file = path.join(dir, name);
|
|
50
|
+
if (fs.existsSync(file)) return file;
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Every locally defined function: `{ name, dir, entry }`. */
|
|
56
|
+
function localFunctions(ctx) {
|
|
57
|
+
const root = functionsDir(ctx);
|
|
58
|
+
if (!fs.existsSync(root)) return [];
|
|
59
|
+
return fs
|
|
60
|
+
.readdirSync(root, { withFileTypes: true })
|
|
61
|
+
.filter(item => item.isDirectory())
|
|
62
|
+
.map(item => ({ name: item.name, dir: path.join(root, item.name), entry: entryFile(path.join(root, item.name)) }))
|
|
63
|
+
.filter(fn => fn.entry);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function resolveLocal(ctx, name) {
|
|
67
|
+
const all = localFunctions(ctx);
|
|
68
|
+
if (!name) return all;
|
|
69
|
+
const hit = all.find(fn => fn.name === name);
|
|
70
|
+
if (!hit) {
|
|
71
|
+
const expected = path.relative(ctx.cwd, path.join(functionsDir(ctx), name, 'index.ts'));
|
|
72
|
+
throw new CliError(`No local function "${name}". Expected ${expected}.`);
|
|
73
|
+
}
|
|
74
|
+
return [hit];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Upsert by route, so deploying twice updates instead of conflicting. */
|
|
78
|
+
async function deployOne(ctx, fn, flags) {
|
|
79
|
+
const code = fs.readFileSync(fn.entry, 'utf8');
|
|
80
|
+
const body = { name: fn.name, route: fn.name, code };
|
|
81
|
+
if (flags && flags.memory) body.memoryLimitMb = Number(flags.memory);
|
|
82
|
+
if (flags && flags.timeout) body.timeoutMs = Number(flags.timeout);
|
|
83
|
+
|
|
84
|
+
const listing = await api.adminRequest(ctx, 'GET', '/edge-functions');
|
|
85
|
+
const existing = ((listing && listing.functions) || []).find(f => f.route === fn.name);
|
|
86
|
+
if (existing) return api.adminRequest(ctx, 'PATCH', `/edge-functions/${existing.id}`, { body });
|
|
87
|
+
return api.adminRequest(ctx, 'POST', '/edge-functions', { body: { ...body, enabled: true } });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const subcommands = {
|
|
91
|
+
list: {
|
|
92
|
+
summary: 'List edge functions',
|
|
93
|
+
usage: 'motherbase functions list',
|
|
94
|
+
async run(ctx) {
|
|
95
|
+
const data = await api.adminRequest(ctx, 'GET', '/edge-functions');
|
|
96
|
+
const rows = (data && data.functions) || [];
|
|
97
|
+
output(ctx, data, (_d, u) =>
|
|
98
|
+
u.table(rows, [
|
|
99
|
+
{ key: 'id', label: 'ID' },
|
|
100
|
+
{ key: 'name', label: 'NAME' },
|
|
101
|
+
{ key: 'route', label: 'ROUTE' },
|
|
102
|
+
{ key: 'enabled', label: 'ENABLED' },
|
|
103
|
+
]),
|
|
104
|
+
);
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
get: {
|
|
108
|
+
summary: 'Get an edge function by id',
|
|
109
|
+
usage: 'motherbase functions get <id>',
|
|
110
|
+
async run(ctx, args) {
|
|
111
|
+
const id = requireArg(args[0], 'id');
|
|
112
|
+
const data = await api.adminRequest(ctx, 'GET', `/edge-functions/${id}`);
|
|
113
|
+
output(ctx, data, (d, u) => u.keyValue((d && d.function) || d || {}));
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
new: {
|
|
117
|
+
summary: 'Scaffold functions/<name>/index.ts',
|
|
118
|
+
usage: 'motherbase functions new <name>',
|
|
119
|
+
async run(ctx, args) {
|
|
120
|
+
const name = requireArg(args[0], 'name');
|
|
121
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) {
|
|
122
|
+
throw new CliError('Function names are lowercase letters, digits and hyphens (the name is also the route).');
|
|
123
|
+
}
|
|
124
|
+
const dir = path.join(functionsDir(ctx), name);
|
|
125
|
+
if (entryFile(dir)) throw new CliError(`${path.relative(ctx.cwd, dir)} already has an entry file.`);
|
|
126
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
127
|
+
const file = path.join(dir, 'index.ts');
|
|
128
|
+
fs.writeFileSync(file, TEMPLATE.replace(/%NAME%/g, name));
|
|
129
|
+
ui.success(`Created ${path.relative(ctx.cwd, file)}`);
|
|
130
|
+
ui.info(`Deploy it with: motherbase functions deploy ${name}`);
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
deploy: {
|
|
134
|
+
summary: 'Deploy local functions (all of them, or one by name)',
|
|
135
|
+
usage: 'motherbase functions deploy [name] [--memory <mb>] [--timeout <ms>] [--file <f> --route <r>]',
|
|
136
|
+
async run(ctx, args, flags) {
|
|
137
|
+
// The single-file form predates the directory layout and stays supported:
|
|
138
|
+
// scripts and older docs pass --file.
|
|
139
|
+
if (flags.file) {
|
|
140
|
+
const route = flags.route || args[0];
|
|
141
|
+
if (!route) throw new CliError('--route is required with --file');
|
|
142
|
+
const code = fs.readFileSync(path.resolve(ctx.cwd, flags.file), 'utf8');
|
|
143
|
+
const name = flags.name || route;
|
|
144
|
+
const listing = await api.adminRequest(ctx, 'GET', '/edge-functions');
|
|
145
|
+
const existing = ((listing && listing.functions) || []).find(f => f.route === route);
|
|
146
|
+
const data = existing
|
|
147
|
+
? await api.adminRequest(ctx, 'PATCH', `/edge-functions/${existing.id}`, { body: { name, route, code } })
|
|
148
|
+
: await api.adminRequest(ctx, 'POST', '/edge-functions', { body: { name, route, code, enabled: true } });
|
|
149
|
+
return output(ctx, data, (_d, u) => u.success(`Deployed function ${name} (${route})`));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const targets = resolveLocal(ctx, args[0]);
|
|
153
|
+
if (!targets.length) {
|
|
154
|
+
const where = path.relative(ctx.cwd, functionsDir(ctx));
|
|
155
|
+
throw new CliError(`No functions in ${where}. Create one with: motherbase functions new <name>`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const deployed = [];
|
|
159
|
+
for (const fn of targets) {
|
|
160
|
+
await deployOne(ctx, fn, flags);
|
|
161
|
+
deployed.push(fn.name);
|
|
162
|
+
}
|
|
163
|
+
output(ctx, { deployed }, (_d, u) => u.success(`Deployed ${deployed.join(', ')}`));
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
pull: {
|
|
167
|
+
summary: 'Write deployed function source back to disk',
|
|
168
|
+
usage: 'motherbase functions pull [name]',
|
|
169
|
+
async run(ctx, args) {
|
|
170
|
+
const listing = await api.adminRequest(ctx, 'GET', '/edge-functions');
|
|
171
|
+
const all = (listing && listing.functions) || [];
|
|
172
|
+
const wanted = args[0] ? all.filter(f => f.route === args[0] || f.name === args[0]) : all;
|
|
173
|
+
if (!wanted.length) throw new CliError(args[0] ? `No deployed function "${args[0]}"` : 'No deployed functions');
|
|
174
|
+
|
|
175
|
+
const written = [];
|
|
176
|
+
for (const fn of wanted) {
|
|
177
|
+
// The list endpoint may omit code, so read the function itself.
|
|
178
|
+
const detail = await api.adminRequest(ctx, 'GET', `/edge-functions/${fn.id}`);
|
|
179
|
+
const code = (detail && detail.function ? detail.function.code : detail && detail.code) || '';
|
|
180
|
+
const dir = path.join(functionsDir(ctx), fn.route || fn.name);
|
|
181
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
182
|
+
const file = path.join(dir, 'index.ts');
|
|
183
|
+
fs.writeFileSync(file, code);
|
|
184
|
+
written.push(path.relative(ctx.cwd, file));
|
|
185
|
+
}
|
|
186
|
+
output(ctx, { written }, (_d, u) => u.success(`Wrote ${written.join(', ')}`));
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
serve: {
|
|
190
|
+
summary: 'Watch local functions and redeploy on every save',
|
|
191
|
+
usage: 'motherbase functions serve [name]',
|
|
192
|
+
async run(ctx, args) {
|
|
193
|
+
// Deliberately not a second local runtime. The isolate, the secrets and
|
|
194
|
+
// the bundler all live in the project API, and a local re-implementation
|
|
195
|
+
// would differ from production in exactly the ways that matter. This
|
|
196
|
+
// watches and redeploys instead, so the edit-invoke loop is one save long.
|
|
197
|
+
const targets = resolveLocal(ctx, args[0]);
|
|
198
|
+
if (!targets.length) {
|
|
199
|
+
throw new CliError('Nothing to serve. Create a function with: motherbase functions new <name>');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
for (const fn of targets) {
|
|
203
|
+
await deployOne(ctx, fn);
|
|
204
|
+
ui.success(`Deployed ${fn.name}`);
|
|
205
|
+
ui.info(` POST ${api.projectUrl(ctx, `/edge/${fn.name}`)}`);
|
|
206
|
+
}
|
|
207
|
+
ui.info('Watching for changes. Ctrl-C to stop.');
|
|
208
|
+
|
|
209
|
+
const pending = new Map();
|
|
210
|
+
for (const fn of targets) {
|
|
211
|
+
fs.watch(fn.dir, { recursive: true }, () => {
|
|
212
|
+
// Editors save in bursts (write, rename, truncate). Coalescing them
|
|
213
|
+
// keeps one save from becoming three deploys.
|
|
214
|
+
clearTimeout(pending.get(fn.name));
|
|
215
|
+
pending.set(
|
|
216
|
+
fn.name,
|
|
217
|
+
setTimeout(async () => {
|
|
218
|
+
try {
|
|
219
|
+
await deployOne(ctx, fn);
|
|
220
|
+
ui.success(`Redeployed ${fn.name} at ${new Date().toLocaleTimeString()}`);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
ui.warn(`${fn.name}: ${error.message}`);
|
|
223
|
+
}
|
|
224
|
+
}, 150),
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Hold the process open; the watchers are the work.
|
|
230
|
+
await new Promise(() => {});
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
delete: {
|
|
234
|
+
summary: 'Delete an edge function by id',
|
|
235
|
+
usage: 'motherbase functions delete <id>',
|
|
236
|
+
async run(ctx, args) {
|
|
237
|
+
const id = requireArg(args[0], 'id');
|
|
238
|
+
const data = await api.adminRequest(ctx, 'DELETE', `/edge-functions/${id}`);
|
|
239
|
+
output(ctx, data, (d, u) => u.success(`Deleted function ${id}`));
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
invoke: {
|
|
243
|
+
summary: 'Invoke an edge function by route',
|
|
244
|
+
usage: 'motherbase functions invoke <route> [--body <json>]',
|
|
245
|
+
async run(ctx, args, flags) {
|
|
246
|
+
const route = requireArg(args[0], 'route');
|
|
247
|
+
let body = {};
|
|
248
|
+
if (flags.body) {
|
|
249
|
+
try { body = JSON.parse(flags.body); } catch { throw new CliError('--body must be valid JSON'); }
|
|
250
|
+
}
|
|
251
|
+
const data = await api.edgeInvokeRequest(ctx, route, body);
|
|
252
|
+
output(ctx, data, (d, u) => u.json(d));
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
logs: {
|
|
256
|
+
summary: 'Show logs for an edge function',
|
|
257
|
+
usage: 'motherbase functions logs <id>',
|
|
258
|
+
async run(ctx, args) {
|
|
259
|
+
const id = requireArg(args[0], 'id');
|
|
260
|
+
const data = await api.adminRequest(ctx, 'GET', `/edge-functions/${id}/logs`);
|
|
261
|
+
output(ctx, data, (d, u) => {
|
|
262
|
+
const logs = (d && d.logs) || d || [];
|
|
263
|
+
if (Array.isArray(logs)) logs.forEach(l => u.print(typeof l === 'string' ? l : JSON.stringify(l)));
|
|
264
|
+
else u.json(d);
|
|
265
|
+
});
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
module.exports = {
|
|
271
|
+
summary: 'Edge functions (repo files, deploy, invoke, logs)',
|
|
272
|
+
usage: 'motherbase functions <list|get|new|deploy|pull|serve|delete|invoke|logs> ...',
|
|
273
|
+
subcommands,
|
|
274
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Code generation from the live schema. Today: TypeScript database types.
|
|
4
|
+
//
|
|
5
|
+
// The server owns the emitter (apps/server/services/admin/typegen.service.ts).
|
|
6
|
+
// This command is a transport: it asks `GET /admin/schema/types` for the
|
|
7
|
+
// rendered file and writes it. Re-implementing the emitter here would guarantee
|
|
8
|
+
// drift between the CLI, the Studio download and the MCP tool within a release.
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
const api = require('../api');
|
|
13
|
+
const config = require('../config');
|
|
14
|
+
const { output } = require('./_util');
|
|
15
|
+
const { CliError } = require('../context');
|
|
16
|
+
|
|
17
|
+
const DEFAULT_OUTPUT = 'database.types.ts';
|
|
18
|
+
const DEFAULT_LOCAL_URL = 'http://localhost:5001';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `--local` bypasses the Console proxy and talks straight to a project API on
|
|
22
|
+
* this machine, which is how someone iterating on a local stack works: there is
|
|
23
|
+
* no console account, no link file, just a server on a port. Everything else
|
|
24
|
+
* about the context (flags, cwd, json mode) is preserved.
|
|
25
|
+
*/
|
|
26
|
+
function resolveTarget(ctx, flags) {
|
|
27
|
+
if (!flags.local) return ctx;
|
|
28
|
+
const baseUrl = config.normalizeUrl(flags.url || ctx.env.MOTHERBASE_URL || DEFAULT_LOCAL_URL);
|
|
29
|
+
const token = flags.token || ctx.env.MOTHERBASE_ADMIN_TOKEN || ctx.token;
|
|
30
|
+
return Object.assign({}, ctx, {
|
|
31
|
+
mode: 'direct',
|
|
32
|
+
requireProject: () => ({ mode: 'direct', baseUrl, token }),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Git checkouts on Windows may rewrite line endings. The generator always emits
|
|
37
|
+
// LF, so compare on normalised text: a CRLF working copy is not schema drift.
|
|
38
|
+
function normalize(text) {
|
|
39
|
+
return String(text).replace(/\r\n/g, '\n');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** First line where the two files diverge, 1-based, for a useful error. */
|
|
43
|
+
function firstDifference(a, b) {
|
|
44
|
+
const left = normalize(a).split('\n');
|
|
45
|
+
const right = normalize(b).split('\n');
|
|
46
|
+
for (let i = 0; i < Math.max(left.length, right.length); i++) {
|
|
47
|
+
if (left[i] !== right[i]) {
|
|
48
|
+
return { line: i + 1, expected: right[i], actual: left[i] };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const typescript = {
|
|
55
|
+
summary: 'Generate TypeScript types for the database schema',
|
|
56
|
+
usage:
|
|
57
|
+
'motherbase gen types typescript [--schema public] [--local] [--output database.types.ts] [--check]',
|
|
58
|
+
async run(ctx, args, flags) {
|
|
59
|
+
const schema = flags.schema || 'public';
|
|
60
|
+
const data = await api.adminRequest(resolveTarget(ctx, flags), 'GET', '/schema/types', {
|
|
61
|
+
query: { schema },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const types = data && typeof data.types === 'string' ? data.types : null;
|
|
65
|
+
if (!types) {
|
|
66
|
+
throw new CliError(
|
|
67
|
+
'The server returned no generated types. Check that the project runs a build with GET /admin/schema/types.',
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const outputPath = flags.output && flags.output !== true ? String(flags.output) : null;
|
|
72
|
+
|
|
73
|
+
// --check is the CI drift gate: compare the committed file with a fresh
|
|
74
|
+
// generation and fail loudly, without ever writing.
|
|
75
|
+
if (flags.check) {
|
|
76
|
+
const target = path.resolve(ctx.cwd, outputPath || DEFAULT_OUTPUT);
|
|
77
|
+
if (!fs.existsSync(target)) {
|
|
78
|
+
throw new CliError(`${target} does not exist. Run \`motherbase gen types typescript --output ${outputPath || DEFAULT_OUTPUT}\`.`);
|
|
79
|
+
}
|
|
80
|
+
const onDisk = fs.readFileSync(target, 'utf8');
|
|
81
|
+
const diff = firstDifference(onDisk, types);
|
|
82
|
+
if (diff) {
|
|
83
|
+
throw new CliError(
|
|
84
|
+
`${target} is out of date with the ${schema} schema (first difference at line ${diff.line}).\n` +
|
|
85
|
+
` on disk: ${diff.actual === undefined ? '(end of file)' : diff.actual}\n` +
|
|
86
|
+
` generated: ${diff.expected === undefined ? '(end of file)' : diff.expected}\n` +
|
|
87
|
+
'Re-run without --check to update it.',
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
output(ctx, { schema, output: target, upToDate: true }, (d, ui) =>
|
|
91
|
+
ui.success(`${target} is up to date`),
|
|
92
|
+
);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// No --output: write the file to stdout so `... > database.types.ts` works.
|
|
97
|
+
// Nothing else may go to stdout on this path or it lands in the file.
|
|
98
|
+
if (!outputPath) {
|
|
99
|
+
if (ctx.flags.json) {
|
|
100
|
+
output(ctx, { schema, types }, () => {});
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
process.stdout.write(types);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const target = path.resolve(ctx.cwd, outputPath);
|
|
108
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
109
|
+
fs.writeFileSync(target, types, 'utf8');
|
|
110
|
+
output(ctx, { schema, output: target, bytes: Buffer.byteLength(types) }, (d, ui) =>
|
|
111
|
+
ui.success(`Wrote ${target} (${schema} schema)`),
|
|
112
|
+
);
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const types = {
|
|
117
|
+
summary: 'Generate types from the database schema',
|
|
118
|
+
usage: 'motherbase gen types typescript [flags]',
|
|
119
|
+
subcommands: { typescript },
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
module.exports = {
|
|
123
|
+
summary: 'Generate code from the live schema',
|
|
124
|
+
usage: 'motherbase gen types typescript [--schema public] [--local] [--output <file>] [--check]',
|
|
125
|
+
subcommands: { types },
|
|
126
|
+
};
|
package/src/commands/index.js
CHANGED
|
@@ -36,11 +36,18 @@ const registry = {
|
|
|
36
36
|
storage: require('./storage'),
|
|
37
37
|
functions: require('./functions'),
|
|
38
38
|
secrets: require('./secrets'),
|
|
39
|
+
keys: require('./keys'),
|
|
39
40
|
realtime: require('./realtime'),
|
|
40
41
|
webhooks: require('./webhooks'),
|
|
41
42
|
cron: require('./cron'),
|
|
42
43
|
config: require('./config'),
|
|
43
44
|
inspect: require('./inspect'),
|
|
45
|
+
|
|
46
|
+
// ── Codegen ────────────────────────────────────────────────────────────────
|
|
47
|
+
gen: require('./gen'),
|
|
48
|
+
|
|
49
|
+
// ── AI agents ──────────────────────────────────────────────────────────────
|
|
50
|
+
mcp: require('./mcp'),
|
|
44
51
|
};
|
|
45
52
|
|
|
46
53
|
// Grouping used only to render a tidy top-level help listing.
|
|
@@ -49,7 +56,9 @@ const groups = [
|
|
|
49
56
|
{ title: 'Projects (Console)', names: ['projects'] },
|
|
50
57
|
{ title: 'Local self-host', names: ['init', 'start', 'stop', 'deploy', 'logs'] },
|
|
51
58
|
{ title: 'Database', names: ['db', 'migrations', 'migration', 'import'] },
|
|
52
|
-
{ title: 'Resources', names: ['rls', 'users', 'storage', 'functions', 'secrets', 'realtime', 'webhooks', 'cron', 'config', 'inspect'] },
|
|
59
|
+
{ title: 'Resources', names: ['rls', 'users', 'storage', 'functions', 'secrets', 'keys', 'realtime', 'webhooks', 'cron', 'config', 'inspect'] },
|
|
60
|
+
{ title: 'Codegen', names: ['gen'] },
|
|
61
|
+
{ title: 'AI agents', names: ['mcp'] },
|
|
53
62
|
];
|
|
54
63
|
|
|
55
64
|
module.exports = { registry, groups };
|