@advanced-communities/salesforce-mcp-server
Version:
MCP server enabling AI assistants to interact with Salesforce orgs through the Salesforce CLI
87 lines (86 loc) • 3.01 kB
JavaScript
import { exec } from "node:child_process";
let cachedResult = null;
let lastCheckTime = 0;
const CACHE_DURATION = 60 * 60 * 1000; // 1 hour
function parseUpdateOutput(output) {
const lines = output.split("\n");
let currentVersion;
let latestStableVersion;
for (const line of lines) {
// Look for current version line: "2.100.3 (current)"
if (line.includes("(current)")) {
const match = line.match(/│\s*([^\s]+)\s+\(current\)/);
if (match) {
currentVersion = match[1];
}
}
// Look for latest stable version line: "│ 2.100.4 │ stable │"
if (line.includes("│ stable │") && !line.includes("(current)")) {
const match = line.match(/│\s*([^\s]+)\s*│\s*stable\s*│/);
if (match) {
latestStableVersion = match[1];
break; // Take the first stable version we find
}
}
}
if (!currentVersion) {
return {
hasUpdate: false,
message: "Could not determine current SF CLI version",
};
}
if (!latestStableVersion) {
return {
hasUpdate: false,
currentVersion,
message: "Could not determine latest stable SF CLI version",
};
}
const hasUpdate = currentVersion !== latestStableVersion;
return {
hasUpdate,
currentVersion,
latestVersion: latestStableVersion,
message: hasUpdate
? `SF CLI update available: ${currentVersion} → ${latestStableVersion}. Run 'sf_update' tool to update.`
: undefined,
};
}
export async function checkForSfUpdates() {
const now = Date.now();
// Return cached result if it's still valid
if (cachedResult && now - lastCheckTime < CACHE_DURATION) {
return cachedResult;
}
return new Promise((resolve) => {
const command = "sf update --available";
exec(command, { maxBuffer: 10 * 1024 * 1024, timeout: 30000 }, (error, stdout, stderr) => {
if (error) {
// If the command fails, assume no update check is possible
const result = {
hasUpdate: false,
message: "Could not check for SF CLI updates",
};
cachedResult = result;
lastCheckTime = now;
resolve(result);
return;
}
try {
const result = parseUpdateOutput(stdout);
cachedResult = result;
lastCheckTime = now;
resolve(result);
}
catch (parseError) {
const result = {
hasUpdate: false,
message: "Could not parse SF CLI update information",
};
cachedResult = result;
lastCheckTime = now;
resolve(result);
}
});
});
}