-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpush.ts
More file actions
248 lines (216 loc) · 6.68 KB
/
push.ts
File metadata and controls
248 lines (216 loc) · 6.68 KB
1
2
3
4
5
6
7
8
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import {
RealmSyncBase,
validateMatrixEnvVars,
isProtectedFile,
type SyncOptions,
} from '../lib/realm-sync-base.js';
import {
CheckpointManager,
type CheckpointChange,
} from '../lib/checkpoint-manager.js';
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
interface SyncManifest {
workspaceUrl: string;
files: Record<string, string>; // relativePath -> contentHash
}
function computeFileHash(filePath: string): string {
const content = fs.readFileSync(filePath);
return crypto.createHash('md5').update(content).digest('hex');
}
function loadManifest(localDir: string): SyncManifest | null {
const manifestPath = path.join(localDir, '.boxel-sync.json');
if (fs.existsSync(manifestPath)) {
try {
return JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
} catch {
return null;
}
}
return null;
}
function saveManifest(localDir: string, manifest: SyncManifest): void {
const manifestPath = path.join(localDir, '.boxel-sync.json');
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
}
interface PushOptions extends SyncOptions {
deleteRemote?: boolean;
force?: boolean;
}
class RealmPusher extends RealmSyncBase {
hasError = false;
constructor(
private pushOptions: PushOptions,
matrixUrl: string,
username: string,
password: string,
) {
super(pushOptions, matrixUrl, username, password);
}
async sync(): Promise<void> {
console.log(
`Starting push from ${this.options.localDir} to ${this.options.workspaceUrl}`,
);
console.log('Testing workspace access...');
try {
await this.getRemoteFileList('');
} catch (error) {
console.error('Failed to access workspace:', error);
throw new Error(
'Cannot proceed with push: Authentication or access failed. ' +
'Please check your Matrix credentials and workspace permissions.',
);
}
console.log('Workspace access verified');
const localFiles = await this.getLocalFileList();
console.log(`Found ${localFiles.size} files in local directory`);
const manifest = loadManifest(this.options.localDir);
const newManifest: SyncManifest = {
workspaceUrl: this.options.workspaceUrl,
files: {},
};
const filesToUpload: Map<string, string> = new Map();
if (
this.pushOptions.force ||
!manifest ||
manifest.workspaceUrl !== this.options.workspaceUrl
) {
if (this.pushOptions.force) {
console.log('Force mode: uploading all files');
} else if (!manifest) {
console.log('No sync manifest found, will upload all files');
} else {
console.log('Workspace URL changed, will upload all files');
}
for (const [relativePath, localPath] of localFiles) {
if (isProtectedFile(relativePath)) continue;
filesToUpload.set(relativePath, localPath);
}
} else {
console.log('Checking for changed files...');
let skipped = 0;
for (const [relativePath, localPath] of localFiles) {
if (isProtectedFile(relativePath)) {
skipped++;
continue;
}
const currentHash = computeFileHash(localPath);
const previousHash = manifest.files[relativePath];
if (previousHash !== currentHash) {
filesToUpload.set(relativePath, localPath);
} else {
skipped++;
newManifest.files[relativePath] = currentHash;
}
}
if (skipped > 0) {
console.log(`Skipping ${skipped} unchanged files`);
}
}
if (filesToUpload.size === 0) {
console.log('No files to upload - everything is up to date');
} else {
console.log(`Uploading ${filesToUpload.size} file(s)...`);
for (const [relativePath, localPath] of filesToUpload) {
try {
await this.uploadFile(relativePath, localPath);
newManifest.files[relativePath] = computeFileHash(localPath);
} catch (error) {
this.hasError = true;
console.error(`Error uploading ${relativePath}:`, error);
}
}
}
if (this.pushOptions.deleteRemote) {
const remoteFiles = await this.getRemoteFileList();
const filesToDelete = new Set(remoteFiles.keys());
for (const relativePath of filesToDelete) {
if (isProtectedFile(relativePath)) {
filesToDelete.delete(relativePath);
}
}
for (const relativePath of localFiles.keys()) {
filesToDelete.delete(relativePath);
}
if (filesToDelete.size > 0) {
console.log(
`Deleting ${filesToDelete.size} remote files that don't exist locally`,
);
for (const relativePath of filesToDelete) {
try {
await this.deleteFile(relativePath);
} catch (error) {
this.hasError = true;
console.error(`Error deleting ${relativePath}:`, error);
}
}
}
}
if (!this.options.dryRun) {
saveManifest(this.options.localDir, newManifest);
}
if (!this.options.dryRun && filesToUpload.size > 0) {
const checkpointManager = new CheckpointManager(this.options.localDir);
const pushChanges: CheckpointChange[] = Array.from(
filesToUpload.keys(),
).map((f) => ({
file: f,
status: 'modified' as const,
}));
const checkpoint = checkpointManager.createCheckpoint(
'local',
pushChanges,
);
if (checkpoint) {
const tag = checkpoint.isMajor ? '[MAJOR]' : '[minor]';
console.log(
`\nCheckpoint created: ${checkpoint.shortHash} ${tag} ${checkpoint.message}`,
);
}
}
console.log('Push completed');
}
}
export interface PushCommandOptions {
delete?: boolean;
dryRun?: boolean;
force?: boolean;
}
export async function pushCommand(
localDir: string,
workspaceUrl: string,
options: PushCommandOptions,
): Promise<void> {
const { matrixUrl, username, password } =
await validateMatrixEnvVars(workspaceUrl);
if (!fs.existsSync(localDir)) {
console.error(`Local directory does not exist: ${localDir}`);
process.exit(1);
}
try {
const pusher = new RealmPusher(
{
workspaceUrl,
localDir,
deleteRemote: options.delete,
dryRun: options.dryRun,
force: options.force,
},
matrixUrl,
username,
password,
);
await pusher.initialize();
await pusher.sync();
if (pusher.hasError) {
console.log('Push did not complete successfully. View logs for details');
process.exit(2);
} else {
console.log('Push completed successfully');
}
} catch (error) {
console.error('Push failed:', error);
process.exit(1);
}
}