Skip to content

Instantly share code, notes, and snippets.

@jdachtera
Forked from anonymous/enyo-nodejs
Created January 12, 2013 13:46
Show Gist options
  • Save jdachtera/4518140 to your computer and use it in GitHub Desktop.
Save jdachtera/4518140 to your computer and use it in GitHub Desktop.
#! /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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment