UNPKG

@kui-shell/core

Version:

An Electron-based shell for cloud-native development

129 lines (128 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tellMain = exports.init = void 0; var _debug = _interopRequireDefault(require("debug")); var _tab = require("./tab"); var _capabilities = require("../core/capabilities"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* * Copyright 2017 The Kubernetes Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const debug = (0, _debug.default)('webapp/electron-events'); debug('loading'); /** * Listen for the main process telling us to execute a command * */ const listenForRemoteEvents = ipcRenderer => { debug('listenForRemoteEvents'); if ((0, _capabilities.inElectron)() && ipcRenderer) { ipcRenderer.on('/repl/pexec', (event, { command }) => __awaiter(void 0, void 0, void 0, function* () { debug('remote pexec', command); const { pexec } = yield Promise.resolve().then(() => require('../repl/exec')); return pexec(command); })); ipcRenderer.on('/repl/qexec', (event, { command }) => __awaiter(void 0, void 0, void 0, function* () { debug('remote qexec', command); return (0, _tab.pexecInCurrentTab)(command, undefined, false, true); })); } }; /** * Set up the IPC channel to the main process * */ const initializeIPC = () => __awaiter(void 0, void 0, void 0, function* () { debug('initializeIPC'); const electron = yield Promise.resolve().then(() => require('electron')); const ipcRenderer = electron.ipcRenderer; (0, _capabilities.setMedia)(_capabilities.Media.Electron); return { ipcRenderer }; }); /** * Send a synchronous message to the main process * */ const tellMain = (message, // eslint-disable-line @typescript-eslint/no-explicit-any channel) => // eslint-disable-next-line no-async-promise-executor new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () { const electron = yield Promise.resolve().then(() => require('electron')); const ipcRenderer = electron.ipcRenderer; ipcRenderer[channel === 'asynchronous-message' ? 'send' : 'sendSync'](channel || 'synchronous-message', typeof message === 'string' ? JSON.stringify({ operation: message }) : JSON.stringify(message)); if (channel === 'asynchronous-message') { console.log('listening'); ipcRenderer.on('asynchronous-reply', (event, response) => { console.log('got response', response); if (response === 'true') { resolve(true); } else { reject(response); } }); } else { resolve(true); } })); // eslint-disable-next-line @typescript-eslint/no-unused-vars exports.tellMain = tellMain; const init = (prefs = {}) => { return initializeIPC().then(({ ipcRenderer }) => { listenForRemoteEvents(ipcRenderer); }); }; exports.init = init;