Skip to content
Browse files

factor out .sync into lib/sync.js

  • Loading branch information...
1 parent a800954 commit ba7038a56d78212329b64287dfaf895b1a85cf2c @substack committed Feb 18, 2013
Showing with 99 additions and 101 deletions.
  1. +1 −101 index.js
  2. +98 −0 lib/sync.js
View
102 index.js
@@ -1,104 +1,4 @@
-var fs = require('fs');
-var path = require('path');
-var existsSync = fs.existsSync || path.existsSync;
var core = require('./lib/core');
-
-exports = module.exports = function () {
-};
-
exports.core = core;
exports.isCore = function (x) { return core[x] };
-
-exports.sync = function (x, opts) {
- if (core[x]) return x;
-
- if (!opts) opts = {};
- var isFile = opts.isFile || function (file) {
- return existsSync(file) && fs.statSync(file).isFile()
- };
- var readFileSync = opts.readFileSync || fs.readFileSync;
-
- var extensions = opts.extensions || [ '.js' ];
- var y = opts.basedir
- || path.dirname(require.cache[__filename].parent.filename)
- ;
-
- opts.paths = opts.paths || [];
-
- if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
- var m = loadAsFileSync(path.resolve(y, x))
- || loadAsDirectorySync(path.resolve(y, x));
- if (m) return m;
- } else {
- var n = loadNodeModulesSync(x, y);
- if (n) return n;
- }
-
- throw new Error("Cannot find module '" + x + "'");
-
- function loadAsFileSync (x) {
- if (isFile(x)) {
- return x;
- }
-
- for (var i = 0; i < extensions.length; i++) {
- var file = x + extensions[i];
- if (isFile(file)) {
- return file;
- }
- }
- }
-
- function loadAsDirectorySync (x) {
- var pkgfile = path.join(x, '/package.json');
- if (isFile(pkgfile)) {
- var body = readFileSync(pkgfile, 'utf8');
- try {
- var pkg = JSON.parse(body);
- if (opts.packageFilter) {
- pkg = opts.packageFilter(pkg, x);
- }
-
- if (pkg.main) {
- var m = loadAsFileSync(path.resolve(x, pkg.main));
- if (m) return m;
- var n = loadAsDirectorySync(path.resolve(x, pkg.main));
- if (n) return n;
- }
- }
- catch (err) {}
- }
-
- return loadAsFileSync(path.join( x, '/index'));
- }
-
- function loadNodeModulesSync (x, start) {
- var dirs = nodeModulesPathsSync(start);
- for (var i = 0; i < dirs.length; i++) {
- var dir = dirs[i];
- var m = loadAsFileSync(path.join( dir, '/', x));
- if (m) return m;
- var n = loadAsDirectorySync(path.join( dir, '/', x ));
- if (n) return n;
- }
- }
-
- function nodeModulesPathsSync (start) {
- var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
- var parts = start.split(splitRe);
-
- var dirs = [];
- for (var i = parts.length - 1; i >= 0; i--) {
- if (parts[i] === 'node_modules') continue;
- var dir = path.join(
- path.join.apply(path, parts.slice(0, i + 1)),
- 'node_modules'
- );
- if (!parts[0].match(/([A-Za-z]:)/)) {
- dir = '/' + dir;
- }
- dirs.push(dir);
- }
- return dirs.concat(opts.paths);
- }
-};
+exports.sync = require('./lib/sync');
View
98 lib/sync.js
@@ -0,0 +1,98 @@
+var core = require('./core');
+var fs = require('fs');
+var path = require('path');
+var existsSync = fs.existsSync || path.existsSync;
+
+module.exports = function (x, opts) {
+ if (core[x]) return x;
+
+ if (!opts) opts = {};
+ var isFile = opts.isFile || function (file) {
+ return existsSync(file) && fs.statSync(file).isFile()
+ };
+ var readFileSync = opts.readFileSync || fs.readFileSync;
+
+ var extensions = opts.extensions || [ '.js' ];
+ var y = opts.basedir
+ || path.dirname(require.cache[__filename].parent.filename)
+ ;
+
+ opts.paths = opts.paths || [];
+
+ if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
+ var m = loadAsFileSync(path.resolve(y, x))
+ || loadAsDirectorySync(path.resolve(y, x));
+ if (m) return m;
+ } else {
+ var n = loadNodeModulesSync(x, y);
+ if (n) return n;
+ }
+
+ throw new Error("Cannot find module '" + x + "'");
+
+ function loadAsFileSync (x) {
+ if (isFile(x)) {
+ return x;
+ }
+
+ for (var i = 0; i < extensions.length; i++) {
+ var file = x + extensions[i];
+ if (isFile(file)) {
+ return file;
+ }
+ }
+ }
+
+ function loadAsDirectorySync (x) {
+ var pkgfile = path.join(x, '/package.json');
+ if (isFile(pkgfile)) {
+ var body = readFileSync(pkgfile, 'utf8');
+ try {
+ var pkg = JSON.parse(body);
+ if (opts.packageFilter) {
+ pkg = opts.packageFilter(pkg, x);
+ }
+
+ if (pkg.main) {
+ var m = loadAsFileSync(path.resolve(x, pkg.main));
+ if (m) return m;
+ var n = loadAsDirectorySync(path.resolve(x, pkg.main));
+ if (n) return n;
+ }
+ }
+ catch (err) {}
+ }
+
+ return loadAsFileSync(path.join( x, '/index'));
+ }
+
+ function loadNodeModulesSync (x, start) {
+ var dirs = nodeModulesPathsSync(start);
+ for (var i = 0; i < dirs.length; i++) {
+ var dir = dirs[i];
+ var m = loadAsFileSync(path.join( dir, '/', x));
+ if (m) return m;
+ var n = loadAsDirectorySync(path.join( dir, '/', x ));
+ if (n) return n;
+ }
+ }
+
+ function nodeModulesPathsSync (start) {
+ var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
+ var parts = start.split(splitRe);
+
+ var dirs = [];
+ for (var i = parts.length - 1; i >= 0; i--) {
+ if (parts[i] === 'node_modules') continue;
+ var dir = path.join(
+ path.join.apply(path, parts.slice(0, i + 1)),
+ 'node_modules'
+ );
+ if (!parts[0].match(/([A-Za-z]:)/)) {
+ dir = '/' + dir;
+ }
+ dirs.push(dir);
+ }
+ return dirs.concat(opts.paths);
+ }
+};

0 comments on commit ba7038a

Please sign in to comment.
Something went wrong with that request. Please try again.