Permalink
Browse files

references #32 #36

  • Loading branch information...
1 parent 33927fe commit d2c31da38bc585dfbe932ae0dc49f381da4af520 @olegp committed May 19, 2012
Showing with 70 additions and 59 deletions.
  1. +4 −4 bin/common-node
  2. +2 −0 lib/all.js
  3. +64 −55 lib/run.js
View
@@ -1,4 +1,4 @@
-#!/bin/bash
-COMMON_PATH="$(npm root -g)/common-node/lib/"
-export NODE_PATH="$NODE_PATH:$COMMON_PATH"
-exec node $COMMON_PATH/run.js "$@"
+#!/usr/bin/env node
+process.argv.shift();
+require('../lib/run')();
+
View
@@ -11,3 +11,5 @@ var modules = ['assert', 'binary', 'fs-base', 'httpclient', 'io', 'jsgi',
for( var i = 0; i < modules.length; i++) {
exports[modules[i]] = require('./' + modules[i]);
}
+
+exports.modules = modules;
View
@@ -5,63 +5,72 @@
/**
* Runs the specified module.
*
- * @param main the module to run
+ * @param main
+ * the module to run
*/
var run = module.exports = function(main) {
- var options = {}, argv = [];
- process.argv.forEach(function(arg) {
- if(!arg.indexOf('-')) {
- options[arg.substr(1)] = true;
- } else {
- argv.push(arg);
- }
- });
- var path = require('path');
- require('./all');
- if(options.v) {
- spawn(function() {
- var fs = require('./fs-base');
- // TODO look into resolve(), this code relies on some odd behavior in it
- var p = fs.resolve(fs.directory(module.filename), 'package.json');
- console.log('v'
- + JSON.parse(fs.openRaw(p).read().decodeToString()).version);
- });
- return;
- }
- if(options.t) {
- require('traceur');
- }
- if(argv.length > 1) {
- spawn(function() {
- try {
- main = main || require(path.resolve(process.cwd(), argv[1]));
- if(main.app) {
- require('./jsgi').run(main.app, argv[2] || 8080);
- }
- } catch(e) {
- // e.stack = e.stack.split('\n').slice(0, -7).join('\n');
- console.error(e.stack);
- }
- });
- } else {
- var system = require('./system');
- var line;
- spawn(function() {
- do {
- system.stdout.write('> ');
- if(line = system.stdin.readLine()) {
- try {
- console.log(eval(line));
- } catch(e) {
- console.log(e.toString());
- }
- }
- } while(line);
- });
- }
+ var options = {}, argv = [];
+ process.argv.forEach(function(arg) {
+ if (!arg.indexOf('-')) {
+ options[arg.substr(1)] = true;
+ } else {
+ argv.push(arg);
+ }
+ });
+ var path = require('path');
+ var all = require('./all');
+ // over-ride built in module names
+ var orig = require('module').prototype.require;
+ require('module').prototype.require = function(path) {
+ return orig.call(this, ~all.modules.indexOf(path) ? 'common-node/lib/' + path
+ : path);
+ };
+
+ if (options.v) {
+ spawn(function() {
+ var fs = require('./fs-base');
+ // TODO look into resolve(), this code relies on some odd behavior in it
+ var p = fs.resolve(fs.directory(module.filename), 'package.json');
+ console.log('v'
+ + JSON.parse(fs.openRaw(p).read().decodeToString()).version);
+ });
+ return;
+ }
+ if (options.t) {
+ require('traceur');
+ }
+ if (argv.length > 1) {
+ spawn(function() {
+ try {
+ var module = require(path.resolve(process.cwd(), main || argv[1]));
+ if (module.app) {
+ require('./jsgi').run(module.app, argv[2] || 8080);
+ }
+ } catch (e) {
+ // e.stack = e.stack.split('\n').slice(0, -7).join('\n');
+ console.error(e.stack);
+ }
+ });
+ } else {
+ var system = require('./system');
+ var line;
+ spawn(function() {
+ do {
+ system.stdout.write('> ');
+
+ if (line = system.stdin.readLine()) {
+ try {
+ console.log(eval(line));
+ } catch (e) {
+ console.log(e.toString());
+ }
+ }
+ } while (line);
+ });
+ }
};
-if(require.main == module) {
- process.argv.shift(); // remove node, which is first arg
- run();
+if (require.main == module) {
+ process.argv.shift(); // remove node, which is first arg
+ run();
}

0 comments on commit d2c31da

Please sign in to comment.