Skip to content

Commit

Permalink
redirect exec calls from renderer to main process
Browse files Browse the repository at this point in the history
  • Loading branch information
LabhanshAgrawal committed Jul 29, 2021
1 parent 146d89a commit 50cd4bb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import {app, dialog, BrowserWindow, App} from 'electron';
import {app, dialog, BrowserWindow, App, ipcMain} from 'electron';
import {resolve, basename} from 'path';
import {writeFileSync} from 'fs';
import Config from 'electron-store';
Expand All @@ -15,6 +15,8 @@ import {install} from './plugins/install';
import {plugs} from './config/paths';
import mapKeys from './utils/map-keys';
import {configOptions} from '../lib/config';
import {promisify} from 'util';
import {exec} from 'child_process';

// local storage
const cache = new Config();
Expand Down Expand Up @@ -449,3 +451,8 @@ export const decorateSessionClass = <T>(Session: T): T => {
};

export {toDependencies as _toDependencies};

ipcMain.handle('child_process.exec', (event, args) => {
const {command, options} = args;
return promisify(exec)(command, options);
});
42 changes: 42 additions & 0 deletions lib/utils/child_process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {ipcRenderer} from 'electron';

export function exec(command: string, options?: any, callback?: Function): void {
if (typeof options === 'function') {
callback = options;
options = {};
}
ipcRenderer.invoke('child_process.exec', {command, options}).then(
({stdout, stderr}) => {
// console.log(`stdout: ${stdout}`);
if (callback) {
callback(null, stdout, stderr);
}
},
(error) => {
if (callback) {
// console.log(error);
callback(error, '', '');
}
}
);
}

export function execSync(command: string, options?: any): any {
return;
}

// function spawn(command: string, options?: any): any {
// return;
// }

// function spawnSync(command: string, options?: any): any {
// return;
// }

// function fork(command: string, options?: any) {
// return;
// }

// function forkSync(command: string, options?: any): any {
// return;
// }
4 changes: 4 additions & 0 deletions lib/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
} from '../hyper';
import {Middleware} from 'redux';
import {ObjectTypedKeys} from './object';
import * as IPCChildProcess from './child_process';
import ChildProcess from 'child_process';

// remote interface to `../plugins`
const plugins = remote.require('./plugins') as typeof import('../../app/plugins');
Expand Down Expand Up @@ -182,6 +184,8 @@ Module._load = function _load(path: string) {
return Notification;
case 'hyper/decorate':
return decorate;
case 'child_process':
return process.platform === 'darwin' ? IPCChildProcess : ChildProcess;
default:
// eslint-disable-next-line prefer-rest-params
return originalLoad.apply(this, arguments);
Expand Down

0 comments on commit 50cd4bb

Please sign in to comment.