Skip to content

Instantly share code, notes, and snippets.

@stelcheck
Last active April 18, 2021 03:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stelcheck/a0c798ff31df5f2c0e5d72513948dcf1 to your computer and use it in GitHub Desktop.
Save stelcheck/a0c798ff31df5f2c0e5d72513948dcf1 to your computer and use it in GitHub Desktop.
REPL example for electron
'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()
'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