Skip to content

Commit

Permalink
Use NodeDomain instead of NodeConnection
Browse files Browse the repository at this point in the history
We switched from a HTTP server responding with a EventSource body to
Brackets' built-in RPC functionality. This removed some boilderplate code
and allows us to send output without waiting for a line terminator.

Fixes #43
  • Loading branch information
Acconut committed Sep 18, 2015
1 parent 4d4a1de commit 0e6909d
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 189 deletions.
126 changes: 0 additions & 126 deletions backend.js

This file was deleted.

4 changes: 0 additions & 4 deletions config.json

This file was deleted.

70 changes: 21 additions & 49 deletions main.js
Expand Up @@ -8,42 +8,28 @@ define(function (require, exports, module) {
WorkspaceManager = brackets.getModule("view/WorkspaceManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NodeConnection = brackets.getModule("utils/NodeConnection"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
ProjectManager = brackets.getModule("project/ProjectManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
ansi = require("./ansi"),
prefs = require("./preferences"),
nodeConnection = new NodeConnection(),
NodeMenuID = "node-menu",
NodeMenu = Menus.addMenu("Node.js", NodeMenuID),
source = null,
NODE_SETTINGS_DIALOG_ID = "node-settings-dialog",
NODE_INSTALL_DIALOG_ID = "node-install-dialog",
NODE_EXEC_DIALOG_ID = "node-exec-dialog",
LS_PREFIX = "node-",
API_VERSION = 1,
DOMAIN_NAME = "brackets-nodejs",
scrollEnabled = prefs.get("autoscroll");

/**
* Load the configuration
* Connect to the backend nodejs domain
*/
var config = JSON.parse(require("text!config.json"));

/**
* Start the node server
*/
nodeConnection.connect(true).then(function () {
nodeConnection.loadDomains(
[ExtensionUtils.getModulePath(module, "server.js")],
true
).then(
function () {
console.log("[brackets-nodejs] Connected to nodejs");
}
).fail(
function () {
console.log("[brackets-nodejs] Failed to connect to nodejs. The server may be up because of another instance");
}
);
var domain = new NodeDomain(DOMAIN_NAME, ExtensionUtils.getModulePath(module, "node/processDomain"));

domain.on("output", function(info, data) {
Panel.write(data);
});

/**
Expand All @@ -64,12 +50,6 @@ define(function (require, exports, module) {
*/
// This need to be inside quotes since new is a reserved word
"new": function (command, cwd) {

if (source && source.close) source.close();

// Build url
var url = "http://" + config.host + ":" + config.port + "/?command=" + encodeURIComponent(command);

// If no cwd is specified use the current file's directory
// if available else fallback to the project directory
var doc = DocumentManager.getCurrentDocument(),
Expand All @@ -81,30 +61,22 @@ define(function (require, exports, module) {
} else {
dir = ProjectManager.getProjectRoot().fullPath;
}

// Append cwd to url
url += "&cwd=" + encodeURIComponent(dir);

// Add api version
url += "&apiversion=" + API_VERSION;


ConnectionManager.exit();
Panel.show(command);
Panel.clear();

domain.exec("startProcess", command, dir)
.done(function(exitCode) {
Panel.write("Program exited with status code of " + exitCode + ".");
}).fail(function(err) {
Panel.write("Error inside brackets-nodejs' processes occured: \n" + err);
});

// Store the last command and cwd
this.last.command = command;
this.last.cwd = dir;

// Server should be running
source = new EventSource(url);

source.addEventListener("message", function (msg) {
Panel.write(msg.data);
}, false);
source.addEventListener("error", function () {
source.close();
Panel.write("Program exited.");
}, false);

Panel.show(command);
Panel.clear();
},

newNpm: function (command) {
Expand Down Expand Up @@ -154,7 +126,7 @@ define(function (require, exports, module) {
* Close the current connection if server is started
*/
exit: function () {
source.close();
domain.exec("stopProcess");
}
};

Expand Down Expand Up @@ -196,7 +168,7 @@ define(function (require, exports, module) {
* @param: String to be output
*/
write: function (str) {
var e = document.createElement("div");
var e = document.createElement("span");
e.innerHTML = ansi(str.replace(/</g, "&lt;").replace(/>/g, "&gt;"));

var scroll = false;
Expand Down
98 changes: 98 additions & 0 deletions node/processDomain.js
@@ -0,0 +1,98 @@
(function () {
"use strict";

var treekill = require("treekill"),
exec = require("child_process").exec,
domain = null,
child = null,
DOMAIN_NAME = "brackets-nodejs";

function cmdStartProcess(command, cwd, cb) {
if(child !== null) {
treekill(child.pid);
}

child = exec(command, {
cwd: cwd,
silent: true
});

// Send data to the client
var send = function(data) {

// Support for ansi colors and text decorations
data = data.toString().replace(/\x1B\[/g, "\\x1B[");

domain.emitEvent(DOMAIN_NAME, "output", data);
};

child.stdout.on("data", send);
child.stderr.on("data", send);

child.on("exit", function(code) {
cb(null, code);
});

child.on("error", function(err) {
cb(err);
});
}

function cmdStopProcess() {
if(child !== null) {
treekill(child.pid);
}
}

function init(domainManager) {
domain = domainManager;

if(!domainManager.hasDomain(DOMAIN_NAME)) {
domainManager.registerDomain(DOMAIN_NAME, {
major: 0,
minor: 0
});
}

domainManager.registerCommand(
DOMAIN_NAME,
"startProcess",
cmdStartProcess,
true,
"Starts the process using the supplied command",
[
{
name: "command",
type: "string"
},
{
name: "cwd",
type: "string"
}
]
);

domainManager.registerCommand(
DOMAIN_NAME,
"stopProcess",
cmdStopProcess,
false,
"Stops the process if one is already started",
[]
);

domainManager.registerEvent(
DOMAIN_NAME,
"output",
[
{
name: "output",
type: "string"
}
]
);
}

exports.init = init;

}());
9 changes: 0 additions & 9 deletions server.js

This file was deleted.

1 change: 1 addition & 0 deletions test/env.js
@@ -0,0 +1 @@
console.log(process.env);
4 changes: 3 additions & 1 deletion test/hello.js
Expand Up @@ -34,4 +34,6 @@ console.log("Hello World".cyan);
console.log("Hello World".green);
console.log("Hello World".magenta);
console.log("Hello World".red);
console.log("Hello World".yellow);
console.log("Hello World".yellow);

process.exit(3);
5 changes: 5 additions & 0 deletions test/no-new-line.js
@@ -0,0 +1,5 @@
for(var i = 0; i < 5; i++) {
setTimeout(function() {
process.stdout.write(". ");
}, i * 1000);
}

0 comments on commit 0e6909d

Please sign in to comment.