-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
201 lines (174 loc) · 5.13 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var assign = require('object-assign')
var path = require('path')
var serializerr = require('serializerr')
var getPort = require('getport')
var parseArgs = require('./lib/parse-args')
var defaults = require('lodash.defaults')
var createHihat = require('./lib/hihat')
module.exports = hihat
module.exports.fromArgs = fromArgs
function fromArgs (args, opt) {
var cliOpts = parseArgs(args)
return hihat(assign(cliOpts, opt))
}
function hihat (opts) {
// require these at runtime
var app = require('app')
app.commandLine.appendSwitch('disable-http-cache')
app.commandLine.appendSwitch('v', 0)
app.commandLine.appendSwitch('vmodule', 'console=0')
var BrowserWindow = require('browser-window')
opts = assign({}, opts)
// ensure defaults like devtool / electron-builtins are set
defaults(opts, parseArgs.defaults)
var entries = opts.entries || []
if (typeof entries === 'string')
entries = [ entries ]
if (opts.exec) {
opts.devtool = false
opts.quit = true
opts.print = true
}
var basedir = opts.basedir || process.cwd()
Error.stackTraceLimit = Infinity
// this will allow the Chrome Console to also
// find modules within the current working directory
if (opts.node) {
var findNodeModules = require('find-node-modules')
process.env.NODE_PATH = findNodeModules({
cwd: basedir,
relative: false
}).join(path.delimiter)
}
var hihat
var mainWindow = null, lastError = null
app.on('window-all-closed', close)
process.on('uncaughtException', function (err) {
process.stderr.write((err.stack ? err.stack : err) + '\n')
if (opts.quit) {
close()
} else {
lastError = err
printLastError()
}
})
function close () {
app.quit()
if (hihat) {
hihat.close()
}
}
app.on('ready', function () {
var basePort = opts.port || 9541
getPort(basePort, function (err, port) {
if (err) {
console.error('Could not get available port')
process.exit(1)
}
var unparsedArgs = opts.browserifyArgs
start(assign({}, opts, {
entries: entries,
browserifyArgs: entries.concat(unparsedArgs),
port: port,
host: opts.host || 'localhost',
dir: opts.dir || process.cwd()
}))
})
})
function start (opt) {
hihat = createHihat(opt)
.on('connect', function (ev) {
var bounds = parseBounds(opts.frame)
// a hidden browser window
mainWindow = new BrowserWindow(assign({
'node-integration': opts.node,
'use-content-size': true
}, bounds, {
preload: getPrelude(),
icon: path.join(__dirname, 'img', 'logo-thumb.png')
}))
var webContents = mainWindow.webContents
webContents.once('did-start-loading', function () {
if (opts.devtool !== false) {
mainWindow.openDevTools({ detach: true })
}
})
webContents.once('did-frame-finish-load', function () {
mainWindow.loadUrl(ev.uri)
mainWindow.once('dom-ready', function () {
printLastError()
})
if (typeof opts.timeout === 'number') {
setTimeout(function () {
close()
}, opts.timeout)
}
})
mainWindow.show()
// REPL with no browserify entries
if (entries.length === 0) {
mainWindow.reload()
}
mainWindow.once('closed', function () {
mainWindow = null
hihat.close()
})
})
.on('update', function () {
if (mainWindow) {
mainWindow.reload()
}
})
}
function parseBounds (frame) {
var bounds = { width: 0, height: 0, x: 0, y: 0 }
if (typeof frame === 'string') {
var parts = frame.split(',').map(function (x) {
return parseInt(x, 10)
})
if (parts.length === 2) {
bounds = { width: parts[0], height: parts[1] }
} else if (parts.length === 4) {
bounds.x = parts[0]
bounds.y = parts[1]
bounds.width = parts[2]
bounds.height = parts[3]
} else {
throw new Error('must specify 2 or 4 values for --frame')
}
} else if (frame) {
bounds = {} // let Electron choose default size
}
return bounds
}
function printLastError () {
if (!mainWindow || !lastError) return
var err = serializerr(lastError)
mainWindow.webContents.executeJavaScript([
'(function() {',
// simulate server-side Error object
'var errObj = ' + JSON.stringify(err),
'var err = new Error()',
'mixin(err, errObj)',
'try {throw err} catch(e) {console.error(e)}',
'function mixin(a, b) { for (var key in b) a[key] = b[key] }',
'})()'
].join('\n'))
lastError = null
}
function getPrelude () {
var name
if (opts.node && opts.print) {
name = 'node-console.js'
} else if (opts.node) {
name = 'node.js'
} else if (opts.print) {
name = 'console.js'
}
if (name) {
return path.resolve(__dirname, 'lib', 'prelude', name)
} else {
return undefined
}
}
}