Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2013 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4482715 to your computer and use it in GitHub Desktop.
Save anonymous/4482715 to your computer and use it in GitHub Desktop.
ENYO on Node.JS USAGE: ./enyo-nodejs [dependency1.js, dependency2.js, ...] The default dependency is "source"
#! /usr/local/bin/node
var vm = require("vm"),
fs = require("fs"),
XMLHttpRequest;
try {
XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
} catch(e) {
console.log("Please run \"npm install xmlhttprequest\" if you want to use enyo.Ajax!");
}
var enyo = {
args: {
root: "./enyo"
},
locateScript: function(inPath) {
return false;
}
},
// Our execution context for enyo
context = vm.createContext({
enyo: enyo,
navigator: {},
addEventListener: function() {},
// A mock document object
document: (function() {
function DOMElement() {
this.style = {cssText: ""};
}
DOMElement.prototype = {
addEventListener: function() {},
setAttribute: function() {}
};
var document = new DOMElement();
document.createElement = function(tagName) {
var el = new DOMElement();
el.tagName = tagName || "div";
return el;
};
document.getElementById = document.write = document.getElementsByTagName = function() {};
return document;
})(),
XMLHttpRequest: XMLHttpRequest,
console: console,
require: require
}),
machine = {
// No Stylesheets needed here
sheet: function() {},
// A script loading machine using the node vm module
script: function(inPath, onSuccess, onError) {
var code = fs.readFileSync(inPath, "utf-8");
if (!code) {
return onError && onError(err);
}
vm.runInContext(code, context, inPath);
onSuccess && onSuccess();
}
};
// Add the window object to the context
context.window = context;
// Enyo bootstrap
machine.script("enyo/loader.js");
machine.script("enyo/source/boot/boot.js");
// Override the loading machine
enyo.loader.machine = machine;
enyo.depends(
"enyo/source/kernel/log.js",
"enyo/source/kernel/lang.js"
);
// Override enyo's global property
enyo.global = context;
// Use node's process.nextTick instead of setTimeout
enyo.asyncMethod = function() {
return process.nextTick(enyo.bind.apply(enyo, arguments));
};
// Load the rest of the framework
enyo.depends(
"enyo/source/kernel/job.js",
"enyo/source/kernel/macroize.js",
"enyo/source/kernel/Oop.js",
"enyo/source/kernel/Object.js",
"enyo/source/kernel/Component.js",
"enyo/source/kernel/UiComponent.js",
"enyo/source/kernel/Layout.js",
"enyo/source/kernel/Signals.js",
"enyo/source/ajax",
"enyo/source/dom",
"enyo/source/touch",
"enyo/source/ui"
);
// Load the default source folder or the dependencies specified via command line
var dependencies = process.argv.slice(2);
if (dependencies.length === 0) {
dependencies.push("source");
} else if (dependencies.length === 1 && dependencies[0] === '-h') {
console.log([
"ENYO on Node.JS",
"USAGE: ./enyo-nodejs [dependency1.js, dependency2.js, ...]",
"The default dependency is \"source\"",
].join("\n"));
return;
}
enyo.depends.apply(enyo, dependencies);
@muraray
Copy link

muraray commented Jan 24, 2014

wow, it really helped me, thanks jdachtera

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment