Permalink
Browse files

Automatic generation of mod conf with args support

  • Loading branch information...
nelsonsilva committed May 23, 2012
1 parent dfae467 commit cfc127a0057792f0ea4f7c508caf179e4c18cf48
Showing with 50 additions and 10 deletions.
  1. +11 −8 README.md
  2. +9 −0 bin/node.vertx
  3. +6 −1 lib/process.js
  4. +24 −1 src/org/vertx/node/NodeVerticle.java
View
@@ -6,25 +6,28 @@ This is a simple node.js compatibility layer for [vert.x](http://vertx.io)
Clone the repo into your vertx/mods directory
+Initialize the submodules:
+`git submodule update --init`
+
Copy build.properties.sample to build.properties
`ant`
## Running
+Add node.vert.x/bin to your PATH
+
Go to your project directory (ex: ShareJS)
-Create a configuration file, ex (vertx.json) :
+Run the node.vert.x mod:
+
+`node.vert.x <arguments>`
-`
-{
- "main": "bin/exampleserver"
-}
-`
+Ex:
-Run the node.vert.x mod and pass it the conf file:
+Running ShareJS
-`vertx run node.vert.x -conf vertx.json`
+`node.vert.x bin/exampleserver.js -p 8080`
## Notes
View
9 bin/node.vertx 100644 → 100755
@@ -1,2 +1,11 @@
#!/bin/sh
+if [ $# -eq 0 ]
+then
+ echo "Usage: node.vert.x <main.js> <args*>"
+ exit 65
+fi
+main=$1
+shift 1
+args=$*
+echo "{\"main\":\"$main\",\"args\":\"$args\"}" > vertx.json
vertx run node.vert.x -conf vertx.json
View
@@ -6,5 +6,10 @@ this.process = {
env: {},
- on: function(event, handler){}
+ on: function(event, handler){},
+
+ argv: __argv,
+
+ cwd: function() { return __cwd; }
+
};
@@ -36,6 +36,9 @@
private static ThreadLocal<ScriptableObject> scopeThreadLocal = new ThreadLocal<>();
private static ThreadLocal<ClassLoader> clThreadLocal = new ThreadLocal<>();
+
+ private String scriptName;
+ private String[] argv = null;
public NodeVerticle() {}
@@ -76,6 +79,21 @@ private static void addStandardObjectsToScope(ScriptableObject scope) {
}
private void addNodeObjectsToScope(ClassLoader cl, Context cx, ScriptableObject scope) throws Exception {
+ ScriptableObject.putProperty(scope, "global", scope);
+ if(argv != null) {
+ String [] args = new String[argv.length + 2];
+ int idx = 0;
+ args[idx++] = "node.vert.x";
+ args[idx++] = scriptName;
+ for (String arg : argv) {
+ args[idx++] = arg;
+ }
+ Object a = Context.javaToJS(args, scope);
+ ScriptableObject.putProperty(scope, "__argv", a);
+ }
+ ScriptableObject.putProperty(scope, "__cwd", new File(".").getAbsolutePath());
+ ScriptableObject.putProperty(scope, "global", scope);
+
loadScript(cl, cx, scope, "./lib/process.js");
loadScript(cl, cx, scope, "./lib/timers.js");
}
@@ -92,7 +110,12 @@ public void start() throws Exception {
ClassLoader cl = this.getClass().getClassLoader();
- String scriptName = getMandatoryStringConfig("main");
+ scriptName = getMandatoryStringConfig("main");
+ String args = config.getString("args");
+ if(args != null){
+ argv = args.split(" ");
+ }
+
if(! (scriptName.startsWith("./") || scriptName.startsWith("/") || scriptName.startsWith("../")) ){
scriptName = "./" + scriptName;

0 comments on commit cfc127a

Please sign in to comment.