farseer-cli 1.0.8 → 1.0.10
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 +377 -0
- package/dist/commands/apps.js +91 -0
- package/dist/commands/checkout.js +164 -0
- package/dist/commands/config.js +141 -0
- package/dist/commands/diff.js +210 -0
- package/dist/commands/dimensions.js +130 -0
- package/dist/commands/files.js +88 -0
- package/dist/commands/install.js +82 -0
- package/dist/commands/list.js +77 -0
- package/dist/commands/login.js +109 -0
- package/dist/commands/logout.js +44 -0
- package/dist/commands/mcp-server.js +26 -0
- package/dist/commands/model.js +181 -0
- package/dist/commands/pull.js +286 -0
- package/dist/commands/push.js +235 -0
- package/dist/commands/run.js +336 -0
- package/dist/commands/status.js +178 -0
- package/dist/commands/variables.js +200 -0
- package/dist/commands/whoami.js +47 -0
- package/dist/index.js +163 -0
- package/dist/mcp/index.js +19 -0
- package/dist/mcp/prompts/workflows.js +403 -0
- package/dist/mcp/resources/documentation.js +158 -0
- package/dist/mcp/server.js +36 -0
- package/dist/mcp/tools/appTools.js +348 -0
- package/dist/mcp/tools/authTools.js +152 -0
- package/dist/mcp/tools/dataTools.js +358 -0
- package/dist/mcp/tools/modelTools.js +293 -0
- package/dist/mcp/tools/runTools.js +239 -0
- package/dist/mcp/tools/syncTools.js +383 -0
- package/dist/mcp/utils/helpers.js +142 -0
- package/dist/services/appSyncService.js +458 -0
- package/dist/services/configService.js +186 -0
- package/dist/services/farseerApi.js +426 -0
- package/dist/services/farseerFactory.js +297 -0
- package/dist/services/farseerService.js +647 -0
- package/dist/services/syncService.js +764 -0
- package/dist/utils/constants.js +21 -0
- package/dist/utils/helpers.js +446 -0
- package/dist/utils/logger.js +45 -0
- package/package.json +8 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* MCP Utility Functions
|
|
4
|
+
*
|
|
5
|
+
* Helper functions for MCP tools including tenant resolution and response formatting.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.resolveOrgAndTenant = resolveOrgAndTenant;
|
|
9
|
+
exports.resolveTenant = resolveTenant;
|
|
10
|
+
exports.resolveOrganisation = resolveOrganisation;
|
|
11
|
+
exports.getClient = getClient;
|
|
12
|
+
exports.successResponse = successResponse;
|
|
13
|
+
exports.errorResponse = errorResponse;
|
|
14
|
+
exports.authRequiredResponse = authRequiredResponse;
|
|
15
|
+
exports.tenantRequiredResponse = tenantRequiredResponse;
|
|
16
|
+
exports.getAuthStatus = getAuthStatus;
|
|
17
|
+
const configService_1 = require("../../services/configService");
|
|
18
|
+
const farseerFactory_1 = require("../../services/farseerFactory");
|
|
19
|
+
/**
|
|
20
|
+
* Resolve organisation and tenant from MCP tool parameters.
|
|
21
|
+
*
|
|
22
|
+
* Resolution order:
|
|
23
|
+
* 1. Explicit org/tenant params
|
|
24
|
+
* 2. Tenant param + saved org mapping (from previous checkout)
|
|
25
|
+
* 3. Current checkout
|
|
26
|
+
*
|
|
27
|
+
* Returns null if no tenant can be resolved.
|
|
28
|
+
*/
|
|
29
|
+
function resolveOrgAndTenant(params) {
|
|
30
|
+
const checkout = (0, configService_1.getCurrentCheckout)();
|
|
31
|
+
const { organisation: orgArg, tenant: tenantArg } = params;
|
|
32
|
+
// Case 1: Both provided explicitly
|
|
33
|
+
if (orgArg && tenantArg) {
|
|
34
|
+
return { organisation: orgArg, tenant: tenantArg };
|
|
35
|
+
}
|
|
36
|
+
// Case 2: Only tenant provided - look up org from saved mapping or assume org=tenant
|
|
37
|
+
if (tenantArg) {
|
|
38
|
+
const savedOrg = (0, configService_1.getTenantOrg)(tenantArg);
|
|
39
|
+
return { organisation: savedOrg || orgArg || tenantArg, tenant: tenantArg };
|
|
40
|
+
}
|
|
41
|
+
// Case 3: Only org provided (unusual, but treat as org=tenant)
|
|
42
|
+
if (orgArg) {
|
|
43
|
+
return { organisation: orgArg, tenant: orgArg };
|
|
44
|
+
}
|
|
45
|
+
// Case 4: No args - use checkout
|
|
46
|
+
if (checkout) {
|
|
47
|
+
return { organisation: checkout.organisation, tenant: checkout.tenant };
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolve tenant from argument or checkout state
|
|
53
|
+
*/
|
|
54
|
+
function resolveTenant(tenantArg) {
|
|
55
|
+
if (tenantArg) {
|
|
56
|
+
return tenantArg;
|
|
57
|
+
}
|
|
58
|
+
return (0, configService_1.getCurrentTenant)() || null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve organisation from argument or checkout state
|
|
62
|
+
*/
|
|
63
|
+
function resolveOrganisation(orgArg, tenantArg) {
|
|
64
|
+
if (orgArg) {
|
|
65
|
+
return orgArg;
|
|
66
|
+
}
|
|
67
|
+
if (tenantArg) {
|
|
68
|
+
const savedOrg = (0, configService_1.getTenantOrg)(tenantArg);
|
|
69
|
+
if (savedOrg)
|
|
70
|
+
return savedOrg;
|
|
71
|
+
return tenantArg;
|
|
72
|
+
}
|
|
73
|
+
const checkout = (0, configService_1.getCurrentCheckout)();
|
|
74
|
+
return checkout?.organisation || null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get authenticated Farseer client
|
|
78
|
+
*/
|
|
79
|
+
async function getClient(tenant, tenantId) {
|
|
80
|
+
const result = await (0, farseerFactory_1.getFarseerClientWithFallback)(tenant, tenantId ?? tenant);
|
|
81
|
+
return result?.client || null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create a success response
|
|
85
|
+
*/
|
|
86
|
+
function successResponse(data) {
|
|
87
|
+
return {
|
|
88
|
+
content: [
|
|
89
|
+
{
|
|
90
|
+
type: 'text',
|
|
91
|
+
text: JSON.stringify({ success: true, ...data }, null, 2),
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create an error response
|
|
98
|
+
*/
|
|
99
|
+
function errorResponse(message, errorCode) {
|
|
100
|
+
return {
|
|
101
|
+
content: [
|
|
102
|
+
{
|
|
103
|
+
type: 'text',
|
|
104
|
+
text: JSON.stringify({
|
|
105
|
+
success: false,
|
|
106
|
+
error: message,
|
|
107
|
+
errorCode: errorCode || 'UNKNOWN',
|
|
108
|
+
}, null, 2),
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
isError: true,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create auth required error response
|
|
116
|
+
*/
|
|
117
|
+
function authRequiredResponse(tenant) {
|
|
118
|
+
const message = tenant
|
|
119
|
+
? `Authentication required for tenant "${tenant}". Run "farseer login" to authenticate.`
|
|
120
|
+
: 'Authentication required. Run "farseer login" to authenticate.';
|
|
121
|
+
return errorResponse(message, 'AUTH_REQUIRED');
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Create tenant required error response
|
|
125
|
+
*/
|
|
126
|
+
function tenantRequiredResponse() {
|
|
127
|
+
return errorResponse('No tenant specified. Either provide tenant parameter or run "farseer checkout <tenant>" first.', 'VALIDATION_ERROR');
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get auth status info
|
|
131
|
+
*/
|
|
132
|
+
function getAuthStatus() {
|
|
133
|
+
const auth = (0, configService_1.getUserAuth)();
|
|
134
|
+
const checkout = (0, configService_1.getCurrentCheckout)();
|
|
135
|
+
const credentials = (0, configService_1.listCredentials)();
|
|
136
|
+
return {
|
|
137
|
+
jwtValid: (0, configService_1.isUserAuthValid)(),
|
|
138
|
+
jwtExpiresAt: auth?.expiresAt,
|
|
139
|
+
currentCheckout: checkout,
|
|
140
|
+
configuredTenants: Object.keys(credentials),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
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.AppSyncService = void 0;
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const helpers_1 = require("../utils/helpers");
|
|
40
|
+
const logger_1 = require("../utils/logger");
|
|
41
|
+
/**
|
|
42
|
+
* Sanitize Croatian diacritics and other special characters to ASCII equivalents
|
|
43
|
+
*/
|
|
44
|
+
function sanitizeDiacritics(text) {
|
|
45
|
+
const diacriticMap = {
|
|
46
|
+
// Croatian
|
|
47
|
+
č: 'c',
|
|
48
|
+
Č: 'C',
|
|
49
|
+
ć: 'c',
|
|
50
|
+
Ć: 'C',
|
|
51
|
+
š: 's',
|
|
52
|
+
Š: 'S',
|
|
53
|
+
ž: 'z',
|
|
54
|
+
Ž: 'Z',
|
|
55
|
+
đ: 'd',
|
|
56
|
+
Đ: 'D',
|
|
57
|
+
// German/Other common
|
|
58
|
+
ä: 'a',
|
|
59
|
+
Ä: 'A',
|
|
60
|
+
ö: 'o',
|
|
61
|
+
Ö: 'O',
|
|
62
|
+
ü: 'u',
|
|
63
|
+
Ü: 'U',
|
|
64
|
+
ß: 'ss',
|
|
65
|
+
// Polish
|
|
66
|
+
ą: 'a',
|
|
67
|
+
Ą: 'A',
|
|
68
|
+
ę: 'e',
|
|
69
|
+
Ę: 'E',
|
|
70
|
+
ł: 'l',
|
|
71
|
+
Ł: 'L',
|
|
72
|
+
ń: 'n',
|
|
73
|
+
Ń: 'N',
|
|
74
|
+
ó: 'o',
|
|
75
|
+
Ó: 'O',
|
|
76
|
+
ś: 's',
|
|
77
|
+
Ś: 'S',
|
|
78
|
+
ź: 'z',
|
|
79
|
+
Ź: 'Z',
|
|
80
|
+
ż: 'z',
|
|
81
|
+
Ż: 'Z',
|
|
82
|
+
// French/Spanish
|
|
83
|
+
à: 'a',
|
|
84
|
+
á: 'a',
|
|
85
|
+
â: 'a',
|
|
86
|
+
è: 'e',
|
|
87
|
+
é: 'e',
|
|
88
|
+
ê: 'e',
|
|
89
|
+
ë: 'e',
|
|
90
|
+
ì: 'i',
|
|
91
|
+
í: 'i',
|
|
92
|
+
î: 'i',
|
|
93
|
+
ï: 'i',
|
|
94
|
+
ò: 'o',
|
|
95
|
+
ô: 'o',
|
|
96
|
+
ù: 'u',
|
|
97
|
+
ú: 'u',
|
|
98
|
+
û: 'u',
|
|
99
|
+
ñ: 'n',
|
|
100
|
+
Ñ: 'N',
|
|
101
|
+
ç: 'c',
|
|
102
|
+
Ç: 'C',
|
|
103
|
+
};
|
|
104
|
+
return text
|
|
105
|
+
.split('')
|
|
106
|
+
.map((char) => diacriticMap[char] || char)
|
|
107
|
+
.join('');
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Service for syncing app configurations between local JSON files and remote Farseer instance
|
|
111
|
+
*/
|
|
112
|
+
class AppSyncService {
|
|
113
|
+
constructor(tenant, api) {
|
|
114
|
+
this.api = api;
|
|
115
|
+
this.appsDir = (0, helpers_1.getTenantAppsDir)(tenant);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get filename for an app (keep original name, just sanitize for filesystem)
|
|
119
|
+
*/
|
|
120
|
+
getAppFilename(name) {
|
|
121
|
+
return (sanitizeDiacritics(name)
|
|
122
|
+
.replace(/[<>:"/\\|?*]/g, '') // Remove invalid filename characters
|
|
123
|
+
.trim() + '.json');
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Load all local app configs
|
|
127
|
+
*/
|
|
128
|
+
loadLocalApps() {
|
|
129
|
+
const apps = new Map();
|
|
130
|
+
if (!fs.existsSync(this.appsDir)) {
|
|
131
|
+
return apps;
|
|
132
|
+
}
|
|
133
|
+
const files = fs.readdirSync(this.appsDir).filter((f) => f.endsWith('.json'));
|
|
134
|
+
for (const file of files) {
|
|
135
|
+
try {
|
|
136
|
+
const content = fs.readFileSync(path.join(this.appsDir, file), 'utf-8');
|
|
137
|
+
const config = JSON.parse(content);
|
|
138
|
+
apps.set(config.name.toLowerCase(), config);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// Skip invalid files
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return apps;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Save a local app config
|
|
148
|
+
*/
|
|
149
|
+
saveLocalApp(config) {
|
|
150
|
+
(0, helpers_1.ensureDirectoryExists)(this.appsDir);
|
|
151
|
+
const filename = this.getAppFilename(config.name);
|
|
152
|
+
const filePath = path.join(this.appsDir, filename);
|
|
153
|
+
fs.writeFileSync(filePath, JSON.stringify(config, null, 2), 'utf-8');
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Delete a local app config
|
|
157
|
+
*/
|
|
158
|
+
deleteLocalApp(name) {
|
|
159
|
+
const filename = this.getAppFilename(name);
|
|
160
|
+
const filePath = path.join(this.appsDir, filename);
|
|
161
|
+
if (fs.existsSync(filePath)) {
|
|
162
|
+
fs.unlinkSync(filePath);
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Convert remote app to local config format
|
|
169
|
+
*/
|
|
170
|
+
remoteToLocal(remote, reference) {
|
|
171
|
+
const scriptFiles = remote.scriptFiles ?? [];
|
|
172
|
+
const mainScript = scriptFiles.find((s) => s.id === remote.mainScriptFileId);
|
|
173
|
+
return {
|
|
174
|
+
name: remote.name,
|
|
175
|
+
description: remote.description || '',
|
|
176
|
+
entrypoint: mainScript?.name || null,
|
|
177
|
+
scripts: scriptFiles.map((s) => s.name),
|
|
178
|
+
arguments: remote.expectedArguments.map((a) => ({
|
|
179
|
+
name: a.name,
|
|
180
|
+
defaultValue: a.defaultValue,
|
|
181
|
+
})),
|
|
182
|
+
_remote: {
|
|
183
|
+
id: remote.id,
|
|
184
|
+
reference: reference,
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Check if local and remote app configs are equivalent
|
|
190
|
+
*/
|
|
191
|
+
areAppsEqual(local, remote) {
|
|
192
|
+
// Compare name
|
|
193
|
+
if (local.name !== remote.name)
|
|
194
|
+
return false;
|
|
195
|
+
// Compare description
|
|
196
|
+
if ((local.description || '') !== (remote.description || ''))
|
|
197
|
+
return false;
|
|
198
|
+
// Compare entrypoint
|
|
199
|
+
const remoteScriptFiles = remote.scriptFiles ?? [];
|
|
200
|
+
const remoteEntrypoint = remoteScriptFiles.find((s) => s.id === remote.mainScriptFileId)?.name || null;
|
|
201
|
+
if (local.entrypoint !== remoteEntrypoint)
|
|
202
|
+
return false;
|
|
203
|
+
// Compare scripts
|
|
204
|
+
const localScripts = [...local.scripts].sort();
|
|
205
|
+
const remoteScripts = [...remoteScriptFiles.map((s) => s.name)].sort();
|
|
206
|
+
if (JSON.stringify(localScripts) !== JSON.stringify(remoteScripts))
|
|
207
|
+
return false;
|
|
208
|
+
// Compare arguments
|
|
209
|
+
const localArgs = local.arguments.map((a) => `${a.name}=${a.defaultValue}`).sort();
|
|
210
|
+
const remoteArgs = remote.expectedArguments.map((a) => `${a.name}=${a.defaultValue}`).sort();
|
|
211
|
+
if (JSON.stringify(localArgs) !== JSON.stringify(remoteArgs))
|
|
212
|
+
return false;
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Pull apps from remote and save locally
|
|
217
|
+
*/
|
|
218
|
+
async pull() {
|
|
219
|
+
const result = {
|
|
220
|
+
created: [],
|
|
221
|
+
updated: [],
|
|
222
|
+
deleted: [],
|
|
223
|
+
unchanged: [],
|
|
224
|
+
};
|
|
225
|
+
// Load local apps
|
|
226
|
+
const localApps = this.loadLocalApps();
|
|
227
|
+
// Fetch remote apps
|
|
228
|
+
const remoteAppList = await this.api.listApps();
|
|
229
|
+
const remoteApps = new Map();
|
|
230
|
+
// Fetch app details in parallel with concurrency limit
|
|
231
|
+
logger_1.logger.dim(`Fetching ${remoteAppList.length} apps in parallel...`);
|
|
232
|
+
const appDetails = await (0, helpers_1.fetchWithConcurrency)(remoteAppList, async (item, index) => {
|
|
233
|
+
const app = await this.api.getApp(item.reference);
|
|
234
|
+
// Show progress every 10 apps
|
|
235
|
+
if ((index + 1) % 10 === 0 || index === remoteAppList.length - 1) {
|
|
236
|
+
process.stdout.write(`\r Progress: ${index + 1}/${remoteAppList.length} apps`);
|
|
237
|
+
}
|
|
238
|
+
return { app, reference: item.reference };
|
|
239
|
+
}, 15);
|
|
240
|
+
// Clear progress line
|
|
241
|
+
if (remoteAppList.length > 0) {
|
|
242
|
+
process.stdout.write('\r' + ' '.repeat(50) + '\r');
|
|
243
|
+
}
|
|
244
|
+
for (const { app, reference } of appDetails) {
|
|
245
|
+
if (app) {
|
|
246
|
+
remoteApps.set(app.name.toLowerCase(), { app, reference });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Process remote apps
|
|
250
|
+
for (const [key, { app, reference }] of remoteApps) {
|
|
251
|
+
const localConfig = this.remoteToLocal(app, reference);
|
|
252
|
+
const existingLocal = localApps.get(key);
|
|
253
|
+
if (!existingLocal) {
|
|
254
|
+
// New app from remote
|
|
255
|
+
this.saveLocalApp(localConfig);
|
|
256
|
+
result.created.push(app.name);
|
|
257
|
+
}
|
|
258
|
+
else if (!this.areAppsEqual(existingLocal, app)) {
|
|
259
|
+
// App changed on remote - update local
|
|
260
|
+
this.saveLocalApp(localConfig);
|
|
261
|
+
result.updated.push(app.name);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
result.unchanged.push(app.name);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// Check for apps deleted on remote
|
|
268
|
+
for (const [key, local] of localApps) {
|
|
269
|
+
if (!remoteApps.has(key) && local._remote) {
|
|
270
|
+
// App was synced before but now deleted on remote
|
|
271
|
+
this.deleteLocalApp(local.name);
|
|
272
|
+
result.deleted.push(local.name);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Push local apps to remote
|
|
279
|
+
*/
|
|
280
|
+
async push() {
|
|
281
|
+
const result = {
|
|
282
|
+
created: [],
|
|
283
|
+
updated: [],
|
|
284
|
+
deleted: [],
|
|
285
|
+
unchanged: [],
|
|
286
|
+
};
|
|
287
|
+
// Load local apps
|
|
288
|
+
const localApps = this.loadLocalApps();
|
|
289
|
+
// Fetch remote apps
|
|
290
|
+
const remoteAppList = await this.api.listApps();
|
|
291
|
+
const remoteApps = new Map();
|
|
292
|
+
// Fetch app details in parallel with concurrency limit
|
|
293
|
+
const appDetails = await (0, helpers_1.fetchWithConcurrency)(remoteAppList, async (item) => {
|
|
294
|
+
const app = await this.api.getApp(item.reference);
|
|
295
|
+
return { app, reference: item.reference };
|
|
296
|
+
}, 15);
|
|
297
|
+
for (const { app, reference } of appDetails) {
|
|
298
|
+
if (app) {
|
|
299
|
+
remoteApps.set(app.name.toLowerCase(), { app, reference });
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// Get available scripts for ID lookup
|
|
303
|
+
// Build maps for both path-based and filename-based lookup (backward compatibility)
|
|
304
|
+
const availableScripts = await this.api.getScriptFiles();
|
|
305
|
+
const scriptIdByPath = new Map();
|
|
306
|
+
const scriptIdByName = new Map();
|
|
307
|
+
for (const s of availableScripts) {
|
|
308
|
+
// s.name now contains full path (e.g., "Scripts/test.ts")
|
|
309
|
+
const pathKey = s.name.toLowerCase();
|
|
310
|
+
const filenameKey = s.name.split('/').pop()?.toLowerCase() || pathKey;
|
|
311
|
+
scriptIdByPath.set(pathKey, s.id);
|
|
312
|
+
// For filename lookup, only set if not already set (first match wins for ambiguous names)
|
|
313
|
+
if (!scriptIdByName.has(filenameKey)) {
|
|
314
|
+
scriptIdByName.set(filenameKey, s.id);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// Helper to find script ID by path or filename
|
|
318
|
+
const findScriptId = (scriptRef) => {
|
|
319
|
+
const key = scriptRef.toLowerCase();
|
|
320
|
+
// Try exact path match first
|
|
321
|
+
const byPath = scriptIdByPath.get(key);
|
|
322
|
+
if (byPath)
|
|
323
|
+
return byPath;
|
|
324
|
+
// Try with Scripts/ prefix
|
|
325
|
+
const withPrefix = scriptIdByPath.get(`scripts/${key}`);
|
|
326
|
+
if (withPrefix)
|
|
327
|
+
return withPrefix;
|
|
328
|
+
// Fall back to filename match
|
|
329
|
+
return scriptIdByName.get(key.split('/').pop() || key);
|
|
330
|
+
};
|
|
331
|
+
// Process local apps
|
|
332
|
+
for (const [key, local] of localApps) {
|
|
333
|
+
const remote = remoteApps.get(key);
|
|
334
|
+
if (!remote) {
|
|
335
|
+
// New local app - create on remote
|
|
336
|
+
try {
|
|
337
|
+
const created = await this.api.createApp(local.name);
|
|
338
|
+
// Update with config (API requires name and description)
|
|
339
|
+
const update = this.buildUpdatePayload(local, findScriptId);
|
|
340
|
+
update.name = local.name;
|
|
341
|
+
update.description = local.description;
|
|
342
|
+
await this.api.updateApp(created.id.toString(), update);
|
|
343
|
+
// Update local with remote ID
|
|
344
|
+
local._remote = {
|
|
345
|
+
id: created.id,
|
|
346
|
+
reference: created.id.toString(),
|
|
347
|
+
};
|
|
348
|
+
this.saveLocalApp(local);
|
|
349
|
+
result.created.push(local.name);
|
|
350
|
+
}
|
|
351
|
+
catch (error) {
|
|
352
|
+
console.error(`Failed to create app ${local.name}:`, error);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
else if (!this.areAppsEqual(local, remote.app)) {
|
|
356
|
+
// App differs - update remote
|
|
357
|
+
try {
|
|
358
|
+
const update = this.buildUpdatePayload(local, findScriptId);
|
|
359
|
+
update.name = local.name;
|
|
360
|
+
update.description = local.description;
|
|
361
|
+
await this.api.updateApp(remote.reference, update);
|
|
362
|
+
result.updated.push(local.name);
|
|
363
|
+
}
|
|
364
|
+
catch (error) {
|
|
365
|
+
console.error(`Failed to update app ${local.name}:`, error);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
result.unchanged.push(local.name);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
// Note: We don't auto-delete remote apps when deleted locally
|
|
373
|
+
// User must use "farseer app delete" explicitly
|
|
374
|
+
return result;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Build update payload for API
|
|
378
|
+
*/
|
|
379
|
+
buildUpdatePayload(local, findScriptId) {
|
|
380
|
+
const update = {};
|
|
381
|
+
// Arguments
|
|
382
|
+
if (local.arguments.length > 0) {
|
|
383
|
+
update.expectedArguments = local.arguments.map((a) => ({
|
|
384
|
+
name: a.name,
|
|
385
|
+
type: 'variable',
|
|
386
|
+
defaultValue: a.defaultValue,
|
|
387
|
+
}));
|
|
388
|
+
}
|
|
389
|
+
// Scripts - lookup by path or filename
|
|
390
|
+
if (local.scripts.length > 0) {
|
|
391
|
+
const scriptIds = [];
|
|
392
|
+
for (const scriptRef of local.scripts) {
|
|
393
|
+
const id = findScriptId(scriptRef);
|
|
394
|
+
if (id) {
|
|
395
|
+
scriptIds.push(id);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (scriptIds.length > 0) {
|
|
399
|
+
update.scriptFileIds = scriptIds;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// Entrypoint - lookup by path or filename
|
|
403
|
+
if (local.entrypoint) {
|
|
404
|
+
const entrypointId = findScriptId(local.entrypoint);
|
|
405
|
+
if (entrypointId) {
|
|
406
|
+
update.mainScriptFileId = entrypointId;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return update;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Get sync status without making changes
|
|
413
|
+
*/
|
|
414
|
+
async getStatus() {
|
|
415
|
+
const status = {
|
|
416
|
+
newLocal: [],
|
|
417
|
+
newRemote: [],
|
|
418
|
+
modified: [],
|
|
419
|
+
synced: [],
|
|
420
|
+
};
|
|
421
|
+
// Load local apps
|
|
422
|
+
const localApps = this.loadLocalApps();
|
|
423
|
+
// Fetch remote apps
|
|
424
|
+
const remoteAppList = await this.api.listApps();
|
|
425
|
+
const remoteApps = new Map();
|
|
426
|
+
// Fetch app details in parallel with concurrency limit
|
|
427
|
+
const appDetails = await (0, helpers_1.fetchWithConcurrency)(remoteAppList, async (item) => {
|
|
428
|
+
const app = await this.api.getApp(item.reference);
|
|
429
|
+
return { app, reference: item.reference };
|
|
430
|
+
}, 15);
|
|
431
|
+
for (const { app, reference } of appDetails) {
|
|
432
|
+
if (app) {
|
|
433
|
+
remoteApps.set(app.name.toLowerCase(), { app, reference });
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// Check local apps
|
|
437
|
+
for (const [key, local] of localApps) {
|
|
438
|
+
const remote = remoteApps.get(key);
|
|
439
|
+
if (!remote) {
|
|
440
|
+
status.newLocal.push(local.name);
|
|
441
|
+
}
|
|
442
|
+
else if (!this.areAppsEqual(local, remote.app)) {
|
|
443
|
+
status.modified.push(local.name);
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
status.synced.push(local.name);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
// Check for new remote apps
|
|
450
|
+
for (const [key, { app }] of remoteApps) {
|
|
451
|
+
if (!localApps.has(key)) {
|
|
452
|
+
status.newRemote.push(app.name);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return status;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
exports.AppSyncService = AppSyncService;
|