|
1 | | -import { replacePlaceholders, joinPath } from '@calycode/utils'; |
| 1 | +import { replacePlaceholders, joinPath, dirname } from '@calycode/utils'; |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * Exports a backup and emits events for CLI/UI. |
5 | 5 | */ |
6 | | -async function exportBackupImplementation({ instance, workspace, branch, core }) { |
| 6 | +async function exportBackupImplementation({ outputDir, instance, workspace, branch, core }) { |
7 | 7 | core.emit('start', { |
8 | 8 | name: 'export-backup', |
9 | 9 | payload: { instance, workspace, branch }, |
@@ -35,43 +35,70 @@ async function exportBackupImplementation({ instance, workspace, branch, core }) |
35 | 35 | percent: 15, |
36 | 36 | }); |
37 | 37 |
|
38 | | - // Resolve output dir |
39 | | - const outputDir = replacePlaceholders(instanceConfig.backups.output, { |
40 | | - instance: instanceConfig.name, |
41 | | - workspace: workspaceConfig.name, |
42 | | - branch: branchConfig.label, |
| 38 | + core.emit('progress', { |
| 39 | + name: 'export-backup', |
| 40 | + message: 'Requesting backup from Xano API...', |
| 41 | + percent: 40, |
43 | 42 | }); |
44 | 43 |
|
45 | | - await core.storage.mkdir(outputDir, { recursive: true }); |
46 | | - |
| 44 | + const startTime = Date.now(); |
47 | 45 | core.emit('progress', { |
48 | 46 | name: 'export-backup', |
49 | 47 | message: 'Requesting backup from Xano API...', |
50 | 48 | percent: 40, |
51 | 49 | }); |
52 | 50 |
|
53 | | - const backupStreamRequest = await fetch( |
54 | | - `${instanceConfig.url}/api:meta/workspace/${workspaceConfig.id}/export`, |
55 | | - { |
56 | | - method: 'POST', |
57 | | - headers: { |
58 | | - Authorization: `Bearer ${await core.loadToken(instanceConfig.name)}`, |
59 | | - 'Content-Type': 'application/json', |
60 | | - }, |
61 | | - body: JSON.stringify({ branch: branchConfig.label }), |
62 | | - } |
63 | | - ); |
| 51 | + let backupStreamRequest; |
| 52 | + try { |
| 53 | + backupStreamRequest = await fetch( |
| 54 | + `${instanceConfig.url}/api:meta/workspace/${workspaceConfig.id}/export`, |
| 55 | + { |
| 56 | + method: 'POST', |
| 57 | + headers: { |
| 58 | + Authorization: `Bearer ${await core.loadToken(instanceConfig.name)}`, |
| 59 | + 'Content-Type': 'application/json', |
| 60 | + }, |
| 61 | + body: JSON.stringify({ branch: branchConfig.label }), |
| 62 | + } |
| 63 | + ); |
| 64 | + } catch (err) { |
| 65 | + core.emit('error', { |
| 66 | + error: err, |
| 67 | + message: 'Fetch failed', |
| 68 | + step: 'fetch', |
| 69 | + elapsed: Date.now() - startTime, |
| 70 | + }); |
| 71 | + throw err; |
| 72 | + } |
64 | 73 |
|
65 | | - core.emit('progress', { |
66 | | - name: 'export-backup', |
67 | | - message: 'Saving backup file...', |
68 | | - percent: 80, |
| 74 | + core.emit('info', { |
| 75 | + message: 'Response headers received', |
| 76 | + headers: backupStreamRequest.headers, |
| 77 | + status: backupStreamRequest.status, |
| 78 | + elapsed: Date.now() - startTime, |
69 | 79 | }); |
70 | 80 |
|
71 | 81 | const now = new Date(); |
72 | 82 | const ts = now.toISOString().replace(/[:.]/g, '-'); |
73 | 83 | const backupPath = joinPath(outputDir, `backup-${ts}.tar.gz`); |
74 | | - await core.storage.streamToFile(backupStreamRequest.body, backupPath); |
| 84 | + |
| 85 | + await core.storage.mkdir(outputDir, { recursive: true }); |
| 86 | + try { |
| 87 | + await core.storage.streamToFile({ path: backupPath, stream: backupStreamRequest.body }); |
| 88 | + core.emit('info', { |
| 89 | + message: 'Streaming complete', |
| 90 | + backupPath, |
| 91 | + elapsed: Date.now() - startTime, |
| 92 | + }); |
| 93 | + } catch (err) { |
| 94 | + core.emit('error', { |
| 95 | + error: err, |
| 96 | + message: 'Streaming to file failed', |
| 97 | + step: 'streamToFile', |
| 98 | + elapsed: Date.now() - startTime, |
| 99 | + }); |
| 100 | + throw err; |
| 101 | + } |
75 | 102 |
|
76 | 103 | core.emit('progress', { |
77 | 104 | name: 'export-backup', |
|
0 commit comments