-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpull.ts
More file actions
180 lines (160 loc) · 5.03 KB
/
pull.ts
File metadata and controls
180 lines (160 loc) · 5.03 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
import {
RealmSyncBase,
validateMatrixEnvVars,
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';
interface PullOptions extends SyncOptions {
deleteLocal?: boolean;
}
class RealmPuller extends RealmSyncBase {
hasError = false;
constructor(
private pullOptions: PullOptions,
matrixUrl: string,
username: string,
password: string,
) {
super(pullOptions, matrixUrl, username, password);
}
async sync(): Promise<void> {
console.log(
`Starting pull from ${this.options.workspaceUrl} to ${this.options.localDir}`,
);
console.log('Testing workspace access...');
try {
await this.getRemoteFileList('');
} catch (error) {
console.error('Failed to access workspace:', error);
throw new Error(
'Cannot proceed with pull: Authentication or access failed. ' +
'Please check your Matrix credentials and workspace permissions.',
);
}
console.log('Workspace access verified');
const remoteFiles = await this.getRemoteFileList();
console.log(`Found ${remoteFiles.size} files in remote workspace`);
const localFiles = await this.getLocalFileList();
console.log(`Found ${localFiles.size} files in local directory`);
if (!fs.existsSync(this.options.localDir)) {
if (this.options.dryRun) {
console.log(
`[DRY RUN] Would create directory: ${this.options.localDir}`,
);
} else {
fs.mkdirSync(this.options.localDir, { recursive: true });
console.log(`Created directory: ${this.options.localDir}`);
}
}
const downloadedFiles: string[] = [];
for (const [relativePath] of remoteFiles) {
try {
const localPath = path.join(this.options.localDir, relativePath);
await this.downloadFile(relativePath, localPath);
downloadedFiles.push(relativePath);
} catch (error) {
this.hasError = true;
console.error(`Error downloading ${relativePath}:`, error);
}
}
if (this.pullOptions.deleteLocal) {
const filesToDelete = new Set(localFiles.keys());
for (const relativePath of remoteFiles.keys()) {
filesToDelete.delete(relativePath);
}
if (filesToDelete.size > 0) {
const checkpointManager = new CheckpointManager(this.options.localDir);
const deleteChanges: CheckpointChange[] = Array.from(filesToDelete).map(
(f) => ({
file: f,
status: 'deleted' as const,
}),
);
const preDeleteCheckpoint = checkpointManager.createCheckpoint(
'remote',
deleteChanges,
`Pre-delete checkpoint: ${filesToDelete.size} files not on server`,
);
if (preDeleteCheckpoint) {
console.log(
`\nCheckpoint created before deletion: ${preDeleteCheckpoint.shortHash}`,
);
}
console.log(
`\nDeleting ${filesToDelete.size} local files that don't exist in workspace...`,
);
for (const relativePath of filesToDelete) {
try {
const localPath = localFiles.get(relativePath);
if (localPath) {
await this.deleteLocalFile(localPath);
console.log(` Deleted: ${relativePath}`);
}
} catch (error) {
this.hasError = true;
console.error(`Error deleting local file ${relativePath}:`, error);
}
}
}
}
if (!this.options.dryRun && downloadedFiles.length > 0) {
const checkpointManager = new CheckpointManager(this.options.localDir);
const pullChanges: CheckpointChange[] = downloadedFiles.map((f) => ({
file: f,
status: 'modified' as const,
}));
const checkpoint = checkpointManager.createCheckpoint(
'remote',
pullChanges,
);
if (checkpoint) {
const tag = checkpoint.isMajor ? '[MAJOR]' : '[minor]';
console.log(
`\nCheckpoint created: ${checkpoint.shortHash} ${tag} ${checkpoint.message}`,
);
}
}
console.log('Pull completed');
}
}
export interface PullCommandOptions {
delete?: boolean;
dryRun?: boolean;
}
export async function pullCommand(
workspaceUrl: string,
localDir: string,
options: PullCommandOptions,
): Promise<void> {
const { matrixUrl, username, password } =
await validateMatrixEnvVars(workspaceUrl);
try {
const puller = new RealmPuller(
{
workspaceUrl,
localDir,
deleteLocal: options.delete,
dryRun: options.dryRun,
},
matrixUrl,
username,
password,
);
await puller.initialize();
await puller.sync();
if (puller.hasError) {
console.log('Pull did not complete successfully. View logs for details');
process.exit(2);
} else {
console.log('Pull completed successfully');
}
} catch (error) {
console.error('Pull failed:', error);
process.exit(1);
}
}