Comparing version 0.1.3 to 0.1.4
73
index.js
var fs=require('fs'); | ||
var vm=require('vm'); | ||
function jsinc() | ||
function jsinc() | ||
{ | ||
var files=arguments; | ||
var callback=false; | ||
if(arguments.length>1 && typeof arguments[arguments.length-1]=='function') | ||
{ | ||
var callback=arguments[arguments.length-1];//the last argument is sand box | ||
arguments=Array.prototype.slice.call(arguments,0,arguments.length-1);// the other arguments are the files array | ||
} | ||
if(arguments.length>1 && typeof arguments[arguments.length-1]=='object') | ||
{ | ||
var sandbox=arguments[arguments.length-1];//the last argument is sand box | ||
files=Array.prototype.slice.call(arguments,0,arguments.length-1);// the other arguments are the files array | ||
arguments=Array.prototype.slice.call(arguments,0,arguments.length-1);// the other arguments are the files array | ||
} | ||
var data = "", sandbox = sandbox || {}; | ||
if(arguments.length>1 && typeof arguments[arguments.length-1]=='function') | ||
{ | ||
var callback=arguments[arguments.length-1];//the last argument is sand box | ||
arguments=Array.prototype.slice.call(arguments,0,arguments.length-1);// the other arguments are the files array | ||
} | ||
var files=arguments; | ||
var sandbox = sandbox || {}; | ||
var called_done=0; | ||
for(var i=0;i<files.length;i++) | ||
{ | ||
var file=files[i]; | ||
data = fs.readFileSync( file,'utf-8'); | ||
if(!('console' in sandbox)) sandbox.console=console; // adds a console variable, probably a wanted future. | ||
if(!('window' in sandbox)) sandbox.window=sandbox; // adds window variable to sandbox, probaly required by some scripts | ||
vm.runInNewContext( data, sandbox, file ); | ||
if(callback) // do this async | ||
{ | ||
fs.readFile( file,'utf-8',function(err,data) | ||
{ | ||
if(!err) | ||
{ | ||
if(!('console' in sandbox)) sandbox.console=console; // adds a console variable, probably a wanted future. | ||
if(!('jsinc' in sandbox)) sandbox.jsinc=getbind(sandbox); | ||
//if(!('window' in sandbox)) sandbox.window=sandbox; // adds window variable to sandbox, probaly required by some scripts// commented out because the getter above workes better | ||
vm.runInNewContext( 'this.__defineGetter__(\'window\',function(){return this;});'+data, sandbox, file ); | ||
} | ||
else | ||
{ | ||
console.log(err.stack) | ||
} | ||
called_done++; | ||
if(files.length==called_done)callback(sandbox); | ||
}); | ||
} | ||
else | ||
{ | ||
var data = 'this.__defineGetter__(\'window\',function(){return this;});' + fs.readFileSync( file,'utf-8'); | ||
if(!('console' in sandbox)) sandbox.console=console; // adds a console variable, probably a wanted future. | ||
if(!('jsinc' in sandbox)) sandbox.jsinc=getbind(sandbox); | ||
//if(!('window' in sandbox)) sandbox.window=sandbox; // adds window variable to sandbox, probaly required by some scripts// commented out because the getter above workes better | ||
vm.runInNewContext( data, sandbox, file ); | ||
} | ||
} | ||
//console.log(sandbox); | ||
return sandbox; | ||
return sandbox; | ||
} | ||
function getbind(that) | ||
{ | ||
if(!that)that={}; | ||
return function() | ||
{ | ||
if(arguments.length>2 && typeof arguments[arguments.length-1]!='object' && typeof arguments[arguments.length-2]!='object' ) | ||
Array.push.call(arguments,that); | ||
else if(arguments.length>1 && typeof arguments[arguments.length-1]!='object') | ||
Array.push.call(arguments,that); | ||
jsinc.call(this,arguments); | ||
} | ||
} | ||
jsinc.getbind=getbind; | ||
jsinc.jsinc=jsinc;//backwards compatibility | ||
@@ -29,3 +80,3 @@ module.exports=jsinc; | ||
//var jsinc = require('jsinc'); | ||
//var date = jsinc( 'date.js' ); | ||
//console.log(date); | ||
//var date = jsinc( 'date.js' ); | ||
//console.log(date); |
@@ -5,3 +5,3 @@ { | ||
"description": "Include non module javascript file as module", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"homepage": "http://github.com/shimondoodkin/jsinc", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -13,3 +13,3 @@ jsinc - node.js module | ||
Object context = jsinc( String file1, ... , String fileN, Object context ) | ||
Object context = jsinc( String file1, ... , String fileN, Object context, Function callback ) | ||
@@ -77,2 +77,9 @@ * the last argument is an optional context object. | ||
Callback and Async file reading and loading | ||
------------------------------------------- | ||
var app={}; | ||
jsinc( 'date.js',{XMLHttpRequest:require('xmlhttprequest')}, function (date){ app.date=date; }); | ||
// if you add a callback function, the file will be read asynchronously. | ||
Other Ideas: | ||
@@ -79,0 +86,0 @@ ------------ |
@@ -7,1 +7,5 @@ var jsinc=require("./index.js"); | ||
var testerr=jsinc('testerrinc.js'); | ||
console.log("outside err test 1",testerr.errtest1()); | ||
console.log("outside err test 2",testerr.errtest2()); |
@@ -50,8 +50,1 @@ if(!('Array' in window)) console.log('no Array'); | ||
console.log(window.foo); | ||
function windowfootest() | ||
{ | ||
window.foo2='setting a variable to the window is the same as setting a variable to this'; | ||
console.log(foo2); | ||
console.log(window.foo2); | ||
} |
@@ -0,1 +1,2 @@ | ||
var test_name; | ||
@@ -2,0 +3,0 @@ for(test_name in this) |
10239
8
168
93