-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathinit.js
More file actions
443 lines (367 loc) · 11.4 KB
/
init.js
File metadata and controls
443 lines (367 loc) · 11.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* @namespace Editor
*/
global.Editor = {};
// ---------------------------
// precheck
// ---------------------------
(function () {
if ( !__app ) {
console.error( '\'global.__app\' is undefined.');
process.exit(1);
return;
}
/**
* The Editor.App is your app.js module. Read more details in
* [Define your application](https://github.com/fireball-x/editor-framework/blob/master/docs/manual/define-your-app.md).
* @type object
*/
Editor.App = __app;
if ( !__app.path ) {
console.error( '\'__app.path\' is undefined. please set `path: __dirname` manually in your __app structure.');
process.exit(1);
return;
}
/**
* The current app.js running directory.
* @type string
*/
Editor.cwd = __app.path;
})();
// ---------------------------
// load modules
// ---------------------------
var App = require('app');
var Path = require('fire-path');
var Fs = require('fire-fs');
var Url = require('fire-url');
var Commander = require('commander');
var Chalk = require('chalk');
var Winston = require('winston');
var Async = require('async');
// this will prevent default atom-shell uncaughtException
process.removeAllListeners('uncaughtException');
process.on('uncaughtException', function(error) {
if ( Editor && Editor.sendToWindows ) {
Editor.sendToWindows('console:error', error.stack || error);
}
Winston.uncaught( error.stack || error );
});
var _appPackageJson = JSON.parse(Fs.readFileSync(Path.join(Editor.cwd,'package.json')));
var _editorFrameworkPackageJson = JSON.parse(Fs.readFileSync(Path.join(__dirname,'package.json')));
// ---------------------------
// initialize minimal Editor
// ---------------------------
/**
* The name of your app. It is defined in the `name` field in package.json
* @type string
*/
Editor.name = App.getName();
/**
* versions of your app and submodules
* @type Object
*/
Editor.versions = {
'editor-framework': _editorFrameworkPackageJson.version,
};
Editor.versions[Editor.name] = App.getVersion();
/**
* The absolute path of your main entry file. Usually it is `{your-app}/app.js`.
* @type string
*/
Editor.mainEntry = Path.join( Editor.cwd, _appPackageJson.main );
/**
* The editor framework module path. Usually it is `{your-app}/editor-framework/`
* @type string
*/
Editor.frameworkPath = __dirname;
/**
* Your application's data path. Usually it is `~/.{your-app-name}`
* @type string
*/
Editor.appHome = Path.join( App.getPath('home'), '.' + Editor.name );
// initialize ~/.{app-name}
if ( !Fs.existsSync(Editor.appHome) ) {
Fs.makeTreeSync(Editor.appHome);
}
// initialize ~/.{app-name}/local/
var settingsPath = Path.join(Editor.appHome, 'local');
if ( !Fs.existsSync(settingsPath) ) {
Fs.mkdirSync(settingsPath);
}
// ---------------------------
// initialize logs/
// ---------------------------
// MacOSX: ~/Library/Logs/{app-name}
// Windows: %APPDATA%, some where like 'C:\Users\{your user name}\AppData\Local\...'
// get log path
var _logpath = '';
if ( process.platform === 'darwin' ) {
_logpath = Path.join(App.getPath('home'), 'Library/Logs/' + Editor.name );
}
else {
_logpath = App.getPath('appData');
}
if ( !Fs.existsSync(_logpath) ) {
Fs.makeTreeSync(_logpath);
}
var _logfile = Path.join(_logpath, Editor.name + '.log');
if ( Fs.existsSync(_logfile) ) {
Fs.unlinkSync(_logfile);
}
var winstonLevels = {
normal : 0,
success : 1,
failed : 2,
info : 3,
warn : 4,
error : 5,
fatal : 6,
uncaught : 7,
};
Winston.setLevels(winstonLevels);
Winston.remove(Winston.transports.Console);
Winston.add(Winston.transports.File, {
level: 'normal',
filename: _logfile,
json: false,
});
var chalk_id = Chalk.bgBlue;
var chalk_success = Chalk.green;
var chalk_warn = Chalk.yellow;
var chalk_error = Chalk.red;
var chalk_info = Chalk.cyan;
var levelToFormat = {
normal: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + text;
},
success: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_success(text);
},
failed: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_error(text);
},
info: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_info(text);
},
warn: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_warn.inverse.bold('Warning:') + ' ' + chalk_warn(text);
},
error: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_error.inverse.bold('Error:') + ' ' + chalk_error(text);
},
fatal: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_error.inverse.bold('Fatal Error:') + ' ' + chalk_error(text);
},
uncaught: function ( text ) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
return pid + chalk_error.inverse.bold('Uncaught Exception:') + ' ' + chalk_error(text);
},
};
Winston.add( Winston.transports.Console, {
level: 'normal',
formatter: function (options) {
var pid = chalk_id('[' + process.pid + ']') + ' ';
var text = '';
if ( options.message !== undefined ) {
text += options.message;
}
if ( options.meta && Object.keys(options.meta).length ) {
text += ' ' + JSON.stringify(options.meta);
}
// output log by different level
var formatter = levelToFormat[options.level];
if ( formatter ) {
return formatter(text);
}
return text;
}
});
// ---------------------------
// initialize Commander
// ---------------------------
// NOTE: commander only get things done barely in core level,
// it doesn't touch the page level, so it should not put into App.on('ready')
Commander
.version(App.getVersion())
.option('--dev', 'Run in development environment')
.option('--dev-mode <mode>', 'Run in specific dev-mode')
.option('--show-devtools', 'Open devtools automatically when main window loaded')
.option('--debug <port>', 'Open in browser context debug mode', parseInt )
.option('--debug-brk <port>', 'Open in browser context debug mode, and break at first.', parseInt)
.option('--test <path>', 'Run tests in path' )
;
// EXAMPLE:
// usage
// Commander
// .usage('[options] <file ...>')
// ;
// command
// Commander
// .command('foobar').action( function () {
// console.log('foobar!!!');
// process.exit(1);
// })
// ;
if ( Editor.App.initCommander ) {
Editor.App.initCommander(Commander);
}
// finish Commander initialize
Commander.parse(process.argv);
// apply argv to Editor
Editor.isDev = Commander.dev;
Editor.devMode = Commander.devMode;
Editor.showDevtools = Commander.showDevtools;
Editor.test = Commander.test;
// ---------------------------
// Define Editor.App APIs
// ---------------------------
var _editorAppIpc;
function _loadEditorApp () {
var editorApp = Editor.App;
if ( editorApp.load ) {
try {
Editor.App.load();
}
catch (err) {
Editor.failed( 'Failed to load Editor.App, %s.', err.stack );
return;
}
}
// register ipcs
var ipcListener = new Editor.IpcListener();
for ( var prop in editorApp ) {
if ( prop.indexOf('app:') !== 0 )
continue;
if ( typeof editorApp[prop] === 'function' ) {
ipcListener.on( prop, editorApp[prop].bind(editorApp) );
}
}
_editorAppIpc = ipcListener;
}
function _unloadEditorApp () {
var editorApp = Editor.App;
// unregister main ipcs
_editorAppIpc.clear();
_editorAppIpc = null;
// unload main
var cache = require.cache;
if ( editorApp.unload ) {
try {
editorApp.unload();
}
catch (err) {
Editor.failed( 'Failed to unload Editor.App, %s.', err.stack );
}
}
delete cache[Editor.mainEntry];
delete global.__app;
}
function _reloadEditorApp () {
_unloadEditorApp();
require(Editor.mainEntry);
Editor.App = __app;
Editor.App.reload = _reloadEditorApp;
_loadEditorApp();
Editor.success('Editor.App reloaded');
}
// ---------------------------
// register App events
// ---------------------------
// DISABLE: http cache only happends afterwhile, not satisefy our demand (which need to happend immediately).
// App.commandLine.appendSwitch('disable-http-cache');
// App.commandLine.appendSwitch('disable-direct-write');
// quit when all windows are closed.
App.on('window-all-closed', function( event ) {
App.quit();
});
//
App.on('will-finish-launching', function() {
if ( !Editor.isDev ) {
var crashReporter = require('crash-reporter');
crashReporter.start({
productName: Editor.name,
companyName: 'Firebox Technology',
submitUrl: 'https://fireball-x.com/crash-report',
autoSubmit: false,
});
}
});
//
App.on('ready', function() {
Winston.normal( 'Initializing protocol' );
require('./core/protocol-init');
Winston.normal( 'Initializing editor' );
require('./core/editor-init');
if ( Commander.test ) {
var Test = require('./core/test-runner');
Test.run(Commander.test);
return;
}
// apply default main menu
Editor.MainMenu.apply();
// register profile path
Editor.registerProfilePath( 'global', Editor.appHome );
Editor.registerProfilePath( 'local', Path.join( Editor.appHome, 'local' ) );
// register package path
Editor.registerPackagePath( Path.join( Editor.App.path, 'builtin' ) );
Editor.registerPackagePath( Path.join( Editor.appHome, 'packages' ) );
// register default layout
Editor.registerDefaultLayout( Editor.url('editor-framework://static/layout.json') );
// init user App
if ( !Editor.App.init ) {
Winston.error('Can not find function "init" in your App');
App.terminate();
return;
}
try {
Editor.App.init(Commander);
} catch ( error ) {
Winston.error(error.stack || error);
App.terminate();
return;
}
// register user App Ipcs
_loadEditorApp();
Editor.App.reload = _reloadEditorApp;
//
Winston.success('Initial success!');
// load windows layout after local profile registered
Editor.Window.loadLayouts();
// before run the app, we start load and watch all packages
Async.series([
function ( next ) {
Winston.normal('Loading packages');
Editor.loadPackages( next );
},
function ( next ) {
Winston.normal('Prepare for watching packages');
Editor.watchPackages( next );
},
function ( next ) {
Editor.success('Watch ready');
// connect to console to sending ipc to it
Editor.connectToConsole();
// run user App
if ( !Editor.App.run ) {
Winston.error('Can not find function "run" in your App');
App.terminate();
return;
}
try {
Editor.App.run();
} catch ( error ) {
Winston.error(error.stack || error);
App.terminate();
return;
}
},
]);
});