1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var createWatch = require('./lib/file-watch');
|
4 | var createMainWindow = require('./lib/main-window');
|
5 | var parseArgs = require('./lib/parse-args');
|
6 | var mime = require('mime');
|
7 |
|
8 | var electron = require('electron');
|
9 | var app = electron.app;
|
10 | var ipc = electron.ipcMain;
|
11 | var globals;
|
12 |
|
13 | var exitWithCode1 = false;
|
14 | process.removeAllListeners('uncaughtException');
|
15 | process.stdin.pause();
|
16 |
|
17 | var argv = parseArgs(process.argv.slice(2));
|
18 | globals = argv.globals;
|
19 |
|
20 | app.commandLine.appendSwitch('disable-http-cache');
|
21 | if (!argv.verbose) {
|
22 | app.commandLine.appendSwitch('v', '-1');
|
23 | app.commandLine.appendSwitch('vmodule', 'console=0');
|
24 | }
|
25 |
|
26 | if (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 |
|
35 | if (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 |
|
45 | var cwd = process.cwd();
|
46 | var entryFile = argv._[0];
|
47 | if (entryFile) {
|
48 | entryFile = path.isAbsolute(entryFile) ? entryFile : path.resolve(cwd, entryFile);
|
49 | try {
|
50 | entryFile = require.resolve(entryFile);
|
51 | globals.entry = entryFile;
|
52 | } catch (err) {
|
53 | console.error(err.stack ? err.stack : err);
|
54 | process.exit(1);
|
55 | }
|
56 | }
|
57 |
|
58 | var watcher = null;
|
59 | var mainWindow = null;
|
60 | app.on('window-all-closed', function () {
|
61 | app.quit();
|
62 | });
|
63 |
|
64 |
|
65 | app.on('quit', function () {
|
66 | if (watcher) watcher.close();
|
67 | if (exitWithCode1) process.exit(1);
|
68 | });
|
69 |
|
70 | app.on('ready', function () {
|
71 | electron.protocol.registerServiceWorkerSchemes(['file:']);
|
72 |
|
73 |
|
74 | var htmlFile = path.resolve(__dirname, 'lib', 'index.html');
|
75 | var customHtml = false;
|
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 |
|
84 | electron.protocol.interceptBufferProtocol('file', function (request, callback) {
|
85 |
|
86 |
|
87 |
|
88 | var file = request.url;
|
89 | if (file === mainIndexURL) {
|
90 | file = htmlFile;
|
91 | } else if (file.indexOf('file://') === 0) {
|
92 |
|
93 | file = file.substring(7);
|
94 | file = path.resolve(cwd, path.relative(__dirname, file));
|
95 | }
|
96 |
|
97 | fs.readFile(file, function (err, data) {
|
98 |
|
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 |
|
110 | mainWindow = createMainWindow(entryFile, mainIndexURL, argv, function () {
|
111 |
|
112 | globals.quit = argv.quit;
|
113 | });
|
114 |
|
115 |
|
116 | mainWindow.on('closed', function () {
|
117 | mainWindow = null;
|
118 | });
|
119 |
|
120 |
|
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 |
|
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 |
|
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 | });
|