forked from Experience-Monks/devtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
168 lines (142 loc) · 4.88 KB
/
server.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
var path = require('path');
var fs = require('fs');
var createWatch = require('./lib/file-watch');
var createMainWindow = require('./lib/main-window');
var parseArgs = require('./lib/parse-args');
var mime = require('mime');
var electron = require('electron');
electron.protocol.registerSchemesAsPrivileged([
{ scheme: 'file', privileges: { standard: true, secure: true, bypassCSP: true } },
]);
// electron.protocol.registerSchemesAsPrivileged([
// { scheme: 'file', privileges: { standard: true, secure: true, bypassCSP: true } },
// ]);
var app = electron.app;
var ipc = electron.ipcMain;
var globals;
var exitWithCode1 = false;
process.removeAllListeners('uncaughtException');
process.stdin.pause();
var argv = parseArgs(process.argv.slice(2));
globals = argv.globals;
app.commandLine.appendSwitch('disable-http-cache');
if (!argv.verbose) {
app.commandLine.appendSwitch('v', '-1');
app.commandLine.appendSwitch('vmodule', 'console=0');
}
if (argv.version || argv.v) {
console.log('devtool ' + require('./package.json').version);
console.log('electron ' + process.versions.electron);
console.log('node ' + process.versions.node);
console.log('chrome ' + process.versions.chrome);
process.exit(0);
}
// inject V8 flags
if (argv.config && argv.config.v8) {
var flags = [].concat(argv.config.v8.flags).filter(Boolean);
flags.forEach(function (flag) {
app.commandLine.appendSwitch('js-flags', flag);
});
}
// determine absolute path to entry file
var cwd = process.cwd();
var entryFile = argv._[0];
if (entryFile) {
entryFile = path.isAbsolute(entryFile) ? entryFile : path.resolve(cwd, entryFile);
try {
entryFile = require.resolve(entryFile);
globals.entry = entryFile; // setup entry for preload
} catch (err) {
console.error(err.stack ? err.stack : err);
process.exit(1);
}
}
var watcher = null;
var mainWindow = null;
app.on('window-all-closed', function () {
app.quit();
});
// Quit the server with the correct exit code
app.on('quit', function () {
if (watcher) watcher.close();
if (exitWithCode1) process.exit(1);
});
app.on('ready', function () {
// Get starting HTML file
var htmlFile = path.resolve(__dirname, 'lib', 'index.html');
var customHtml = false; // if we should watch it as well
if (argv.index) {
customHtml = true;
htmlFile = path.isAbsolute(argv.index) ? argv.index : path.resolve(cwd, argv.index);
}
var mainIndexURL = 'file://' + __dirname + '/index.html';
// Replace index.html with custom one
electron.protocol.interceptBufferProtocol(
'file',
function (request, callback) {
// We can't just spin up a local server for this, see here:
// https://github.com/atom/electron/issues/2414
var file = request.url;
if (file === mainIndexURL) {
file = htmlFile;
} else if (file.indexOf('file://') === 0) {
// All other assets should be relative to the user's cwd
file = file.substring(7);
file = path.resolve(cwd, path.relative(__dirname, file));
}
fs.readFile(file, function (err, data) {
// Could convert Node error codes to Chromium for better reporting
if (err) return callback(-6);
callback({
data: data,
mimeType: mime.getType(file),
});
});
},
function (err) {
if (err) fatal(err);
}
);
// Setup the BrowserWindow
mainWindow = createMainWindow(entryFile, mainIndexURL, argv, function () {
// When we first launch, ensure the quit flag is set to the user args
globals.quit = argv.quit;
// Focus the devtools.
app.focus();
});
// De-reference for GC
mainWindow.on('closed', function () {
mainWindow = null;
});
// Setup the file watcher
if (argv.watch) {
var globs = [].concat(argv.watch).filter(function (f) {
return typeof f === 'string';
});
if (globs.length === 0) globs = ['**/*.{js,json}'];
if (customHtml && globs.indexOf(htmlFile) === -1) {
// also watch the specified --index HTML file
globs.push(htmlFile);
}
watcher = createWatch(globs, argv);
watcher.on('change', function (file) {
if (mainWindow) mainWindow.reload();
});
}
// Fatal error in renderer
ipc.on('error', function (event, errObj) {
var err = JSON.parse(errObj);
bail(err.stack);
});
function bail(err) {
console.error(err.stack ? err.stack : err);
if (globals.quit) {
exitWithCode1 = true;
if (mainWindow) mainWindow.close();
}
}
function fatal(err) {
globals.quit = true;
bail(err);
}
});