farseer-cli 1.0.8 → 1.0.9
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/dist/commands/app.js +349 -0
- package/dist/commands/app.js.map +7 -0
- package/dist/commands/apps.js +111 -0
- package/dist/commands/apps.js.map +7 -0
- package/dist/commands/checkout.js +142 -0
- package/dist/commands/checkout.js.map +7 -0
- package/dist/commands/config.js +139 -0
- package/dist/commands/config.js.map +7 -0
- package/dist/commands/diff.js +185 -0
- package/dist/commands/diff.js.map +7 -0
- package/dist/commands/dimensions.js +145 -0
- package/dist/commands/dimensions.js.map +7 -0
- package/dist/commands/files.js +99 -0
- package/dist/commands/files.js.map +7 -0
- package/dist/commands/install.js +79 -0
- package/dist/commands/install.js.map +7 -0
- package/dist/commands/list.js +92 -0
- package/dist/commands/list.js.map +7 -0
- package/dist/commands/login.js +134 -0
- package/dist/commands/login.js.map +7 -0
- package/dist/commands/logout.js +59 -0
- package/dist/commands/logout.js.map +7 -0
- package/dist/commands/mcp-server.js +41 -0
- package/dist/commands/mcp-server.js.map +7 -0
- package/dist/commands/model.js +189 -0
- package/dist/commands/model.js.map +7 -0
- package/dist/commands/pull.js +258 -0
- package/dist/commands/pull.js.map +7 -0
- package/dist/commands/push.js +213 -0
- package/dist/commands/push.js.map +7 -0
- package/dist/commands/run.js +288 -0
- package/dist/commands/run.js.map +7 -0
- package/dist/commands/status.js +155 -0
- package/dist/commands/status.js.map +7 -0
- package/dist/commands/variables.js +176 -0
- package/dist/commands/variables.js.map +7 -0
- package/dist/commands/whoami.js +63 -0
- package/dist/commands/whoami.js.map +7 -0
- package/dist/index.js +165 -0
- package/dist/index.js.map +7 -0
- package/dist/mcp/index.js +35 -0
- package/dist/mcp/index.js.map +7 -0
- package/dist/mcp/prompts/workflows.js +448 -0
- package/dist/mcp/prompts/workflows.js.map +7 -0
- package/dist/mcp/resources/documentation.js +167 -0
- package/dist/mcp/resources/documentation.js.map +7 -0
- package/dist/mcp/server.js +52 -0
- package/dist/mcp/server.js.map +7 -0
- package/dist/mcp/tools/appTools.js +377 -0
- package/dist/mcp/tools/appTools.js.map +7 -0
- package/dist/mcp/tools/authTools.js +159 -0
- package/dist/mcp/tools/authTools.js.map +7 -0
- package/dist/mcp/tools/dataTools.js +362 -0
- package/dist/mcp/tools/dataTools.js.map +7 -0
- package/dist/mcp/tools/modelTools.js +325 -0
- package/dist/mcp/tools/modelTools.js.map +7 -0
- package/dist/mcp/tools/runTools.js +233 -0
- package/dist/mcp/tools/runTools.js.map +7 -0
- package/dist/mcp/tools/syncTools.js +409 -0
- package/dist/mcp/tools/syncTools.js.map +7 -0
- package/dist/mcp/utils/helpers.js +135 -0
- package/dist/mcp/utils/helpers.js.map +7 -0
- package/dist/services/appSyncService.js +399 -0
- package/dist/services/appSyncService.js.map +7 -0
- package/dist/services/configService.js +196 -0
- package/dist/services/configService.js.map +7 -0
- package/dist/services/farseerApi.js +426 -0
- package/dist/services/farseerApi.js.map +7 -0
- package/dist/services/farseerFactory.js +231 -0
- package/dist/services/farseerFactory.js.map +7 -0
- package/dist/services/farseerService.js +614 -0
- package/dist/services/farseerService.js.map +7 -0
- package/dist/services/syncService.js +689 -0
- package/dist/services/syncService.js.map +7 -0
- package/dist/utils/constants.js +46 -0
- package/dist/utils/constants.js.map +7 -0
- package/dist/utils/helpers.js +413 -0
- package/dist/utils/helpers.js.map +7 -0
- package/dist/utils/logger.js +76 -0
- package/dist/utils/logger.js.map +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var syncService_exports = {};
|
|
29
|
+
__export(syncService_exports, {
|
|
30
|
+
SyncService: () => SyncService
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(syncService_exports);
|
|
33
|
+
var fs = __toESM(require("fs"));
|
|
34
|
+
var path = __toESM(require("path"));
|
|
35
|
+
var import_farseerFactory = require("./farseerFactory");
|
|
36
|
+
var import_helpers = require("../utils/helpers");
|
|
37
|
+
var import_logger = require("../utils/logger");
|
|
38
|
+
class SyncService {
|
|
39
|
+
constructor(tenant, farseerClient) {
|
|
40
|
+
this.tenant = tenant;
|
|
41
|
+
this.farseerClient = farseerClient;
|
|
42
|
+
}
|
|
43
|
+
loadSyncState() {
|
|
44
|
+
const syncFilePath = (0, import_helpers.getSyncFilePath)(this.tenant);
|
|
45
|
+
if (!fs.existsSync(syncFilePath)) {
|
|
46
|
+
return {
|
|
47
|
+
lastSync: "",
|
|
48
|
+
files: {}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const content = fs.readFileSync(syncFilePath, "utf-8");
|
|
53
|
+
return JSON.parse(content);
|
|
54
|
+
} catch {
|
|
55
|
+
return {
|
|
56
|
+
lastSync: "",
|
|
57
|
+
files: {}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
saveSyncState(state) {
|
|
62
|
+
const syncFilePath = (0, import_helpers.getSyncFilePath)(this.tenant);
|
|
63
|
+
(0, import_helpers.ensureDirectoryExists)(path.dirname(syncFilePath));
|
|
64
|
+
fs.writeFileSync(syncFilePath, JSON.stringify(state, null, 2), "utf-8");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Fast status check that uses sync state instead of downloading remote file content.
|
|
68
|
+
* This is much faster than getStatus() but may miss changes made by others since last pull.
|
|
69
|
+
* Use this for push operations where we trust the sync state.
|
|
70
|
+
*/
|
|
71
|
+
async getStatusFast(options) {
|
|
72
|
+
const syncState = this.loadSyncState();
|
|
73
|
+
const filesDir = (0, import_helpers.getTenantFilesDir)(this.tenant);
|
|
74
|
+
let localFiles = (0, import_helpers.getAllFilesInDir)(filesDir);
|
|
75
|
+
if (!options?.all) {
|
|
76
|
+
localFiles = localFiles.filter(
|
|
77
|
+
(f) => import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => f.toLowerCase().endsWith(ext))
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
let remoteFiles = await this.farseerClient.listFiles();
|
|
81
|
+
if (!options?.all) {
|
|
82
|
+
remoteFiles = remoteFiles.filter(
|
|
83
|
+
(f) => import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => f.name.toLowerCase().endsWith(ext))
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const remoteFilePaths = new Set(remoteFiles.map((f) => f.path));
|
|
87
|
+
const status = {
|
|
88
|
+
modifiedLocally: [],
|
|
89
|
+
modifiedRemotely: [],
|
|
90
|
+
onlyLocal: [],
|
|
91
|
+
onlyRemote: [],
|
|
92
|
+
inSync: [],
|
|
93
|
+
conflicts: []
|
|
94
|
+
};
|
|
95
|
+
for (const localFile of localFiles) {
|
|
96
|
+
const localPath = path.join(filesDir, localFile);
|
|
97
|
+
const content = fs.readFileSync(localPath);
|
|
98
|
+
const localHash = (0, import_helpers.calculateHash)(content);
|
|
99
|
+
const syncInfo = syncState.files[localFile];
|
|
100
|
+
if (remoteFilePaths.has(localFile)) {
|
|
101
|
+
if (syncInfo && localHash === syncInfo.localHash) {
|
|
102
|
+
status.inSync.push(localFile);
|
|
103
|
+
} else {
|
|
104
|
+
status.modifiedLocally.push(localFile);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
if (syncInfo && syncInfo.remoteHash) {
|
|
108
|
+
if (localHash !== syncInfo.localHash) {
|
|
109
|
+
status.conflicts.push({
|
|
110
|
+
filePath: localFile,
|
|
111
|
+
type: "local_modified_remote_deleted",
|
|
112
|
+
description: "File modified locally but deleted on remote"
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
status.modifiedRemotely.push(localFile);
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
status.onlyLocal.push(localFile);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
for (const remoteFile of remoteFiles) {
|
|
123
|
+
if (!localFiles.includes(remoteFile.path)) {
|
|
124
|
+
const syncInfo = syncState.files[remoteFile.path];
|
|
125
|
+
if (syncInfo && syncInfo.localHash) {
|
|
126
|
+
status.modifiedLocally.push(remoteFile.path);
|
|
127
|
+
} else {
|
|
128
|
+
status.onlyRemote.push(remoteFile.path);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return status;
|
|
133
|
+
}
|
|
134
|
+
async getStatus(options) {
|
|
135
|
+
const syncState = this.loadSyncState();
|
|
136
|
+
const filesDir = (0, import_helpers.getTenantFilesDir)(this.tenant);
|
|
137
|
+
let localFiles = (0, import_helpers.getAllFilesInDir)(filesDir);
|
|
138
|
+
if (!options?.all) {
|
|
139
|
+
localFiles = localFiles.filter(
|
|
140
|
+
(f) => import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => f.toLowerCase().endsWith(ext))
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const localHashes = /* @__PURE__ */ new Map();
|
|
144
|
+
for (const file of localFiles) {
|
|
145
|
+
const fullPath = path.join(filesDir, file);
|
|
146
|
+
const content = fs.readFileSync(fullPath);
|
|
147
|
+
localHashes.set(file, (0, import_helpers.calculateHash)(content));
|
|
148
|
+
}
|
|
149
|
+
let remoteFiles = await this.farseerClient.listFiles();
|
|
150
|
+
if (!options?.all) {
|
|
151
|
+
remoteFiles = remoteFiles.filter(
|
|
152
|
+
(f) => import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => f.name.toLowerCase().endsWith(ext))
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
const remoteHashes = /* @__PURE__ */ new Map();
|
|
156
|
+
const fileContents = await (0, import_helpers.fetchWithConcurrency)(
|
|
157
|
+
remoteFiles,
|
|
158
|
+
async (file) => {
|
|
159
|
+
const content = await this.farseerClient.getFileContentAsBuffer(file.reference);
|
|
160
|
+
return { path: file.path, hash: (0, import_helpers.calculateHash)(content) };
|
|
161
|
+
},
|
|
162
|
+
15
|
|
163
|
+
);
|
|
164
|
+
for (const { path: filePath, hash } of fileContents) {
|
|
165
|
+
remoteHashes.set(filePath, hash);
|
|
166
|
+
}
|
|
167
|
+
const comparisons = this.compareFiles({ localHashes, remoteHashes, syncState, all: options?.all });
|
|
168
|
+
const status = {
|
|
169
|
+
modifiedLocally: [],
|
|
170
|
+
modifiedRemotely: [],
|
|
171
|
+
onlyLocal: [],
|
|
172
|
+
onlyRemote: [],
|
|
173
|
+
inSync: [],
|
|
174
|
+
conflicts: []
|
|
175
|
+
};
|
|
176
|
+
for (const c of comparisons) {
|
|
177
|
+
switch (c.changeType) {
|
|
178
|
+
case "unchanged":
|
|
179
|
+
status.inSync.push(c.filePath);
|
|
180
|
+
break;
|
|
181
|
+
case "local_modified":
|
|
182
|
+
status.modifiedLocally.push(c.filePath);
|
|
183
|
+
break;
|
|
184
|
+
case "remote_modified":
|
|
185
|
+
status.modifiedRemotely.push(c.filePath);
|
|
186
|
+
break;
|
|
187
|
+
case "local_new":
|
|
188
|
+
status.onlyLocal.push(c.filePath);
|
|
189
|
+
break;
|
|
190
|
+
case "remote_new":
|
|
191
|
+
status.onlyRemote.push(c.filePath);
|
|
192
|
+
break;
|
|
193
|
+
case "remote_deleted":
|
|
194
|
+
status.modifiedRemotely.push(c.filePath);
|
|
195
|
+
break;
|
|
196
|
+
case "local_deleted":
|
|
197
|
+
status.modifiedLocally.push(c.filePath);
|
|
198
|
+
break;
|
|
199
|
+
default:
|
|
200
|
+
status.conflicts.push(
|
|
201
|
+
SyncService.buildConflicts([c])[0]
|
|
202
|
+
);
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return status;
|
|
207
|
+
}
|
|
208
|
+
async pull(options) {
|
|
209
|
+
const filesDir = (0, import_helpers.getTenantFilesDir)(this.tenant);
|
|
210
|
+
(0, import_helpers.ensureDirectoryExists)(filesDir);
|
|
211
|
+
const syncState = this.loadSyncState();
|
|
212
|
+
let remoteFiles = await this.farseerClient.listFiles();
|
|
213
|
+
if (!options?.all) {
|
|
214
|
+
remoteFiles = remoteFiles.filter(
|
|
215
|
+
(f) => import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => f.name.toLowerCase().endsWith(ext))
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
remoteFiles = await this.fetchMetadataForFiles(remoteFiles);
|
|
219
|
+
const result = {
|
|
220
|
+
downloaded: [],
|
|
221
|
+
deleted: [],
|
|
222
|
+
unchanged: [],
|
|
223
|
+
conflicts: [],
|
|
224
|
+
conflictFilesWritten: []
|
|
225
|
+
};
|
|
226
|
+
import_logger.logger.dim(`Fetching ${remoteFiles.length} files in parallel...`);
|
|
227
|
+
const fileContents = await (0, import_helpers.fetchWithConcurrency)(
|
|
228
|
+
remoteFiles,
|
|
229
|
+
async (file, index) => {
|
|
230
|
+
const content = await this.farseerClient.getFileContentAsBuffer(file.reference);
|
|
231
|
+
if ((index + 1) % 10 === 0 || index === remoteFiles.length - 1) {
|
|
232
|
+
process.stdout.write(`\r Progress: ${index + 1}/${remoteFiles.length} files`);
|
|
233
|
+
}
|
|
234
|
+
return { file, content };
|
|
235
|
+
},
|
|
236
|
+
15
|
|
237
|
+
);
|
|
238
|
+
if (remoteFiles.length > 0) {
|
|
239
|
+
process.stdout.write("\r" + " ".repeat(50) + "\r");
|
|
240
|
+
}
|
|
241
|
+
const remoteContentMap = /* @__PURE__ */ new Map();
|
|
242
|
+
const remoteHashes = /* @__PURE__ */ new Map();
|
|
243
|
+
for (const { file, content } of fileContents) {
|
|
244
|
+
const hash = (0, import_helpers.calculateHash)(content);
|
|
245
|
+
remoteContentMap.set(file.path, { file, content, hash });
|
|
246
|
+
remoteHashes.set(file.path, hash);
|
|
247
|
+
}
|
|
248
|
+
const localFiles = (0, import_helpers.getAllFilesInDir)(filesDir);
|
|
249
|
+
const localHashes = /* @__PURE__ */ new Map();
|
|
250
|
+
for (const localFile of localFiles) {
|
|
251
|
+
if (!options?.all && !import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => localFile.toLowerCase().endsWith(ext))) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const localPath = path.join(filesDir, localFile);
|
|
255
|
+
const content = fs.readFileSync(localPath);
|
|
256
|
+
localHashes.set(localFile, (0, import_helpers.calculateHash)(content));
|
|
257
|
+
}
|
|
258
|
+
const comparisons = this.compareFiles({ localHashes, remoteHashes, syncState, all: options?.all });
|
|
259
|
+
const resolutions = options?.conflictResolutions ?? {};
|
|
260
|
+
for (const comparison of comparisons) {
|
|
261
|
+
const { filePath, changeType } = comparison;
|
|
262
|
+
const localPath = path.join(filesDir, filePath);
|
|
263
|
+
const remoteInfo = remoteContentMap.get(filePath);
|
|
264
|
+
switch (changeType) {
|
|
265
|
+
case "unchanged":
|
|
266
|
+
case "local_modified":
|
|
267
|
+
case "local_new":
|
|
268
|
+
result.unchanged.push(filePath);
|
|
269
|
+
if (remoteInfo) {
|
|
270
|
+
const existingSync = syncState.files[filePath];
|
|
271
|
+
if (existingSync) {
|
|
272
|
+
existingSync.remoteHash = remoteInfo.hash;
|
|
273
|
+
existingSync.metadata = this.buildMetadata(remoteInfo.file);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
break;
|
|
277
|
+
case "local_deleted":
|
|
278
|
+
result.unchanged.push(filePath);
|
|
279
|
+
break;
|
|
280
|
+
case "remote_modified":
|
|
281
|
+
case "remote_new":
|
|
282
|
+
(0, import_helpers.ensureDirectoryExists)(path.dirname(localPath));
|
|
283
|
+
fs.writeFileSync(localPath, remoteInfo.content);
|
|
284
|
+
result.downloaded.push(filePath);
|
|
285
|
+
syncState.files[filePath] = {
|
|
286
|
+
localHash: remoteInfo.hash,
|
|
287
|
+
remoteHash: remoteInfo.hash,
|
|
288
|
+
metadata: this.buildMetadata(remoteInfo.file)
|
|
289
|
+
};
|
|
290
|
+
break;
|
|
291
|
+
case "remote_deleted":
|
|
292
|
+
if (fs.existsSync(localPath)) {
|
|
293
|
+
fs.unlinkSync(localPath);
|
|
294
|
+
}
|
|
295
|
+
result.deleted.push(filePath);
|
|
296
|
+
delete syncState.files[filePath];
|
|
297
|
+
break;
|
|
298
|
+
case "conflict_both_modified":
|
|
299
|
+
case "conflict_local_modified_remote_deleted":
|
|
300
|
+
case "conflict_local_deleted_remote_modified":
|
|
301
|
+
case "conflict_both_new": {
|
|
302
|
+
if (options?.force) {
|
|
303
|
+
if (remoteInfo) {
|
|
304
|
+
(0, import_helpers.ensureDirectoryExists)(path.dirname(localPath));
|
|
305
|
+
fs.writeFileSync(localPath, remoteInfo.content);
|
|
306
|
+
result.downloaded.push(filePath);
|
|
307
|
+
syncState.files[filePath] = {
|
|
308
|
+
localHash: remoteInfo.hash,
|
|
309
|
+
remoteHash: remoteInfo.hash,
|
|
310
|
+
metadata: this.buildMetadata(remoteInfo.file)
|
|
311
|
+
};
|
|
312
|
+
} else {
|
|
313
|
+
if (fs.existsSync(localPath)) {
|
|
314
|
+
fs.unlinkSync(localPath);
|
|
315
|
+
result.deleted.push(filePath);
|
|
316
|
+
}
|
|
317
|
+
delete syncState.files[filePath];
|
|
318
|
+
}
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
const resolution = resolutions[filePath];
|
|
322
|
+
if (!resolution) {
|
|
323
|
+
result.conflicts.push(SyncService.buildConflicts([comparison])[0]);
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
switch (resolution) {
|
|
327
|
+
case "keep_local":
|
|
328
|
+
if (fs.existsSync(localPath)) {
|
|
329
|
+
const localContent = fs.readFileSync(localPath);
|
|
330
|
+
const localHash = (0, import_helpers.calculateHash)(localContent);
|
|
331
|
+
syncState.files[filePath] = {
|
|
332
|
+
localHash,
|
|
333
|
+
remoteHash: comparison.currentRemoteHash ?? "",
|
|
334
|
+
metadata: remoteInfo ? this.buildMetadata(remoteInfo.file) : void 0
|
|
335
|
+
};
|
|
336
|
+
} else {
|
|
337
|
+
delete syncState.files[filePath];
|
|
338
|
+
}
|
|
339
|
+
result.unchanged.push(filePath);
|
|
340
|
+
break;
|
|
341
|
+
case "keep_remote":
|
|
342
|
+
if (remoteInfo) {
|
|
343
|
+
(0, import_helpers.ensureDirectoryExists)(path.dirname(localPath));
|
|
344
|
+
fs.writeFileSync(localPath, remoteInfo.content);
|
|
345
|
+
result.downloaded.push(filePath);
|
|
346
|
+
syncState.files[filePath] = {
|
|
347
|
+
localHash: remoteInfo.hash,
|
|
348
|
+
remoteHash: remoteInfo.hash,
|
|
349
|
+
metadata: this.buildMetadata(remoteInfo.file)
|
|
350
|
+
};
|
|
351
|
+
} else {
|
|
352
|
+
if (fs.existsSync(localPath)) {
|
|
353
|
+
fs.unlinkSync(localPath);
|
|
354
|
+
result.deleted.push(filePath);
|
|
355
|
+
}
|
|
356
|
+
delete syncState.files[filePath];
|
|
357
|
+
}
|
|
358
|
+
break;
|
|
359
|
+
case "keep_both":
|
|
360
|
+
if (remoteInfo) {
|
|
361
|
+
const ext = path.extname(filePath);
|
|
362
|
+
const base = filePath.slice(0, -ext.length || void 0);
|
|
363
|
+
const conflictFileName = `${base}.remote${ext}`;
|
|
364
|
+
const conflictPath = path.join(filesDir, conflictFileName);
|
|
365
|
+
(0, import_helpers.ensureDirectoryExists)(path.dirname(conflictPath));
|
|
366
|
+
fs.writeFileSync(conflictPath, remoteInfo.content);
|
|
367
|
+
result.conflictFilesWritten.push(conflictFileName);
|
|
368
|
+
}
|
|
369
|
+
if (fs.existsSync(localPath)) {
|
|
370
|
+
const localContent = fs.readFileSync(localPath);
|
|
371
|
+
const localHash = (0, import_helpers.calculateHash)(localContent);
|
|
372
|
+
syncState.files[filePath] = {
|
|
373
|
+
localHash,
|
|
374
|
+
remoteHash: comparison.currentRemoteHash ?? "",
|
|
375
|
+
metadata: remoteInfo ? this.buildMetadata(remoteInfo.file) : void 0
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
result.unchanged.push(filePath);
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
syncState.lastSync = (/* @__PURE__ */ new Date()).toISOString();
|
|
386
|
+
this.saveSyncState(syncState);
|
|
387
|
+
return result;
|
|
388
|
+
}
|
|
389
|
+
buildMetadata(file) {
|
|
390
|
+
if (!file.metadata) return void 0;
|
|
391
|
+
return {
|
|
392
|
+
uploadTime: file.metadata.uploadTime,
|
|
393
|
+
uploaderEmail: file.metadata.uploader?.email,
|
|
394
|
+
uploaderName: file.metadata.uploader ? `${file.metadata.uploader.firstName} ${file.metadata.uploader.lastName}`.trim() : void 0,
|
|
395
|
+
lastFetched: (/* @__PURE__ */ new Date()).toISOString()
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
async push(options) {
|
|
399
|
+
const filesDir = (0, import_helpers.getTenantFilesDir)(this.tenant);
|
|
400
|
+
const syncState = this.loadSyncState();
|
|
401
|
+
const result = {
|
|
402
|
+
uploaded: [],
|
|
403
|
+
updated: [],
|
|
404
|
+
deleted: [],
|
|
405
|
+
unchanged: [],
|
|
406
|
+
conflicts: []
|
|
407
|
+
};
|
|
408
|
+
const remoteFiles = await this.farseerClient.listFiles();
|
|
409
|
+
const remoteFileMap = /* @__PURE__ */ new Map();
|
|
410
|
+
for (const file of remoteFiles) {
|
|
411
|
+
remoteFileMap.set(file.path, file);
|
|
412
|
+
}
|
|
413
|
+
const localFiles = (0, import_helpers.getAllFilesInDir)(filesDir);
|
|
414
|
+
const localHashes = /* @__PURE__ */ new Map();
|
|
415
|
+
const localContents = /* @__PURE__ */ new Map();
|
|
416
|
+
for (const localFile of localFiles) {
|
|
417
|
+
const localPath = path.join(filesDir, localFile);
|
|
418
|
+
const content = fs.readFileSync(localPath);
|
|
419
|
+
localHashes.set(localFile, (0, import_helpers.calculateHash)(content));
|
|
420
|
+
localContents.set(localFile, content);
|
|
421
|
+
}
|
|
422
|
+
const candidateFiles = [];
|
|
423
|
+
for (const localFile of localFiles) {
|
|
424
|
+
const syncInfo = syncState.files[localFile];
|
|
425
|
+
const localHash = localHashes.get(localFile);
|
|
426
|
+
if (!syncInfo || localHash !== syncInfo.localHash) {
|
|
427
|
+
if (remoteFileMap.has(localFile)) {
|
|
428
|
+
candidateFiles.push(localFile);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const locallyDeletedFiles = Object.keys(syncState.files).filter(
|
|
433
|
+
(f) => !localFiles.includes(f) && remoteFileMap.has(f)
|
|
434
|
+
);
|
|
435
|
+
const filesToFetchRemote = [.../* @__PURE__ */ new Set([...candidateFiles, ...locallyDeletedFiles])];
|
|
436
|
+
const remoteHashes = /* @__PURE__ */ new Map();
|
|
437
|
+
if (filesToFetchRemote.length > 0 && !options?.force) {
|
|
438
|
+
const toFetch = filesToFetchRemote.filter((f) => remoteFileMap.has(f)).map((f) => remoteFileMap.get(f));
|
|
439
|
+
const fetched = await (0, import_helpers.fetchWithConcurrency)(toFetch, async (file) => {
|
|
440
|
+
const content = await this.farseerClient.getFileContentAsBuffer(file.reference);
|
|
441
|
+
return { file, hash: (0, import_helpers.calculateHash)(content) };
|
|
442
|
+
}, 15);
|
|
443
|
+
for (const { file, hash } of fetched) {
|
|
444
|
+
remoteHashes.set(file.path, hash);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const comparisons = this.compareFiles({ localHashes, remoteHashes, syncState, all: true });
|
|
448
|
+
const conflictPaths = /* @__PURE__ */ new Set();
|
|
449
|
+
if (!options?.force) {
|
|
450
|
+
for (const c of comparisons) {
|
|
451
|
+
if (c.changeType.startsWith("conflict_")) {
|
|
452
|
+
const conflict = SyncService.buildConflicts([c])[0];
|
|
453
|
+
result.conflicts.push(conflict);
|
|
454
|
+
conflictPaths.add(c.filePath);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
for (const localFile of localFiles) {
|
|
459
|
+
if (conflictPaths.has(localFile)) continue;
|
|
460
|
+
const localPath = path.join(filesDir, localFile);
|
|
461
|
+
const content = localContents.get(localFile);
|
|
462
|
+
const localHash = localHashes.get(localFile);
|
|
463
|
+
const remoteFile = remoteFileMap.get(localFile);
|
|
464
|
+
const syncInfo = syncState.files[localFile];
|
|
465
|
+
if (remoteFile) {
|
|
466
|
+
if (syncInfo && localHash === syncInfo.localHash) {
|
|
467
|
+
result.unchanged.push(localFile);
|
|
468
|
+
} else {
|
|
469
|
+
await this.farseerClient.updateFile(remoteFile.reference, content, path.basename(localFile));
|
|
470
|
+
result.updated.push(localFile);
|
|
471
|
+
syncState.files[localFile] = {
|
|
472
|
+
localHash,
|
|
473
|
+
remoteHash: localHash
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
} else {
|
|
477
|
+
if (syncInfo && localHash === syncInfo.localHash) {
|
|
478
|
+
result.unchanged.push(localFile);
|
|
479
|
+
} else {
|
|
480
|
+
const pathParts = localFile.split("/");
|
|
481
|
+
const fileName = pathParts.pop();
|
|
482
|
+
const folderPath = ["Files", ...pathParts];
|
|
483
|
+
await this.farseerClient.createFile(content, fileName, folderPath);
|
|
484
|
+
result.uploaded.push(localFile);
|
|
485
|
+
syncState.files[localFile] = {
|
|
486
|
+
localHash,
|
|
487
|
+
remoteHash: localHash
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
const localFileSet = new Set(localFiles);
|
|
493
|
+
for (const [remotePath, remoteFile] of remoteFileMap) {
|
|
494
|
+
if (!localFileSet.has(remotePath) && !conflictPaths.has(remotePath)) {
|
|
495
|
+
const syncInfo = syncState.files[remotePath];
|
|
496
|
+
if (syncInfo) {
|
|
497
|
+
await this.farseerClient.deleteFile(remoteFile.reference);
|
|
498
|
+
result.deleted.push(remotePath);
|
|
499
|
+
delete syncState.files[remotePath];
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
syncState.lastSync = (/* @__PURE__ */ new Date()).toISOString();
|
|
504
|
+
this.saveSyncState(syncState);
|
|
505
|
+
return result;
|
|
506
|
+
}
|
|
507
|
+
async checkRemoteChanges() {
|
|
508
|
+
const syncState = this.loadSyncState();
|
|
509
|
+
let remoteFiles = await this.farseerClient.listFiles();
|
|
510
|
+
remoteFiles = remoteFiles.filter(
|
|
511
|
+
(f) => import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => f.name.toLowerCase().endsWith(ext))
|
|
512
|
+
);
|
|
513
|
+
for (const file of remoteFiles) {
|
|
514
|
+
const syncInfo = syncState.files[file.path];
|
|
515
|
+
if (!syncInfo) {
|
|
516
|
+
return true;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
for (const filePath of Object.keys(syncState.files)) {
|
|
520
|
+
const isScript = import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => filePath.toLowerCase().endsWith(ext));
|
|
521
|
+
if (!isScript) continue;
|
|
522
|
+
const exists = remoteFiles.some((f) => f.path === filePath);
|
|
523
|
+
if (!exists && syncState.files[filePath].remoteHash) {
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
async getFileDiff(filePath) {
|
|
530
|
+
const filesDir = (0, import_helpers.getTenantFilesDir)(this.tenant);
|
|
531
|
+
const localPath = path.join(filesDir, filePath);
|
|
532
|
+
let localContent = null;
|
|
533
|
+
let remoteContent = null;
|
|
534
|
+
if (fs.existsSync(localPath)) {
|
|
535
|
+
localContent = fs.readFileSync(localPath, "utf-8");
|
|
536
|
+
}
|
|
537
|
+
const remoteFile = await this.farseerClient.getFileByPath(filePath);
|
|
538
|
+
if (remoteFile) {
|
|
539
|
+
remoteContent = await this.farseerClient.getFileContent(remoteFile.reference);
|
|
540
|
+
}
|
|
541
|
+
return { localContent, remoteContent };
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Three-way comparison of all files.
|
|
545
|
+
* Compares current local state, current remote state, and baseline from sync state.
|
|
546
|
+
*/
|
|
547
|
+
compareFiles(options) {
|
|
548
|
+
const { localHashes, remoteHashes, syncState } = options;
|
|
549
|
+
const allPaths = /* @__PURE__ */ new Set([
|
|
550
|
+
...localHashes.keys(),
|
|
551
|
+
...remoteHashes.keys(),
|
|
552
|
+
...Object.keys(syncState.files)
|
|
553
|
+
]);
|
|
554
|
+
const results = [];
|
|
555
|
+
for (const filePath of allPaths) {
|
|
556
|
+
if (!options.all) {
|
|
557
|
+
if (!import_farseerFactory.DEFAULT_SCRIPT_EXTENSIONS.some((ext) => filePath.toLowerCase().endsWith(ext))) {
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const currentLocalHash = localHashes.get(filePath) ?? null;
|
|
562
|
+
const currentRemoteHash = remoteHashes.get(filePath) ?? null;
|
|
563
|
+
const baseline = syncState.files[filePath];
|
|
564
|
+
const baselineLocalHash = baseline?.localHash ?? null;
|
|
565
|
+
const baselineRemoteHash = baseline?.remoteHash ?? null;
|
|
566
|
+
const localExists = currentLocalHash !== null;
|
|
567
|
+
const remoteExists = currentRemoteHash !== null;
|
|
568
|
+
const hadBaseline = baseline != null;
|
|
569
|
+
let changeType;
|
|
570
|
+
if (localExists && remoteExists) {
|
|
571
|
+
if (currentLocalHash === currentRemoteHash) {
|
|
572
|
+
changeType = "unchanged";
|
|
573
|
+
} else if (!hadBaseline) {
|
|
574
|
+
changeType = "conflict_both_new";
|
|
575
|
+
} else {
|
|
576
|
+
const localChanged = currentLocalHash !== baselineLocalHash;
|
|
577
|
+
const remoteChanged = currentRemoteHash !== baselineRemoteHash;
|
|
578
|
+
if (localChanged && remoteChanged) {
|
|
579
|
+
changeType = "conflict_both_modified";
|
|
580
|
+
} else if (localChanged) {
|
|
581
|
+
changeType = "local_modified";
|
|
582
|
+
} else if (remoteChanged) {
|
|
583
|
+
changeType = "remote_modified";
|
|
584
|
+
} else {
|
|
585
|
+
changeType = "unchanged";
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
} else if (localExists && !remoteExists) {
|
|
589
|
+
if (!hadBaseline) {
|
|
590
|
+
changeType = "local_new";
|
|
591
|
+
} else {
|
|
592
|
+
const localChanged = currentLocalHash !== baselineLocalHash;
|
|
593
|
+
if (localChanged) {
|
|
594
|
+
changeType = "conflict_local_modified_remote_deleted";
|
|
595
|
+
} else {
|
|
596
|
+
changeType = "remote_deleted";
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
} else if (!localExists && remoteExists) {
|
|
600
|
+
if (!hadBaseline) {
|
|
601
|
+
changeType = "remote_new";
|
|
602
|
+
} else {
|
|
603
|
+
const remoteChanged = currentRemoteHash !== baselineRemoteHash;
|
|
604
|
+
if (remoteChanged) {
|
|
605
|
+
changeType = "conflict_local_deleted_remote_modified";
|
|
606
|
+
} else {
|
|
607
|
+
changeType = "local_deleted";
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
} else {
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
results.push({
|
|
614
|
+
filePath,
|
|
615
|
+
changeType,
|
|
616
|
+
currentLocalHash,
|
|
617
|
+
currentRemoteHash,
|
|
618
|
+
baselineLocalHash,
|
|
619
|
+
baselineRemoteHash
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
return results;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Extract SyncConflict[] from FileCompareResult[].
|
|
626
|
+
*/
|
|
627
|
+
static buildConflicts(comparisons) {
|
|
628
|
+
const conflicts = [];
|
|
629
|
+
for (const c of comparisons) {
|
|
630
|
+
switch (c.changeType) {
|
|
631
|
+
case "conflict_both_modified":
|
|
632
|
+
conflicts.push({
|
|
633
|
+
filePath: c.filePath,
|
|
634
|
+
type: "both_modified",
|
|
635
|
+
description: "Both local and remote versions modified since last sync"
|
|
636
|
+
});
|
|
637
|
+
break;
|
|
638
|
+
case "conflict_local_modified_remote_deleted":
|
|
639
|
+
conflicts.push({
|
|
640
|
+
filePath: c.filePath,
|
|
641
|
+
type: "local_modified_remote_deleted",
|
|
642
|
+
description: "File modified locally but deleted on remote"
|
|
643
|
+
});
|
|
644
|
+
break;
|
|
645
|
+
case "conflict_local_deleted_remote_modified":
|
|
646
|
+
conflicts.push({
|
|
647
|
+
filePath: c.filePath,
|
|
648
|
+
type: "local_deleted_remote_modified",
|
|
649
|
+
description: "File deleted locally but modified on remote"
|
|
650
|
+
});
|
|
651
|
+
break;
|
|
652
|
+
case "conflict_both_new":
|
|
653
|
+
conflicts.push({
|
|
654
|
+
filePath: c.filePath,
|
|
655
|
+
type: "both_new",
|
|
656
|
+
description: "File exists both locally and remotely with different content (no sync baseline)"
|
|
657
|
+
});
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return conflicts;
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Batch fetch metadata for multiple files in parallel.
|
|
665
|
+
* Fetches metadata in batches of 10 to avoid overwhelming the API.
|
|
666
|
+
* Non-critical operation - failures are silently ignored.
|
|
667
|
+
*/
|
|
668
|
+
async fetchMetadataForFiles(files) {
|
|
669
|
+
const batchSize = 10;
|
|
670
|
+
for (let i = 0; i < files.length; i += batchSize) {
|
|
671
|
+
const batch = files.slice(i, i + batchSize);
|
|
672
|
+
await Promise.all(batch.map(async (file) => {
|
|
673
|
+
try {
|
|
674
|
+
const metadata = await this.farseerClient.getFileMetadata(file.reference);
|
|
675
|
+
if (metadata) {
|
|
676
|
+
file.metadata = metadata;
|
|
677
|
+
}
|
|
678
|
+
} catch {
|
|
679
|
+
}
|
|
680
|
+
}));
|
|
681
|
+
}
|
|
682
|
+
return files;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
686
|
+
0 && (module.exports = {
|
|
687
|
+
SyncService
|
|
688
|
+
});
|
|
689
|
+
//# sourceMappingURL=syncService.js.map
|