Created
January 8, 2013 10:23
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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
wow, it really helped me, thanks jdachtera