forked from adobe/brackets-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncher.js
More file actions
132 lines (120 loc) · 4.8 KB
/
Launcher.js
File metadata and controls
132 lines (120 loc) · 4.8 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
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
"use strict";
var Logger = require("./Logger"),
Server = require("./Server"),
os = require("os");
/** @define {boolean} Whether debugger should be enabled at launch
*
* If true, the debugger is enabled automatically, and the init function is
* *not* called. This gives the user time to connect with the remote
* debugger and set breakpoints before running "launch" him/herself from the
* console. In this case, launch is run by calling the global function
* debugLaunch(), which only exists if DEBUG_ON_LAUNCH is set.
*
* NOTE: This is only useful for debugging the launch routine. If you
* want to simply debug some command module, enable the debugger by
* sending the base.enableDebugger command and then connect to it.
*
* NOTE: On Mac, if you leave the debugger stopped at a breakpoint and then
* exit Brackets, the node process will become abandoned. (Because it is
* stopped, it will never check its stdin to see if it's closed, so it
* won't know that it's parent process has died.)
*/
var DEBUG_ON_LAUNCH = false;
/** @define {?string} Filename to dump all log data to.
* If not null, all log data is appended to the specified file.
*/
var LOG_FILENAME_ON_LAUNCH = null;
function exit() {
if (Server) {
Server.stop();
}
process.exit(0);
}
/**
* Top-level handler for any uncaught exception. Attempts to log
* the exception and shut down gracefully. (Though that may of course
* fail depending on the exception.)
*
* TODO: Switch to using Node Domains once they're stable enough. In v0.8.x
* Domains are "Stability: 1 - Experimental". What we really want to
* do is run every connection in a separate Domain. Then, if an individual
* connection has an uncaught exception, we can just close it and still
* continue to run normally.
*/
function uncaughtExceptionHandler() {
var args = Array.prototype.slice.call(arguments, 0);
args = args.map(function (arg) {
return arg instanceof Error ? arg.stack : arg;
});
args.unshift("[Launcher] uncaught exception at top level, exiting.");
Logger.error.apply(null, args);
exit();
}
/**
* Setup the Logger and launch the server. If DEBUG_ON_LAUNCH is false,
* this is called immediately on module load. If DEBUG_ON_LAUNCH is true,
* this method can be called by calling the global debugLaunch function
* from the console.
*/
function launch() {
process.on("uncaughtException", uncaughtExceptionHandler);
if (LOG_FILENAME_ON_LAUNCH) {
Logger.setLogFilename(LOG_FILENAME_ON_LAUNCH);
}
Logger.remapConsole();
Server.on("end", function () {
Server = null;
Logger.info(
"[Launcher] received Server \"end\" event, exiting process"
);
exit();
});
Server.start();
}
if (!DEBUG_ON_LAUNCH) {
launch();
} else {
var noopTimer = setInterval(function () {
// no-op so that we don't exit the process
}, 100000);
// Inject a global so that user can call launch from the console
// The debugger won't stop at breakpoints if they're reached as a
// direct result of evaluation of the console. So, we call launch()
// from a timer. This will allow hitting breakpoints set in launch()
// and in the functions it calls.
global.debugLaunch = function () {
process.nextTick(function () {
clearInterval(noopTimer);
launch();
});
};
process._debugProcess(process.pid);
}
// Set environment variable to use built-in Node.js API for temp directory
if (!process.env["TMPDIR"] && !process.env["TMP"] && !process.env["TEMP"]) {
process.env["TMPDIR"] = process.env["TMP"] = process.env["TEMP"] = os.tmpdir();
}
exports.launch = launch;
exports.exit = exit;