-
Notifications
You must be signed in to change notification settings - Fork 3
/
dev.coffee
90 lines (74 loc) · 2.5 KB
/
dev.coffee
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
window.CRASHED = false
if process?
nwgui = window.require "nw.gui"
nwwin = nwgui.Window.get window
# Get rid of the default error handler
process.removeAllListeners "uncaughtException"
# Add our own handler
process.on "uncaughtException", (e)->
unless window.CRASHED
window.CRASHED = true
console?.warn? "CRASH"
nwwin.showDevTools()
nwwin.show() if nwgui.App.manifest.window?.show is false
# Keyboard shortcuts
window.addEventListener "keydown", (e)->
switch e.key ? e.keyIdentifier
when "F12"
nwwin.showDevTools()
when "F5"
window.location.reload()
ignored = [/node_modules|npm-debug\.log|\.git|\.hg|\.svn/]
ignore = document.currentScript.dataset.ignore
ignored.push ignore if ignore
# Live reload
try
chokidar = window.require "nw-dev/node_modules/chokidar/"
catch err1
try
chokidar = window.require "chokidar"
catch err2
console.warn "Live reload disabled:", [err1.stack, err2.stack]
if chokidar
watcher = chokidar.watch ".", {ignored}
# @TODO: watch linked dependencies and bundled dependencies
# and make a package called watch-package
watcher.on "change", (path)->
watcher.close()
# this could theoretically cause issues in very obscure cases
# where caching behavior is relied upon in a separate page/context
for own k of global.require.cache
delete global.require.cache[k]
# very obscure cases
# overall, it's very helpful
# this could cause issues in somewhat less obscure cases
# where another page/context has listeners on this window
nwwin.removeAllListeners()
# but it fixes event listeners being leaked between reloads
# which cause errors that wouldn't occur without reloading
nwwin.closeDevTools()
if path is "package.json"
# we want to do a full reload
fs = require "fs"
pkg = JSON.parse fs.readFileSync "package.json", "utf8"
# override the getter by redefining the property
Object.defineProperty nwgui.App,
"manifest"
value: pkg
configurable: yes # (so this will work a second time)
{x, y} = nwwin
width = window.innerWidth
height = window.innerHeight
window.close()
newwin_options = {}
newwin_options[k] = v for k, v of pkg.window
newwin_options[k] = v for k, v of {x, y, width, height}
newwin = nwgui.Window.open window.location.href, newwin_options
else
location?.reload()
window.onerror = (e)->
unless window.CRASHED
window.CRASHED = true
console?.warn? "CRASH"
nwwin?.showDevTools()
return false