-
-
Save rquast/5e42b7c3cab04737eae8b79eb02c748f to your computer and use it in GitHub Desktop.
REPL example for electron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const net = require('net') | |
const chalk = require('chalk') | |
const path = require('path') | |
const cp = require('child_process') | |
const rimraf = require('rimraf') | |
function getPath() { | |
if (process.platform === 'win32') { | |
return path.join('\\\\.\\pipe', process.cwd(), 'pipe') | |
} | |
let ipcPath = path.join(process.cwd(), '.pipe') | |
rimraf.sync(ipcPath) | |
return ipcPath | |
} | |
const ipcPath = getPath() | |
let command = 'node_modules/.bin/electron' | |
let args = ['.', '--color'] | |
if (process.platform === 'win32') { | |
command = 'cmd' | |
args = ['/s', '/c', 'node_modules\\.bin\\electron.cmd . --color'] | |
} | |
let subprocess = cp.spawn(command, args, { | |
stdio: ['ignore', process.stdout, process.stderr], | |
}) | |
subprocess.on('exit', function () { | |
process.exit() | |
}) | |
function wait() { | |
setTimeout(function () { | |
let socket = net.connect(ipcPath, function () { | |
console.log(chalk.green('REPL terminal enabled, type "help" for more details')) | |
let stdin = process.stdin | |
stdin.setRawMode(true) | |
stdin.setEncoding('utf8') | |
stdin.resume() | |
stdin.pipe(socket) | |
socket.pipe(process.stdout) | |
}).on('error', function () { | |
console.error(chalk.yellow('Application is not started yet, retrying to connect')) | |
wait() | |
}) | |
}, 1000) | |
} | |
wait() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const electron = require('electron') | |
const app = electron.app | |
const chalk = require('chalk') | |
const path = require('path') | |
const net = require('net') | |
const stream = require('stream') | |
let ipcPath = path.join(process.cwd(), '..', '.pipe') | |
if (process.platform === 'win32') { | |
ipcPath = path.join('\\\\.\\pipe', process.cwd(), '..', 'pipe') | |
} | |
net.createServer(function (socket) { | |
let repl = require('repl').start({ | |
input: socket, | |
output: socket, | |
useColors: true, | |
terminal: true, | |
prompt: chalk.magenta('[') | |
+ chalk.gray('electron-develop') | |
+ chalk.magenta(']') | |
+ chalk.blue('>> ') | |
}) | |
/** | |
* Exit when the REPL is closed | |
*/ | |
repl.on('exit', function () { | |
app.exit(0) | |
}) | |
/** | |
* Variables to expose to the REPL interface | |
*/ | |
repl.context.electron = electron | |
repl.context.app = app | |
}).listen(ipcPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment