UNPKG

2.63 kBJavaScriptView Raw
1'use strict'
2
3const App = require('app')
4const Path = require('path')
5const Electron = require('electron')
6const STDIN = []
7let window
8let request = process.argv[3]
9
10let name = 'de-node'
11let options = {
12 hide: true,
13 width: 800,
14 height: 800
15}
16let manifestInfo
17try {
18 manifestInfo = require(Path.join(process.cwd(), 'package.json'))
19} catch (_) { }
20if (manifestInfo && process.argv.indexOf('--ignore-local') === -1) {
21 if (typeof manifestInfo.name === 'string' && manifestInfo.name) {
22 name = manifestInfo.name.toLowerCase()
23 }
24 if (typeof manifestInfo.electronOptions === 'object' && manifestInfo.electronOptions) {
25 Object.assign(options, manifestInfo.electronOptions)
26 }
27}
28if (manifestInfo && (request === './' || request === '.' || request === '.\\') && typeof manifestInfo.electronMain === 'string' && manifestInfo.electronMain) {
29 request = manifestInfo.electronMain
30}
31
32if (process.env.hasOwnProperty('DENODE_INSECURE') || (manifestInfo && manifestInfo.denodeOptions.indexOf('DENODE_INSECURE') !== -1)) {
33 App.commandLine.appendSwitch('--ignore-certificate-errors')
34}
35
36Electron.app.on('ready', function() {
37 Electron.protocol.interceptFileProtocol('file', function(request, callback) {
38 if (request.url === `file:///app-${name}`) {
39 callback(Path.join(__dirname, 'index.html'))
40 } else {
41 callback(request.url)
42 }
43 })
44
45 window = new Electron.BrowserWindow(options)
46 window.loadURL(`file:///app-${name}`)
47 window.on('closed', function() {
48 Electron.app.quit()
49 })
50 window.webContents.openDevTools({ detach: options.hide })
51 if (options.hide) {
52 window.webContents.once('devtools-opened', function() {
53 setImmediate(function() {
54 window.hide()
55 window.webContents.once('devtools-closed', function() {
56 setImmediate(function() {
57 window.close()
58 })
59 })
60 })
61 })
62 }
63
64 window.webContents.on('dom-ready', function() {
65 window.webContents.send('setup', JSON.stringify({
66 argv: process.argv.slice(2),
67 request: request,
68 stdoutIsTTY: process.stdout.isTTY,
69 stderrIsTTY: process.stderr.isTTY
70 }))
71
72 for (let i = 0, length = STDIN.length; i < length; ++i) {
73 window.webContents.send('stdin', STDIN[i])
74 }
75 })
76})
77
78Electron.ipcMain.on('stdout', function(_, data) {
79 process.stdout.write(data)
80})
81Electron.ipcMain.on('stderr', function(_, data) {
82 process.stderr.write(data)
83})
84process.stdin.on('data', function(chunk) {
85 const stringish = chunk.toString()
86 if (window) {
87 window.webContents.send('stdin', stringish)
88 }
89 STDIN.push(stringish)
90})
91process.stdin.resume()