@glash/cli 0.2.7 → 0.2.12
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/bin/glash.mjs +1 -2
- package/commands/database.mjs +58 -1
- package/commands/deploy.mjs +31 -3
- package/commands/login.mjs +3 -0
- package/commands/logout.mjs +14 -2
- package/lib/api.mjs +22 -3
- package/lib/version.mjs +1 -0
- package/lib/zip.mjs +74 -13
- package/package.json +1 -1
package/bin/glash.mjs
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
import { argv, exit } from 'node:process';
|
|
3
3
|
import { parseArgs } from '../lib/args.mjs';
|
|
4
4
|
import { info, fail, color } from '../lib/ui.mjs';
|
|
5
|
-
|
|
6
|
-
const VERSION = '0.2.7';
|
|
5
|
+
import { VERSION } from '../lib/version.mjs';
|
|
7
6
|
|
|
8
7
|
const sub = argv[2];
|
|
9
8
|
const rest = argv.slice(3);
|
package/commands/database.mjs
CHANGED
|
@@ -25,7 +25,64 @@ export async function database(parsed) {
|
|
|
25
25
|
const next = { ...parsed, positional: parsed.positional.slice(1) };
|
|
26
26
|
if (action === 'dump') return databaseDump(next);
|
|
27
27
|
if (action === 'upload') return databaseUpload(next);
|
|
28
|
-
|
|
28
|
+
if (action === 'migrate') return databaseMigrate(next);
|
|
29
|
+
return fail('Usage: glash database <dump|upload|migrate> [flags]');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* End-to-end Supabase → glashDB migration.
|
|
34
|
+
*
|
|
35
|
+
* glash database migrate \
|
|
36
|
+
* --from "$SUPABASE_DB_URL" # source connection (defaults to discovered DATABASE_URL/SUPABASE_DB_URL)
|
|
37
|
+
* --project my-app # destination glashDB project (defaults to linked project)
|
|
38
|
+
* --include-auth-users # also copy auth.users → glashauth (bcrypt hashes preserved)
|
|
39
|
+
* --yes # skip confirmation prompts
|
|
40
|
+
*
|
|
41
|
+
* Steps: dump (custom format) → confirm → restore --clean --if-exists → optional auth.users copy.
|
|
42
|
+
*/
|
|
43
|
+
export async function databaseMigrate({ flags, positional }) {
|
|
44
|
+
const sourceUrl = flags.from || flags.url || positional[0] || await discoverSourceUrl();
|
|
45
|
+
if (!sourceUrl) return fail('No source URL. Pass --from "$SUPABASE_DB_URL" or set DATABASE_URL/SUPABASE_DB_URL.');
|
|
46
|
+
|
|
47
|
+
const project = await resolveProject(flags.project);
|
|
48
|
+
const yes = Boolean(flags.yes || flags.y);
|
|
49
|
+
if (!yes) {
|
|
50
|
+
const answer = await prompt(`Migrate into glashDB project "${project.slug || project.id}"? Type "migrate" to continue: `);
|
|
51
|
+
if (answer !== 'migrate') return fail('Aborted');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 1. dump
|
|
55
|
+
await databaseDump({ flags: { ...flags, url: sourceUrl, format: 'custom' }, positional: [] });
|
|
56
|
+
// 2. upload (the latest dump is already pointed at by .glash/database-dumps/latest.json)
|
|
57
|
+
await databaseUpload({ flags: { ...flags, project: project.slug || project.id, yes: true }, positional: [] });
|
|
58
|
+
// 3. optional auth users
|
|
59
|
+
if (flags['include-auth-users'] || flags.includeAuthUsers) {
|
|
60
|
+
await copyAuthUsers(sourceUrl, project.id);
|
|
61
|
+
}
|
|
62
|
+
ok(`Migration complete → ${project.slug || project.id}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function copyAuthUsers(sourceUrl, projectId) {
|
|
66
|
+
info(color.dim('→ copying auth.users → glashauth (bcrypt hashes preserved)'));
|
|
67
|
+
const source = parsePostgresUrl(sourceUrl, 'source');
|
|
68
|
+
// Stream rows out via psql --csv, then POST to the backend importer.
|
|
69
|
+
const psql = 'psql';
|
|
70
|
+
const sql = `COPY (SELECT id, email, encrypted_password, raw_user_meta_data->>'full_name' AS full_name, created_at FROM auth.users WHERE email IS NOT NULL) TO STDOUT WITH CSV HEADER`;
|
|
71
|
+
const child = spawn(psql, ['-h', source.host, '-p', String(source.port), '-U', source.user, '-d', source.database, '-c', sql], {
|
|
72
|
+
env: processEnvForDb(source),
|
|
73
|
+
});
|
|
74
|
+
const chunks = [];
|
|
75
|
+
child.stdout.on('data', (d) => chunks.push(d));
|
|
76
|
+
const errs = [];
|
|
77
|
+
child.stderr.on('data', (d) => errs.push(d));
|
|
78
|
+
await new Promise((res, rej) => {
|
|
79
|
+
child.on('error', rej);
|
|
80
|
+
child.on('close', (code) => code === 0 ? res() : rej(new Error(`psql exit ${code}: ${Buffer.concat(errs).toString().slice(0, 400)}`)));
|
|
81
|
+
});
|
|
82
|
+
const csv = Buffer.concat(chunks).toString('utf8');
|
|
83
|
+
const { apiPost } = await import('../lib/api.mjs');
|
|
84
|
+
const result = await apiPost(`/database/migrate/${projectId}/import-auth-users`, { csv });
|
|
85
|
+
ok(`Imported ${result.imported ?? 0} auth users (${result.skipped ?? 0} skipped).`);
|
|
29
86
|
}
|
|
30
87
|
|
|
31
88
|
export async function databaseDump({ flags, positional }) {
|
package/commands/deploy.mjs
CHANGED
|
@@ -177,10 +177,27 @@ export async function deploy({ flags }) {
|
|
|
177
177
|
const seen = new Set();
|
|
178
178
|
let lastStatus = '';
|
|
179
179
|
const start = Date.now();
|
|
180
|
+
let pollDelayMs = 5000;
|
|
181
|
+
let lastProgressAt = Date.now();
|
|
180
182
|
while (Date.now() - start < 15 * 60 * 1000) {
|
|
181
183
|
let pipe;
|
|
182
|
-
try {
|
|
183
|
-
|
|
184
|
+
try {
|
|
185
|
+
pipe = await apiGet(`/deployments/${dep.id}/pipeline`);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
if (e.status === 429) {
|
|
188
|
+
const wait = Math.min(Math.max(e.retryAfterMs ?? pollDelayMs * 2, 10000), 60000);
|
|
189
|
+
pollDelayMs = wait;
|
|
190
|
+
await sleep(wait);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (Date.now() - lastProgressAt > 60_000) {
|
|
194
|
+
info(color.dim(` • deploy Still working; waiting for the next deployment update…`));
|
|
195
|
+
lastProgressAt = Date.now();
|
|
196
|
+
}
|
|
197
|
+
pollDelayMs = Math.min(Math.max(pollDelayMs * 1.5, 5000), 30000);
|
|
198
|
+
await sleep(pollDelayMs);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
184
201
|
|
|
185
202
|
for (const ev of pipe.events ?? []) {
|
|
186
203
|
const key = `${ev.id ?? ev.createdAt}:${ev.step}:${ev.status}`;
|
|
@@ -188,6 +205,7 @@ export async function deploy({ flags }) {
|
|
|
188
205
|
seen.add(key);
|
|
189
206
|
const tag = stepBadge(ev.status);
|
|
190
207
|
info(` ${tag} ${color.dim(ev.step.padEnd(8))} ${ev.message ?? ''}`);
|
|
208
|
+
lastProgressAt = Date.now();
|
|
191
209
|
}
|
|
192
210
|
|
|
193
211
|
const status = pipe.deployStatus ?? pipe.buildStatus ?? '';
|
|
@@ -200,7 +218,8 @@ export async function deploy({ flags }) {
|
|
|
200
218
|
if (['error', 'failed'].includes(status)) {
|
|
201
219
|
return fail(`Deployment failed (${status}). Run \`glash logs ${dep.id}\` for details.`);
|
|
202
220
|
}
|
|
203
|
-
|
|
221
|
+
pollDelayMs = nextPollDelay(pipe);
|
|
222
|
+
await sleep(pollDelayMs);
|
|
204
223
|
}
|
|
205
224
|
warn('Timed out waiting for deployment. Check `glash status <id>`.');
|
|
206
225
|
}
|
|
@@ -272,4 +291,13 @@ function stepBadge(status) {
|
|
|
272
291
|
return color.cyan('•');
|
|
273
292
|
}
|
|
274
293
|
|
|
294
|
+
function nextPollDelay(pipe) {
|
|
295
|
+
const status = pipe.deployStatus ?? pipe.buildStatus ?? '';
|
|
296
|
+
const last = [...(pipe.events ?? [])].reverse().find(Boolean);
|
|
297
|
+
if (status === 'queued' || last?.status === 'queued') return 8000;
|
|
298
|
+
if (status === 'building' || last?.step === 'build') return 10000;
|
|
299
|
+
if (status === 'rolling-out') return 5000;
|
|
300
|
+
return 5000;
|
|
301
|
+
}
|
|
302
|
+
|
|
275
303
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
package/commands/login.mjs
CHANGED
|
@@ -9,6 +9,9 @@ export async function login({ flags }) {
|
|
|
9
9
|
|
|
10
10
|
try {
|
|
11
11
|
const r = await apiPost('/auth/login', { email, password });
|
|
12
|
+
if (!r?.accessToken || !r?.user?.email) {
|
|
13
|
+
throw new Error('Login response was missing a session token. Please try again.');
|
|
14
|
+
}
|
|
12
15
|
await writeCreds({ token: r.accessToken, email: r.user?.email });
|
|
13
16
|
ok(`Signed in as ${r.user?.email ?? email}`);
|
|
14
17
|
} catch (e) {
|
package/commands/logout.mjs
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { apiPost } from '../lib/api.mjs';
|
|
2
|
+
import { clearCreds, getToken } from '../lib/config.mjs';
|
|
3
|
+
import { ok, info, color } from '../lib/ui.mjs';
|
|
3
4
|
|
|
4
5
|
export async function logout() {
|
|
6
|
+
// Best-effort server-side revocation. If the token is already invalid or the
|
|
7
|
+
// server is unreachable, we still clear the local file so the user is signed
|
|
8
|
+
// out from this machine.
|
|
9
|
+
const token = await getToken();
|
|
10
|
+
if (token) {
|
|
11
|
+
try {
|
|
12
|
+
await apiPost('/auth/logout', {}, { token });
|
|
13
|
+
} catch (e) {
|
|
14
|
+
info(color.dim(`(server-side revoke failed: ${e?.message || e} — clearing local creds anyway)`));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
5
17
|
await clearCreds();
|
|
6
18
|
ok('Signed out');
|
|
7
19
|
}
|
package/lib/api.mjs
CHANGED
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import { API_URL, getToken } from './config.mjs';
|
|
2
|
+
import { VERSION } from './version.mjs';
|
|
2
3
|
|
|
3
4
|
export class GlashApiError extends Error {
|
|
4
|
-
constructor(status, message) {
|
|
5
|
+
constructor(status, message, meta = {}) {
|
|
5
6
|
super(message);
|
|
6
7
|
this.status = status;
|
|
8
|
+
this.retryAfterMs = meta.retryAfterMs;
|
|
7
9
|
}
|
|
8
10
|
}
|
|
9
11
|
|
|
12
|
+
function retryAfterMs(headers) {
|
|
13
|
+
const raw = headers?.get?.('retry-after');
|
|
14
|
+
if (!raw) return undefined;
|
|
15
|
+
const seconds = Number(raw);
|
|
16
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000);
|
|
17
|
+
const date = Date.parse(raw);
|
|
18
|
+
if (Number.isFinite(date)) return Math.max(0, date - Date.now());
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
export async function api(method, path, { body, token, query } = {}) {
|
|
11
|
-
const headers = {
|
|
23
|
+
const headers = {
|
|
24
|
+
Accept: 'application/json',
|
|
25
|
+
'User-Agent': `glash-cli/${VERSION}`,
|
|
26
|
+
'X-Glash-Client': 'cli',
|
|
27
|
+
};
|
|
12
28
|
const t = token ?? (await getToken());
|
|
13
29
|
if (t) headers.Authorization = `Bearer ${t}`;
|
|
14
30
|
if (body !== undefined && !(body instanceof FormData)) {
|
|
@@ -42,7 +58,10 @@ export async function api(method, path, { body, token, query } = {}) {
|
|
|
42
58
|
if (typeof data?.message === 'string') message = data.message;
|
|
43
59
|
else if (Array.isArray(data?.message)) message = data.message.join(', ');
|
|
44
60
|
} catch {}
|
|
45
|
-
|
|
61
|
+
if (res.status === 401 && !path.includes('/auth/login') && !path.includes('/auth/cli/login')) {
|
|
62
|
+
message = `${message}. Run \`glash login\` again, then retry the command.`;
|
|
63
|
+
}
|
|
64
|
+
throw new GlashApiError(res.status, message, { retryAfterMs: retryAfterMs(res.headers) });
|
|
46
65
|
}
|
|
47
66
|
|
|
48
67
|
if (res.status === 204) return undefined;
|
package/lib/version.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = '0.2.12';
|
package/lib/zip.mjs
CHANGED
|
@@ -6,7 +6,9 @@ import fs from 'fs-extra';
|
|
|
6
6
|
export async function zipDirectory(sourceDir, outPath, { ignore = [] } = {}) {
|
|
7
7
|
return new Promise((resolve, reject) => {
|
|
8
8
|
const output = createWriteStream(outPath);
|
|
9
|
-
const
|
|
9
|
+
const rawLevel = Number(process.env.GLASH_ZIP_LEVEL ?? 1);
|
|
10
|
+
const level = Number.isInteger(rawLevel) && rawLevel >= 0 && rawLevel <= 9 ? rawLevel : 1;
|
|
11
|
+
const archive = archiver('zip', { zlib: { level } });
|
|
10
12
|
|
|
11
13
|
output.on('close', () => resolve(archive.pointer()));
|
|
12
14
|
archive.on('error', (err) => reject(err));
|
|
@@ -16,34 +18,61 @@ export async function zipDirectory(sourceDir, outPath, { ignore = [] } = {}) {
|
|
|
16
18
|
// Default ignores
|
|
17
19
|
const defaultIgnore = [
|
|
18
20
|
'node_modules/**',
|
|
21
|
+
'**/node_modules/**',
|
|
19
22
|
'.git/**',
|
|
23
|
+
'**/.git/**',
|
|
20
24
|
'.next/**',
|
|
25
|
+
'**/.next/**',
|
|
21
26
|
'.next*/**',
|
|
27
|
+
'**/.next*/**',
|
|
22
28
|
'.nuxt/**',
|
|
29
|
+
'**/.nuxt/**',
|
|
23
30
|
'.nuxt*/**',
|
|
31
|
+
'**/.nuxt*/**',
|
|
24
32
|
'.output/**',
|
|
33
|
+
'**/.output/**',
|
|
25
34
|
'.svelte-kit/**',
|
|
35
|
+
'**/.svelte-kit/**',
|
|
26
36
|
'.wrangler/**',
|
|
37
|
+
'**/.wrangler/**',
|
|
27
38
|
'.netlify/**',
|
|
39
|
+
'**/.netlify/**',
|
|
28
40
|
'dist/**',
|
|
41
|
+
'**/dist/**',
|
|
29
42
|
'build/**',
|
|
43
|
+
'**/build/**',
|
|
30
44
|
'out/**',
|
|
45
|
+
'**/out/**',
|
|
31
46
|
'coverage/**',
|
|
47
|
+
'**/coverage/**',
|
|
32
48
|
'.turbo/**',
|
|
49
|
+
'**/.turbo/**',
|
|
33
50
|
'.cache/**',
|
|
51
|
+
'**/.cache/**',
|
|
34
52
|
'.parcel-cache/**',
|
|
53
|
+
'**/.parcel-cache/**',
|
|
35
54
|
'.vite/**',
|
|
55
|
+
'**/.vite/**',
|
|
36
56
|
'.glash/**',
|
|
57
|
+
'**/.glash/**',
|
|
37
58
|
'*.log',
|
|
59
|
+
'**/*.log',
|
|
38
60
|
'npm-debug.log*',
|
|
61
|
+
'**/npm-debug.log*',
|
|
39
62
|
'yarn-debug.log*',
|
|
63
|
+
'**/yarn-debug.log*',
|
|
40
64
|
'yarn-error.log*',
|
|
65
|
+
'**/yarn-error.log*',
|
|
41
66
|
'.env',
|
|
67
|
+
'**/.env',
|
|
42
68
|
'.env.local',
|
|
69
|
+
'**/.env.local',
|
|
43
70
|
'.env.production',
|
|
71
|
+
'**/.env.production',
|
|
44
72
|
'.env.development',
|
|
73
|
+
'**/.env.development',
|
|
45
74
|
];
|
|
46
|
-
const allIgnore = [...new Set([...defaultIgnore, ...
|
|
75
|
+
const allIgnore = [...new Set([...defaultIgnore, ...loadIgnorePatterns(sourceDir), ...ignore])];
|
|
47
76
|
|
|
48
77
|
archive.glob('**/*', {
|
|
49
78
|
cwd: sourceDir,
|
|
@@ -55,19 +84,51 @@ export async function zipDirectory(sourceDir, outPath, { ignore = [] } = {}) {
|
|
|
55
84
|
});
|
|
56
85
|
}
|
|
57
86
|
|
|
58
|
-
function
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
87
|
+
function parseIgnorePattern(line) {
|
|
88
|
+
const raw = line.trim();
|
|
89
|
+
if (!raw || raw.startsWith('#') || raw.startsWith('!')) return [];
|
|
90
|
+
|
|
91
|
+
const hasLeadingSlash = raw.startsWith('/');
|
|
92
|
+
|
|
93
|
+
// Normalize by stripping leading and trailing slashes
|
|
94
|
+
let normalized = raw.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
95
|
+
if (!normalized) return [];
|
|
96
|
+
|
|
97
|
+
const hasMiddleSlash = normalized.includes('/');
|
|
98
|
+
const isRootRelative = hasLeadingSlash || hasMiddleSlash;
|
|
61
99
|
|
|
62
100
|
const patterns = [];
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (!normalized) continue;
|
|
69
|
-
patterns.push(normalized);
|
|
70
|
-
if (!/[*!?[\]{}()]/.test(normalized)) patterns.push(`${normalized}/**`);
|
|
101
|
+
|
|
102
|
+
// Add base patterns
|
|
103
|
+
patterns.push(normalized);
|
|
104
|
+
if (!normalized.endsWith('/**') && !normalized.endsWith('/*')) {
|
|
105
|
+
patterns.push(`${normalized}/**`);
|
|
71
106
|
}
|
|
107
|
+
|
|
108
|
+
// If it's not root-relative, match at any level
|
|
109
|
+
if (!isRootRelative) {
|
|
110
|
+
patterns.push(`**/${normalized}`);
|
|
111
|
+
if (!normalized.endsWith('/**') && !normalized.endsWith('/*')) {
|
|
112
|
+
patterns.push(`**/${normalized}/**`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
72
116
|
return patterns;
|
|
73
117
|
}
|
|
118
|
+
|
|
119
|
+
function loadIgnorePatterns(sourceDir) {
|
|
120
|
+
const files = ['.gitignore', '.glashignore'];
|
|
121
|
+
const allPatterns = [];
|
|
122
|
+
|
|
123
|
+
for (const filename of files) {
|
|
124
|
+
const path = join(sourceDir, filename);
|
|
125
|
+
if (!fs.existsSync(path)) continue;
|
|
126
|
+
|
|
127
|
+
const text = fs.readFileSync(path, 'utf8');
|
|
128
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
129
|
+
const parsed = parseIgnorePattern(raw);
|
|
130
|
+
allPatterns.push(...parsed);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return allPatterns;
|
|
134
|
+
}
|
package/package.json
CHANGED