diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea4812e --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +brackets-simple-node +==================== + +Simple example of how to use node from a Brackets extension. For more info, see +[Brackets Node Process: Overview for Developers](https://github.com/adobe/brackets/wiki/Brackets-Node-Process:-Overview-for-Developers). diff --git a/main.js b/main.js index e70a36b..2f30cea 100644 --- a/main.js +++ b/main.js @@ -29,77 +29,25 @@ maxerr: 50, browser: true */ define(function (require, exports, module) { "use strict"; - var AppInit = brackets.getModule("utils/AppInit"), - ProjectManager = brackets.getModule("project/ProjectManager"), - ExtensionUtils = brackets.getModule("utils/ExtensionUtils"), - NodeConnection = brackets.getModule("utils/NodeConnection"); + var ExtensionUtils = brackets.getModule("utils/ExtensionUtils"), + NodeDomain = brackets.getModule("utils/NodeDomain"); - // Helper function that chains a series of promise-returning - // functions together via their done callbacks. - function chain() { - var functions = Array.prototype.slice.call(arguments, 0); - if (functions.length > 0) { - var firstFunction = functions.shift(); - var firstPromise = firstFunction.call(); - firstPromise.done(function () { - chain.apply(null, functions); - }); - } - } + var simpleDomain = new NodeDomain("simple", ExtensionUtils.getModulePath(module, "node/SimpleDomain")); - - AppInit.appReady(function () { - // Create a new node connection. Requires the following extension: - // https://github.com/joelrbrandt/brackets-node-client - var nodeConnection = new NodeConnection(); - - // Every step of communicating with node is asynchronous, and is - // handled through jQuery promises. To make things simple, we - // construct a series of helper functions and then chain their - // done handlers together. Each helper function registers a fail - // handler with its promise to report any errors along the way. - - - // Helper function to connect to node - function connect() { - var connectionPromise = nodeConnection.connect(true); - connectionPromise.fail(function () { - console.error("[brackets-simple-node] failed to connect to node"); - }); - return connectionPromise; - } - - // Helper function that loads our domain into the node server - function loadSimpleDomain() { - var path = ExtensionUtils.getModulePath(module, "node/SimpleDomain"); - var loadPromise = nodeConnection.loadDomains([path], true); - loadPromise.fail(function () { - console.log("[brackets-simple-node] failed to load domain"); - }); - return loadPromise; - } - - // Helper function that runs the simple.getMemory command and - // logs the result to the console - function logMemory() { - var memoryPromise = nodeConnection.domains.simple.getMemory(); - memoryPromise.fail(function (err) { - console.error("[brackets-simple-node] failed to run simple.getMemory", err); - }); - memoryPromise.done(function (memory) { + // Helper function that runs the simple.getMemory command and + // logs the result to the console + function logMemory() { + simpleDomain.exec("getMemory", false) + .done(function (memory) { console.log( - "[brackets-simple-node] Memory: %d of %d bytes free (%d%)", - memory.free, - memory.total, - Math.floor(memory.free * 100 / memory.total) + "[brackets-simple-node] Memory: %d bytes free", + memory ); + }).fail(function (err) { + console.error("[brackets-simple-node] failed to run simple.getMemory", err); }); - return memoryPromise; - } - - // Call all the helper functions in order - chain(connect, loadSimpleDomain, logMemory); - - }); + } + // Log memory when extension is loaded + logMemory(); }); diff --git a/node/SimpleDomain.js b/node/SimpleDomain.js index c7c4b73..f3acebd 100644 --- a/node/SimpleDomain.js +++ b/node/SimpleDomain.js @@ -33,31 +33,37 @@ maxerr: 50, node: true */ /** * @private * Handler function for the simple.getMemory command. - * @return {{total: number, free: number}} The total and free amount of - * memory on the user's system, in bytes. + * @param {boolean} total If true, return total memory; if false, return free memory only. + * @return {number} The amount of memory. */ - function cmdGetMemory() { - return {total: os.totalmem(), free: os.freemem()}; + function cmdGetMemory(total) { + if (total) { + return os.totalmem(); + } else { + return os.freemem(); + } } /** * Initializes the test domain with several test commands. - * @param {DomainManager} DomainManager The DomainManager for the server + * @param {DomainManager} domainManager The DomainManager for the server */ - function init(DomainManager) { - if (!DomainManager.hasDomain("simple")) { - DomainManager.registerDomain("simple", {major: 0, minor: 1}); + function init(domainManager) { + if (!domainManager.hasDomain("simple")) { + domainManager.registerDomain("simple", {major: 0, minor: 1}); } - DomainManager.registerCommand( + domainManager.registerCommand( "simple", // domain name "getMemory", // command name cmdGetMemory, // command handler function - false, // this command is synchronous - "Returns the total and free memory on the user's system in bytes", - [], // no parameters - [{name: "memory", - type: "{total: number, free: number}", - description: "amount of total and free memory in bytes"}] + false, // this command is synchronous in Node + "Returns the total or free memory on the user's system in bytes", + [{name: "total", // parameters + type: "string", + description: "True to return total memory, false to return free memory"}], + [{name: "memory", // return values + type: "number", + description: "amount of memory in bytes"}] ); }