1 | 'use strict'
|
2 |
|
3 | const App = require('app')
|
4 | const Path = require('path')
|
5 | const Electron = require('electron')
|
6 | const STDIN = []
|
7 | let window
|
8 | let request = process.argv[3]
|
9 |
|
10 | let name = 'de-node'
|
11 | let options = {
|
12 | hide: true,
|
13 | width: 800,
|
14 | height: 800
|
15 | }
|
16 | let manifestInfo
|
17 | try {
|
18 | manifestInfo = require(Path.join(process.cwd(), 'package.json'))
|
19 | } catch (_) { }
|
20 | if (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 | }
|
28 | if (manifestInfo && (request === './' || request === '.' || request === '.\\') && typeof manifestInfo.electronMain === 'string' && manifestInfo.electronMain) {
|
29 | request = manifestInfo.electronMain
|
30 | }
|
31 |
|
32 | if (process.env.hasOwnProperty('DENODE_INSECURE') || (manifestInfo && manifestInfo.denodeOptions.indexOf('DENODE_INSECURE') !== -1)) {
|
33 | App.commandLine.appendSwitch('--ignore-certificate-errors')
|
34 | }
|
35 |
|
36 | Electron.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 |
|
78 | Electron.ipcMain.on('stdout', function(_, data) {
|
79 | process.stdout.write(data)
|
80 | })
|
81 | Electron.ipcMain.on('stderr', function(_, data) {
|
82 | process.stderr.write(data)
|
83 | })
|
84 | process.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 | })
|
91 | process.stdin.resume()
|