This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Adding support for require-like initialization of node,

so `node foo`
will load one of:
./foo.js
./foo.node
./foo/index.js
./foo/index.node

Test cases added.
Ensured no conflict with native names.
  • Loading branch information...
1 parent 4c5e570 commit 52f93185c783beb71535f59e478fcac8912d0c54 @aikar aikar committed with ry Jan 25, 2011
View
@@ -150,8 +150,9 @@ Module._resolveLookupPaths = function(request, parent) {
Module._load = function(request, parent) {
- debug('Module._load REQUEST ' + (request) +
- ' parent: ' + parent.id);
+ if (parent) {
+ debug('Module._load REQUEST ' + (request) + ' parent: ' + parent.id);
+ }
var resolved = Module._resolveFilename(request, parent);
var id = resolved[0];
@@ -300,7 +301,7 @@ Module._extensions['.node'] = function(module, filename) {
Module.runMain = function() {
// Load the main module--the command line argument.
process.mainModule = new Module('.');
- process.mainModule.load(process.argv[1]);
+ Module._load(process.argv[1]);
};
Module._initPaths = function() {
@@ -0,0 +1,3 @@
+(function() {
+ require('util').print('Loaded successfully!');
+})();
@@ -0,0 +1,6 @@
+(function() {
+ var fs = require('fs');
+ if (fs.readFile) {
+ require('util').print('fs loaded successfully');
+ }
+})();
@@ -9,5 +9,6 @@ ReferenceError: foo is not defined
at Module._compile (module.js:*)
at Object..js (module.js:*)
at Module.load (module.js:*)
+ at Function._load (module.js:178:10)
at Array.<anonymous> (module.js:*)
at EventEmitter._tickCallback (node.js:*)
View
@@ -0,0 +1,39 @@
+(function() {
+ var assert = require('assert'),
+ child = require('child_process'),
+ util = require('util'),
+ common = require('../common');
+ if (process.env['TEST_INIT']) {
+ util.print('Loaded successfully!');
+ } else {
+ // change CWD as we do this test so its not dependant on current CWD
+ // being in the test folder
+ process.chdir(__dirname);
+
+ child.exec(process.execPath + ' test-init',{env:{'TEST_INIT':1}},
+ function(err, stdout, stderr) {
+ assert.equal(stdout, 'Loaded successfully!', '`node test-init` failed!');
+ });
+ child.exec(process.execPath + ' test-init.js', {env:{'TEST_INIT':1}},
+ function(err, stdout, stderr) {
+ assert.equal(stdout, 'Loaded successfully!', '`node test-init.js` failed!');
+ });
+
+ // test-init-index is in fixtures dir as requested by ry, so go there
+ process.chdir(common.fixturesDir);
+
+ child.exec(process.execPath + ' test-init-index',{env:{'TEST_INIT':1}},
+ function(err, stdout, stderr) {
+ assert.equal(stdout, 'Loaded successfully!', '`node test-init-index failed!');
+ });
+
+ // ensures that `node fs` does not mistakenly load the native 'fs' module
+ // instead of the desired file and that the fs module loads as expected in node
+ process.chdir(common.fixturesDir + '/test-init-native/');
+
+ child.exec(process.execPath + ' fs', {env:{'TEST_INIT':1}},
+ function(err, stdout, stderr) {
+ assert.equal(stdout, 'fs loaded successfully', '`node fs` failed!');
+ });
+ }
+})();

0 comments on commit 52f9318

Please sign in to comment.