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