Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ define(function (require, exports, module) {
PreferencesManager = require("preferences/PreferencesManager"),
Resizer = require("utils/Resizer"),
LiveDevelopmentMain = require("LiveDevelopment/main"),
NodeConnection = require("utils/NodeConnection"),
ExtensionUtils = require("utils/ExtensionUtils");

// Load modules that self-register and just need to get included in the main project
Expand Down
3 changes: 3 additions & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ define(function (require, exports, module) {
exports.DEBUG_SHOW_PERF_DATA = "debug.showPerfData";
exports.DEBUG_NEW_BRACKETS_WINDOW = "debug.newBracketsWindow";
exports.DEBUG_SWITCH_LANGUAGE = "debug.switchLanguage";
exports.DEBUG_ENABLE_NODE_DEBUGGER = "debug.enableNodeDebugger";
exports.DEBUG_LOG_NODE_STATE = "debug.logNodeState";
exports.DEBUG_RESTART_NODE = "debug.restartNode";

// Help
exports.HELP_CHECK_FOR_UPDATE = "help.checkForUpdate";
Expand Down
4 changes: 4 additions & 0 deletions src/command/Menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@ define(function (require, exports, module) {
menu.addMenuDivider();
menu.addMenuItem(Commands.DEBUG_RUN_UNIT_TESTS);
menu.addMenuItem(Commands.DEBUG_SHOW_PERF_DATA);
menu.addMenuDivider();
menu.addMenuItem(Commands.DEBUG_ENABLE_NODE_DEBUGGER);
menu.addMenuItem(Commands.DEBUG_LOG_NODE_STATE);
menu.addMenuItem(Commands.DEBUG_RESTART_NODE);
}

/*
Expand Down
9 changes: 8 additions & 1 deletion src/debug/DebugCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ define(function (require, exports, module) {
Strings = require("strings"),
PerfUtils = require("utils/PerfUtils"),
NativeApp = require("utils/NativeApp"),
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem;
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
NodeDebugUtils = require("debug/NodeDebugUtils");

function handleShowDeveloperTools(commandData) {
brackets.app.showDeveloperTools();
Expand Down Expand Up @@ -272,6 +273,12 @@ define(function (require, exports, module) {
CommandManager.register(Strings.CMD_SHOW_PERF_DATA, Commands.DEBUG_SHOW_PERF_DATA, _handleShowPerfData);
CommandManager.register(Strings.CMD_SWITCH_LANGUAGE, Commands.DEBUG_SWITCH_LANGUAGE, _handleSwitchLanguage);


// Node-related Commands
CommandManager.register(Strings.CMD_ENABLE_NODE_DEBUGGER, Commands.DEBUG_ENABLE_NODE_DEBUGGER, NodeDebugUtils.enableDebugger);
CommandManager.register(Strings.CMD_LOG_NODE_STATE, Commands.DEBUG_LOG_NODE_STATE, NodeDebugUtils.logNodeState);
CommandManager.register(Strings.CMD_RESTART_NODE, Commands.DEBUG_RESTART_NODE, NodeDebugUtils.restartNode);

_enableRunTestsMenuItem();

// exposed for convenience, but not official API
Expand Down
133 changes: 133 additions & 0 deletions src/debug/NodeDebugUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.
*
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */

define(function (require, exports, module) {
"use strict";

var NodeConnection = require("utils/NodeConnection");

/**
* @private
* @type{NodeConnection}
* Connection to node for executing commands like enableDebugger
*/
var _nodeConnection = null;

/**
* @private
* @type{Array.<{level: string, timestamp: Date, message: string}>}
* History of all log messages received from node (useful for inspecting
* from the developer tools console)
*/
var _nodeLog = [];

/**
* Logs the state of the current node server to the console.
*/
function logNodeState() {
if (brackets.app && brackets.app.getNodeState) {
brackets.app.getNodeState(function (err, port) {
if (err) {
console.log("[NodeDebugUtils] Node is in error state " + err);
} else {
console.log("[NodeDebugUtils] Node is listening on port " + port);
}
});
} else {
console.error("[NodeDebugUtils] No brackets.app.getNodeState function. Maybe you're running the wrong shell?");
}
}

/**
* Sends a command to node to cause a restart.
*/
function restartNode() {
try {
_nodeConnection.domains.base.restartNode();
} catch (e) {
alert("Failed trying to restart Node: " + e.message);
}
}

/**
* Sends a command to node to enable the debugger.
*/
function enableDebugger() {
try {
_nodeConnection.domains.base.enableDebugger();
} catch (e) {
alert("Failed trying to enable Node debugger: " + e.message);
}
}

/**
* @private
* Handler for log events from Node. Stores the messages in an internal array
* for possible inspection in the developer tools. Also forwards messages to
* the developer tools console.
* @param {jQuery.Event} evt The event object from jQuery (not used)
* @param {string} level The level of the log message. Can be anything, but
* should be something like "log", "info", "warn", or "error"
* @param {string} timestamp Time the event occurred in node, as a string
* @param {string} message The log message
*/
function handleLogEvent(evt, level, timestamp, message) {
// For some reason, stringifying and then parsing a Date through JSON turns
// it into a string.
var timestampAsDate = new Date(timestamp);

_nodeLog.push({
level: level,
timestamp: timestampAsDate,
message: message
});

var formattedMessage = "[node-" + level + " " + timestampAsDate.toLocaleTimeString() + "] " + message;

switch (level) {
case "info":
case "warn":
case "error":
console[level](formattedMessage);
break;
default:
console.log(formattedMessage);
}

}

_nodeConnection = new NodeConnection();
_nodeConnection.connect(true);

// TODO: It would be nice to add a menu item that allows the user
// to enable/disable forwarding of node logs to the console.
$(_nodeConnection).on("base.log", handleLogEvent);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to have an option to turn off node logging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Added a TODO.


exports.logNodeState = logNodeState;
exports.restartNode = restartNode;
exports.enableDebugger = enableDebugger;

});
3 changes: 3 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ define({
"CMD_NEW_BRACKETS_WINDOW" : "New {APP_NAME} Window",
"CMD_SHOW_EXTENSIONS_FOLDER" : "Show Extensions Folder",
"CMD_SWITCH_LANGUAGE" : "Switch Language",
"CMD_ENABLE_NODE_DEBUGGER" : "Enable Node Debugger",
"CMD_LOG_NODE_STATE" : "Log Node State to Console",
"CMD_RESTART_NODE" : "Restart Node",

// Help menu commands
"HELP_MENU" : "Help",
Expand Down
Loading