UNPKG

sfdx-hardis

Version:

Swiss-army-knife Toolbox for Salesforce. Allows you to define a complete CD/CD Pipeline. Orchestrate base commands and assist users with interactive wizards

108 lines 3.62 kB
import c from 'chalk'; import * as util from 'util'; import WebSocket from 'ws'; import { isCI, uxLog } from './utils/index.js'; import { SfError } from '@salesforce/core'; let globalWs; let isWsOpen = false; const PORT = process.env.SFDX_HARDIS_WEBSOCKET_PORT || 2702; export class WebSocketClient { ws; wsContext; promptResponse; constructor(context) { this.wsContext = context; const wsHostPort = context.websocketHostPort ? `ws://${context.websocketHostPort}` : `ws://localhost:${PORT}`; try { this.ws = new WebSocket(wsHostPort); globalWs = this; // eslint-disable-line this.start(); } catch (err) { uxLog(this, c.yellow('Warning: Unable to start WebSocket client on ' + wsHostPort + '\n' + err.message)); } } static isAlive() { return !isCI && globalWs != null && isWsOpen === true; } static sendMessage(data) { if (globalWs) { globalWs.sendMessageToServer(data); } } // Requests open file within VsCode if linked static requestOpenFile(file) { WebSocketClient.sendMessage({ event: 'openFile', file: file.replace(/\\/g, '/') }); } static sendPrompts(prompts) { if (globalWs) { return globalWs.promptServer(prompts); } throw new SfError('globalWs should be set in sendPrompts'); } start() { this.ws.on('open', () => { isWsOpen = true; this.ws.send(JSON.stringify({ event: 'initClient', context: this.wsContext, })); // uxLog(this,c.grey('Initialized WebSocket connection with VsCode SFDX Hardis')); }); this.ws.on('message', (data) => { this.receiveMessage(JSON.parse(data)); }); this.ws.on('error', (err) => { this.ws.terminate(); globalWs = null; if (process.env.DEBUG) { console.error(err); } }); } receiveMessage(data) { if (process.env.DEBUG) { console.debug('websocket: received: %s', util.inspect(data)); } if (data.event === 'promptsResponse') { this.promptResponse = data.promptsResponse; } } sendMessageToServer(data) { data.context = this.wsContext; this.ws.send(JSON.stringify(data)); } promptServer(prompts) { this.sendMessageToServer({ event: 'prompts', prompts: prompts }); this.promptResponse = null; let ok = false; return new Promise((resolve, reject) => { let interval = null; let timeout = null; interval = setInterval(() => { if (this.promptResponse != null) { clearInterval(interval); clearTimeout(timeout); ok = true; resolve(this.promptResponse); } }, 300); timeout = setTimeout(() => { if (ok === false) { clearInterval(interval); reject('[sfdx-hardis] No response from UI WebSocket Server'); } }, 7200000); // 2h timeout }); } dispose() { this.ws.send(JSON.stringify({ event: 'closeClient', context: this.wsContext, })); this.ws.terminate(); globalWs = null; // uxLog(this,c.grey('Closed WebSocket connection with VsCode SFDX Hardis')); } } //# sourceMappingURL=websocketClient.js.map