This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
index.js
135 lines (121 loc) · 3.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict'
const { app, BrowserWindow, shell, Menu, ipcMain } = require('electron')
const { neutral } = require('dat-colors')
const autoUpdater = require('./lib/auto-updater')
const defaultMenu = require('electron-default-menu')
const doctor = require('dat-doctor')
const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
const isTest = process.env.NODE_ENV === 'test'
const { Writable } = require('stream')
if (typeof process.env.NODE_V === 'string' && process.env.NODE_V !== process.version) {
console.error(`
WARNING:
You are using a different version of Node than is used in this electron release!
- Used Version: ${process.env.NODE_V}
- Electron's Node Version: ${process.version}
We recommend running:
$ nvm install ${process.version}; npm rebuild;
`)
}
const menu = defaultMenu(app, shell)
menu[menu.length - 1].submenu.push({
label: 'Doctor',
click: () => {
win.webContents.openDevTools({ mode: 'detach' })
const out = Writable({
write (chunk, env, done) {
if (win) win.webContents.send('log', chunk.toString())
done()
}
})
doctor({ out })
}
})
let win
let watchProcess
app.on('ready', () => {
if (isDev) {
BrowserWindow.addDevToolsExtension(path.join(__dirname, 'dev', 'react-dev-tools'))
watchAndReload()
}
win = new BrowserWindow({
// Extending the size of the browserwindow to make sure that the developer bar is visible.
width: 800 + (isDev ? 50 : 0),
height: 600 + (isDev ? 200 : 0),
titleBarStyle: 'hiddenInset',
minWidth: 640,
minHeight: 395,
backgroundColor: neutral,
icon: path.resolve(`${__dirname}/build/icon.png`),
// Spectron doesn't work with "preload" enabled, loading is handled in index.html
webPreferences: (!isTest
? {
nodeIntegration: false,
preload: `${__dirname}/preload.js`
}
: {
nodeIntegration: true
}
)
})
win.loadURL(`file://${__dirname}/index.html#${process.env.NODE_ENV}`)
Menu.setApplicationMenu(Menu.buildFromTemplate(menu))
ipcMain.on('progress', (_, progress) => win && win.setProgressBar(progress))
if (isDev) {
win.webContents.openDevTools()
} else {
const log = str => win && win.webContents.send('log', str)
autoUpdater({ log })
}
})
app.on('will-finish-launching', () => {
app.on('open-url', (_, url) => win.webContents.send('link', url))
app.on('open-file', (_, path) => win.webContents.send('file', path))
})
app.on('window-all-closed', () => {
if (watchProcess) {
watchProcess.close()
watchProcess = null
}
app.quit()
})
// const quit = app.makeSingleInstance(() => {
// if (!win) return
// if (win.isMinimized()) win.restore()
// win.focus()
// })
//
// if (quit) app.quit()
function watchAndReload () {
let gaze
let first = true
try {
gaze = require('gaze')
} catch (e) {
console.warn('Gaze is not installed, wont be able to reload the app')
// In case dev dependencies are not installed
return
}
gaze([
`preload.js`,
`static/**/*`
], {
debounceDelay: 60,
cwd: __dirname
}, (err, process) => {
if (err) {
console.warn('Gaze doesnt run well, wont be able to reload the app')
console.warn(err)
return
}
watchProcess = process
watchProcess.on('all', () => {
if (first) {
first = false
return
}
win && win.reload()
})
})
}