UNPKG

4.29 kBJavaScriptView Raw
1var path = require('path');
2var fs = require('fs');
3var createWatch = require('./lib/file-watch');
4var createMainWindow = require('./lib/main-window');
5var parseArgs = require('./lib/parse-args');
6var mime = require('mime');
7
8var electron = require('electron');
9var app = electron.app;
10var ipc = electron.ipcMain;
11var globals;
12
13var exitWithCode1 = false;
14process.removeAllListeners('uncaughtException');
15process.stdin.pause();
16
17var argv = parseArgs(process.argv.slice(2));
18globals = argv.globals;
19
20app.commandLine.appendSwitch('disable-http-cache');
21if (!argv.verbose) {
22 app.commandLine.appendSwitch('v', '-1');
23 app.commandLine.appendSwitch('vmodule', 'console=0');
24}
25
26if (argv.version || argv.v) {
27 console.log('devtool ' + require('./package.json').version);
28 console.log('electron ' + process.versions.electron);
29 console.log('node ' + process.versions.node);
30 console.log('chrome ' + process.versions.chrome);
31 process.exit(0);
32}
33
34// inject V8 flags
35if (argv.config && argv.config.v8) {
36 var flags = []
37 .concat(argv.config.v8.flags)
38 .filter(Boolean);
39 flags.forEach(function (flag) {
40 app.commandLine.appendSwitch('js-flags', flag);
41 });
42}
43
44// determine absolute path to entry file
45var cwd = process.cwd();
46var entryFile = argv._[0];
47if (entryFile) {
48 entryFile = path.isAbsolute(entryFile) ? entryFile : path.resolve(cwd, entryFile);
49 try {
50 entryFile = require.resolve(entryFile);
51 globals.entry = entryFile; // setup entry for preload
52 } catch (err) {
53 console.error(err.stack ? err.stack : err);
54 process.exit(1);
55 }
56}
57
58var watcher = null;
59var mainWindow = null;
60app.on('window-all-closed', function () {
61 app.quit();
62});
63
64// Quit the server with the correct exit code
65app.on('quit', function () {
66 if (watcher) watcher.close();
67 if (exitWithCode1) process.exit(1);
68});
69
70app.on('ready', function () {
71 electron.protocol.registerServiceWorkerSchemes(['file:']);
72
73 // Get starting HTML file
74 var htmlFile = path.resolve(__dirname, 'lib', 'index.html');
75 var customHtml = false; // if we should watch it as well
76 if (argv.index) {
77 customHtml = true;
78 htmlFile = path.isAbsolute(argv.index) ? argv.index : path.resolve(cwd, argv.index);
79 }
80
81 var mainIndexURL = 'file://' + __dirname + '/index.html';
82
83 // Replace index.html with custom one
84 electron.protocol.interceptBufferProtocol('file', function (request, callback) {
85 // We can't just spin up a local server for this, see here:
86 // https://github.com/atom/electron/issues/2414
87
88 var file = request.url;
89 if (file === mainIndexURL) {
90 file = htmlFile;
91 } else if (file.indexOf('file://') === 0) {
92 // All other assets should be relative to the user's cwd
93 file = file.substring(7);
94 file = path.resolve(cwd, path.relative(__dirname, file));
95 }
96
97 fs.readFile(file, function (err, data) {
98 // Could convert Node error codes to Chromium for better reporting
99 if (err) return callback(-6);
100 callback({
101 data: data,
102 mimeType: mime.lookup(file)
103 });
104 });
105 }, function (err) {
106 if (err) fatal(err);
107 });
108
109 // Setup the BrowserWindow
110 mainWindow = createMainWindow(entryFile, mainIndexURL, argv, function () {
111 // When we first launch, ensure the quit flag is set to the user args
112 globals.quit = argv.quit;
113 });
114
115 // De-reference for GC
116 mainWindow.on('closed', function () {
117 mainWindow = null;
118 });
119
120 // Setup the file watcher
121 if (argv.watch) {
122 var globs = [].concat(argv.watch).filter(function (f) {
123 return typeof f === 'string';
124 });
125 if (globs.length === 0) globs = [ '**/*.{js,json}' ];
126 if (customHtml && globs.indexOf(htmlFile) === -1) {
127 // also watch the specified --index HTML file
128 globs.push(htmlFile);
129 }
130 watcher = createWatch(globs, argv);
131 watcher.on('change', function (file) {
132 if (mainWindow) mainWindow.reload();
133 });
134 }
135
136 // Fatal error in renderer
137 ipc.on('error', function (event, errObj) {
138 var err = JSON.parse(errObj);
139 bail(err.stack);
140 });
141
142 function bail (err) {
143 console.error(err.stack ? err.stack : err);
144 if (globals.quit) {
145 exitWithCode1 = true;
146 if (mainWindow) mainWindow.close();
147 }
148 }
149
150 function fatal (err) {
151 globals.quit = true;
152 bail(err);
153 }
154});