@motherbase/cli 0.2.5 → 0.2.8
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/LICENSE +21 -0
- package/README.md +2 -1
- package/bin/.motherbase/config.json +5 -0
- package/package.json +35 -26
- package/src/api.js +256 -241
- package/src/commands/_util.js +14 -1
- package/src/commands/db-catalog.js +326 -326
- package/src/commands/functions.js +304 -297
- package/src/commands/gen.js +7 -1
- package/src/commands/local.js +371 -273
- package/src/commands/login.js +45 -39
- package/src/commands/migrate.js +202 -179
- package/src/commands/migration.js +108 -108
- package/src/commands/projects.js +2 -1
- package/src/commands/storage.js +2 -2
- package/src/commands/users.js +142 -142
- package/src/context.js +6 -2
- package/src/docs.js +68 -0
- package/src/main.js +224 -194
- package/src/mcp/protocol.js +4 -1
- package/src/migration/commands.js +375 -375
- package/src/migration/files.js +258 -245
package/src/commands/login.js
CHANGED
|
@@ -1,39 +1,45 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const api = require('../api');
|
|
4
|
-
const config = require('../config');
|
|
5
|
-
const prompt = require('../prompt');
|
|
6
|
-
const ui = require('../ui');
|
|
7
|
-
const { CliError } = require('../context');
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
summary: 'Log in to a Motherbase Console (email/password → session token)',
|
|
11
|
-
usage: 'motherbase login [--url <consoleUrl>] [--email <email>]',
|
|
12
|
-
async run(ctx, args, flags) {
|
|
13
|
-
const consoleUrl = config.normalizeUrl(
|
|
14
|
-
flags.url || ctx.env.MOTHERBASE_CONSOLE_URL || ctx.consoleUrl || '',
|
|
15
|
-
);
|
|
16
|
-
if (!consoleUrl) {
|
|
17
|
-
throw new CliError('Console URL required. Use --url <consoleUrl> or set MOTHERBASE_CONSOLE_URL.');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Non-interactive: accept a token directly (CI).
|
|
21
|
-
if (flags.token) {
|
|
22
|
-
const email = flags.email || 'token';
|
|
23
|
-
config.saveConsoleToken(consoleUrl, flags.token, email);
|
|
24
|
-
ui.success(`Stored token for ${consoleUrl}`);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const api = require('../api');
|
|
4
|
+
const config = require('../config');
|
|
5
|
+
const prompt = require('../prompt');
|
|
6
|
+
const ui = require('../ui');
|
|
7
|
+
const { CliError } = require('../context');
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
summary: 'Log in to a Motherbase Console (email/password → session token)',
|
|
11
|
+
usage: 'motherbase login [--url <consoleUrl>] [--email <email>] [--code <totp>]',
|
|
12
|
+
async run(ctx, args, flags) {
|
|
13
|
+
const consoleUrl = config.normalizeUrl(
|
|
14
|
+
flags.url || ctx.env.MOTHERBASE_CONSOLE_URL || ctx.consoleUrl || '',
|
|
15
|
+
);
|
|
16
|
+
if (!consoleUrl) {
|
|
17
|
+
throw new CliError('Console URL required. Use --url <consoleUrl> or set MOTHERBASE_CONSOLE_URL.');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Non-interactive: accept a token directly (CI).
|
|
21
|
+
if (flags.token) {
|
|
22
|
+
const email = flags.email || 'token';
|
|
23
|
+
config.saveConsoleToken(consoleUrl, flags.token, email);
|
|
24
|
+
ui.success(`Stored token for ${consoleUrl}`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const email = flags.email || (await prompt.question('Email: '));
|
|
30
|
+
if (!email) throw new CliError('email is required');
|
|
31
|
+
const password = (typeof flags.password === 'string' && flags.password) || (await prompt.password('Password: '));
|
|
32
|
+
if (!password) throw new CliError('password is required');
|
|
33
|
+
|
|
34
|
+
let data = await api.loginRequest(consoleUrl, email, password);
|
|
35
|
+
if (data.twoFactorCookie) {
|
|
36
|
+
const code = (typeof flags.code === 'string' && flags.code) || (await prompt.question('Authenticator code: '));
|
|
37
|
+
if (!code) throw new CliError('two-factor code is required');
|
|
38
|
+
data = await api.verifyTotpRequest(consoleUrl, data.twoFactorCookie, code);
|
|
39
|
+
}
|
|
40
|
+
const token = data.token;
|
|
41
|
+
if (!token) throw new CliError('login succeeded but no token was returned');
|
|
42
|
+
config.saveConsoleToken(consoleUrl, token, email);
|
|
43
|
+
ui.success(`Logged in to ${consoleUrl} as ${email}`);
|
|
44
|
+
},
|
|
45
|
+
};
|
package/src/commands/migrate.js
CHANGED
|
@@ -1,179 +1,202 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// `motherbase migrate codemod <dir>`: port a Supabase codebase's imports, and
|
|
4
|
-
// report what this SDK does not implement.
|
|
5
|
-
//
|
|
6
|
-
// The rewrite is trivial and the report is the point. `@motherbase/client` is
|
|
7
|
-
// supabase-js shaped
|
|
8
|
-
//
|
|
9
|
-
// build time beats finding them when a user
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
{ from: '@supabase/
|
|
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
|
-
for (const
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// `motherbase migrate codemod <dir>`: port a Supabase codebase's imports, and
|
|
4
|
+
// report what this SDK does not implement.
|
|
5
|
+
//
|
|
6
|
+
// The rewrite is trivial and the report is the point. `@motherbase/client` is
|
|
7
|
+
// supabase-js shaped (auth aside: it is Better Auth's client), so most files
|
|
8
|
+
// need one import line changed; what breaks a port is the calls that do not
|
|
9
|
+
// exist here, and finding those at build time beats finding them when a user
|
|
10
|
+
// hits that screen.
|
|
11
|
+
//
|
|
12
|
+
// Deliberately a text pass, not an AST pass. A codemod that needs a TypeScript
|
|
13
|
+
// parser needs typescript as a CLI dependency, and the patterns below are
|
|
14
|
+
// import specifiers and method names, which do not need one. The cost is that a
|
|
15
|
+
// match inside a comment or a string is still reported: over-reporting in a
|
|
16
|
+
// list a human reads is the acceptable direction to be wrong in.
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const { output } = require('./_util');
|
|
21
|
+
const { CliError } = require('../context');
|
|
22
|
+
|
|
23
|
+
const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.mts', '.cts']);
|
|
24
|
+
const SKIP_DIRECTORIES = new Set(['node_modules', '.git', 'dist', 'build', '.next', 'coverage', '.turbo']);
|
|
25
|
+
|
|
26
|
+
/** Import specifiers that map one to one. */
|
|
27
|
+
const IMPORT_REWRITES = [
|
|
28
|
+
{ from: '@supabase/supabase-js', to: '@motherbase/client' },
|
|
29
|
+
{ from: '@supabase/ssr', to: '@motherbase/ssr' },
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* supabase-js auth calls, with the Better Auth call that replaces each one.
|
|
34
|
+
* `client.auth` is Better Auth's own client, and it turns an unknown method
|
|
35
|
+
* into a request that answers 404, so these fail at runtime, not at import.
|
|
36
|
+
*/
|
|
37
|
+
function authRenames(renames) {
|
|
38
|
+
return Object.entries(renames).map(([method, replacement]) => ({
|
|
39
|
+
pattern: new RegExp(`\\.auth\\.${method}\\s*\\(`, 'g'),
|
|
40
|
+
what: `auth.${method}()`,
|
|
41
|
+
advice: `Use ${replacement}.`,
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Calls this SDK does not implement, and what to do instead.
|
|
47
|
+
*
|
|
48
|
+
* Kept short and kept true: every entry here is a row marked `Not supported` in
|
|
49
|
+
* packages/client/COMPATIBILITY.md. An entry that becomes supported and is left
|
|
50
|
+
* behind sends people to rewrite working code, so this list is part of the same
|
|
51
|
+
* change as implementing the feature.
|
|
52
|
+
*/
|
|
53
|
+
const UNSUPPORTED = [
|
|
54
|
+
{
|
|
55
|
+
pattern: /\.schema\(\s*['"`](?!public['"`])/g,
|
|
56
|
+
what: 'schema() on a schema other than public',
|
|
57
|
+
advice: 'The REST API exposes public only. Move the tables, or expose them through a view or an RPC.',
|
|
58
|
+
},
|
|
59
|
+
...authRenames({
|
|
60
|
+
signInWithPassword: 'auth.signIn.email({ email, password })',
|
|
61
|
+
signUp: 'auth.signUp.email({ email, password, name })',
|
|
62
|
+
signInWithOAuth: 'auth.signIn.social({ provider, callbackURL })',
|
|
63
|
+
signInWithIdToken: 'auth.signIn.social({ provider, idToken: { token } })',
|
|
64
|
+
signInWithOtp: "auth.emailOtp.sendVerificationOtp({ email, type: 'sign-in' }), or auth.phoneNumber.sendOtp({ phoneNumber })",
|
|
65
|
+
verifyOtp: 'auth.signIn.emailOtp({ email, otp }), or auth.phoneNumber.verify({ phoneNumber, code })',
|
|
66
|
+
signInAnonymously: 'auth.signIn.anonymous()',
|
|
67
|
+
signInWithSSO: 'auth.signIn.sso({ email, callbackURL })',
|
|
68
|
+
getUser: 'auth.getSession(), which asks the server and returns { session, user }',
|
|
69
|
+
refreshSession: 'auth.getSession(): Better Auth sessions are revalidated, not rotated',
|
|
70
|
+
setSession: 'a storage adapter holding the session, or auth.getSessionFromUrl() on an OAuth return',
|
|
71
|
+
resetPasswordForEmail: 'auth.emailOtp.requestPasswordReset({ email }), then auth.emailOtp.resetPassword({ email, otp, password })',
|
|
72
|
+
getUserIdentities: 'auth.listAccounts()',
|
|
73
|
+
linkIdentity: 'auth.linkSocial({ provider, callbackURL })',
|
|
74
|
+
unlinkIdentity: 'auth.unlinkAccount({ providerId })',
|
|
75
|
+
getClaims: 'auth.token(), which returns a JWT for the session',
|
|
76
|
+
startAutoRefresh: 'nothing: the client revalidates on a 401',
|
|
77
|
+
}),
|
|
78
|
+
{
|
|
79
|
+
pattern: /\.auth\.mfa\./g,
|
|
80
|
+
what: 'auth.mfa.*',
|
|
81
|
+
advice: 'Use auth.twoFactor: enable({ password }), verifyTotp({ code }), disable({ password }).',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
pattern: /\.delete\(\s*\)\s*(?![\s\S]{0,200}?\.(eq|neq|in|gt|gte|lt|lte|like|ilike|is|filter|match|or|not)\()/g,
|
|
85
|
+
what: 'delete() with no filter',
|
|
86
|
+
advice: 'Refused with 400 on purpose, so a missing filter cannot empty a table. Add a filter.',
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
function walk(dir, files = []) {
|
|
91
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
92
|
+
if (entry.name.startsWith('.') && entry.name !== '.') continue;
|
|
93
|
+
const full = path.join(dir, entry.name);
|
|
94
|
+
if (entry.isDirectory()) {
|
|
95
|
+
if (SKIP_DIRECTORIES.has(entry.name)) continue;
|
|
96
|
+
walk(full, files);
|
|
97
|
+
} else if (SOURCE_EXTENSIONS.has(path.extname(entry.name))) {
|
|
98
|
+
files.push(full);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return files;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Line number of an offset, 1-based, for a report a human can act on. */
|
|
105
|
+
function lineAt(text, index) {
|
|
106
|
+
let line = 1;
|
|
107
|
+
for (let i = 0; i < index && i < text.length; i++) if (text[i] === '\n') line++;
|
|
108
|
+
return line;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function rewriteImports(text) {
|
|
112
|
+
let out = text;
|
|
113
|
+
let changed = 0;
|
|
114
|
+
for (const { from, to } of IMPORT_REWRITES) {
|
|
115
|
+
// Only inside a quoted specifier, so a mention in prose is left alone.
|
|
116
|
+
const pattern = new RegExp(`(['"\`])${from.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}\\1`, 'g');
|
|
117
|
+
out = out.replace(pattern, match => {
|
|
118
|
+
changed++;
|
|
119
|
+
return match.replace(from, to);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return { text: out, changed };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function scanUnsupported(text, file) {
|
|
126
|
+
const findings = [];
|
|
127
|
+
for (const rule of UNSUPPORTED) {
|
|
128
|
+
rule.pattern.lastIndex = 0;
|
|
129
|
+
let match;
|
|
130
|
+
while ((match = rule.pattern.exec(text)) !== null) {
|
|
131
|
+
findings.push({ file, line: lineAt(text, match.index), what: rule.what, advice: rule.advice });
|
|
132
|
+
if (match.index === rule.pattern.lastIndex) rule.pattern.lastIndex++;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return findings;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const codemod = {
|
|
139
|
+
summary: 'Rewrite Supabase imports and report what this SDK does not support',
|
|
140
|
+
usage: 'motherbase migrate codemod <dir> [--write]',
|
|
141
|
+
async run(ctx, args, flags) {
|
|
142
|
+
const target = path.resolve(ctx.cwd, args[0] || '.');
|
|
143
|
+
if (!fs.existsSync(target) || !fs.statSync(target).isDirectory()) {
|
|
144
|
+
throw new CliError(`${target} is not a directory.`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const files = walk(target);
|
|
148
|
+
const rewritten = [];
|
|
149
|
+
const findings = [];
|
|
150
|
+
|
|
151
|
+
for (const file of files) {
|
|
152
|
+
const original = fs.readFileSync(file, 'utf8');
|
|
153
|
+
const { text, changed } = rewriteImports(original);
|
|
154
|
+
if (changed) {
|
|
155
|
+
rewritten.push({ file: path.relative(ctx.cwd, file), imports: changed });
|
|
156
|
+
// Scan the rewritten text so a line number matches what the file will
|
|
157
|
+
// hold after --write, not what it held before.
|
|
158
|
+
if (flags.write) fs.writeFileSync(file, text, 'utf8');
|
|
159
|
+
}
|
|
160
|
+
findings.push(...scanUnsupported(text, path.relative(ctx.cwd, file)));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const result = {
|
|
164
|
+
root: target,
|
|
165
|
+
scanned: files.length,
|
|
166
|
+
rewritten,
|
|
167
|
+
written: Boolean(flags.write),
|
|
168
|
+
findings,
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
output(ctx, result, (data, ui) => {
|
|
172
|
+
const verb = data.written ? 'Rewrote' : 'Would rewrite';
|
|
173
|
+
ui.info(`Scanned ${data.scanned} files under ${data.root}`);
|
|
174
|
+
if (data.rewritten.length) {
|
|
175
|
+
ui.info(`${verb} imports in ${data.rewritten.length} file(s):`);
|
|
176
|
+
for (const entry of data.rewritten) ui.info(` ${entry.file} (${entry.imports})`);
|
|
177
|
+
} else {
|
|
178
|
+
ui.info('No Supabase imports found.');
|
|
179
|
+
}
|
|
180
|
+
if (!data.written && data.rewritten.length) ui.info('Re-run with --write to apply.');
|
|
181
|
+
|
|
182
|
+
if (!data.findings.length) {
|
|
183
|
+
ui.success('No unsupported calls found.');
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
ui.info('');
|
|
187
|
+
ui.info(`${data.findings.length} call(s) this SDK does not support:`);
|
|
188
|
+
for (const finding of data.findings) {
|
|
189
|
+
ui.info(` ${finding.file}:${finding.line} ${finding.what}`);
|
|
190
|
+
ui.info(` ${finding.advice}`);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
module.exports = {
|
|
197
|
+
summary: 'Port an application from Supabase',
|
|
198
|
+
usage: 'motherbase migrate codemod <dir> [--write]',
|
|
199
|
+
subcommands: { codemod },
|
|
200
|
+
// Exported for the unit test: the scanner is the part worth pinning.
|
|
201
|
+
_internals: { rewriteImports, scanUnsupported },
|
|
202
|
+
};
|