forked from OakLabsInc/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·185 lines (169 loc) · 3.93 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
#!/usr/bin/env node
const { resolve, join, basename } = require('path')
const Module = require('module')
const _ = require('lodash')
require(join(__dirname, 'lib', 'util'))
const program = require('commander')
const {
version,
dependencies: {
electron: electronVersion
}
} = require(join(__dirname, 'package.json'))
// oak gets loaded from this path
const oakPath = join(__dirname, 'lib', 'index.js')
// resolve future require('oak') to our core path
const origResolve = Module._resolveFilename
Module._resolveFilename = function (request) {
return request === 'oak' ? oakPath : origResolve(...[...arguments])
}
const oak = require('oak')
// Attaches oak property to |exports|
exports.defineProperties = function (exports) {
return Object.defineProperties(exports, {
oak: {
enumerable: false,
get: () => require('oak')
}
})
}
let opts = {}
program
.version(version)
.description('If you load oak with a script path, no commandline options will apply automatically.')
.option(
'-b, --background [String]',
'Hex background color for initial window. Example: #f0f0f0',
'#000000'
)
.option(
'-f, --fullscreen [Boolean]',
'Set the window to full width and height',
_.toBoolean, true
)
.option(
'-h, --height [Number]',
'Window height',
_.toInteger, 768
)
.option(
'-w, --width [Number]',
'Window width',
_.toInteger, 1024
)
.option(
'-x, --x [Number]',
'Window X position',
_.toInteger, 0
)
.option(
'-y, --y [Number]',
'Window Y position',
_.toInteger, 0
)
.option(
'-t, --title [String]',
'Window title',
_.toString, 'Oak'
)
.option(
'-t, --ontop [Boolean]',
'Start window ontop of others',
_.toBoolean, true
)
.option(
'-k, --kiosk [Boolean]',
'Kiosk mode',
_.toBoolean, true
)
.option(
'-D, --display [Number]',
'Display to use',
_.toInteger, 0
)
.option(
'-S, --shortcut [List]',
'Register shortcuts, comma separated. reload,quit',
v => v.split(','), []
)
.option(
'-u, --useragent [String]',
'User-Agent string',
_.toString
)
.option(
'-F, --frame [Boolean]',
'Show window frame',
_.toBoolean, false
)
.option(
'-s, --show [Boolean]',
'Show window on start',
_.toBoolean, true
)
.option(
'-n, --node [Boolean]',
'Enable node integration',
_.toBoolean, false
)
.option(
'-i, --insecure [Boolean]',
'Allow insecure connections (not recommended)',
_.toBoolean, false
)
.option(
'-c, --cache [Boolean]',
'Use HTTP cache',
_.toBoolean, true
)
.option(
'-d, --debugger [Boolean]',
'Open chrome dev tools on load',
_.toBoolean, false
)
.option(
'--sslExceptions [Array]',
'Bypass SSL security for specific hosts. This uses a host pattern. Example: *.mysite.com',
v => v.split(','), []
)
.option(
'--electron-version',
'Print electron version'
)
.arguments('<url>')
.action(function (url, options) {
if (_.has(options, 'electronVersion')) {
console.log(electronVersion)
process.exit(0)
}
if (url) {
opts.url = url
}
})
// setting temp argv for commander to use if we are inside our compiled app
let tmpArgv = process.argv
if (_.get(process.env, 'CHROME_DESKTOP') === 'oak.desktop' || basename(process.argv[0]).indexOf('oak') !== -1) {
tmpArgv = ['', ...process.argv]
}
program.parse(tmpArgv)
opts = _(program._events)
.omit('*', 'version', 'electronVersion')
.mapValues((v, k) => program[k])
.omitBy(_.isUndefined)
.merge(opts)
.value()
if (!opts.url) {
program.help()
}
if (require('url').parse(opts.url).protocol !== null) {
// if the url argument is an actual URI, just load it
oak.on('ready', () => oak.load(opts))
} else {
// if not, require it as a file
try {
require(resolve(opts.url))
} catch (e) {
console.error('Not a valid URL or file path.')
process.exit(1)
}
}