root-require
Advanced tools
Comparing version 0.1.0 to 0.2.0
12
index.js
@@ -15,4 +15,12 @@ /** | ||
*/ | ||
module.exports = function requireFromModuleRoot ( relativePathFromModuleRoot ) { | ||
function requireFromModuleRoot ( relativePathFromModuleRoot ) { | ||
return require(path.join(packpath.parent(),relativePathFromModuleRoot)); | ||
}; | ||
} | ||
/** | ||
* Provide access to packpath directly | ||
* @type {Object} | ||
*/ | ||
requireFromModuleRoot.packpath = packpath; | ||
module.exports = requireFromModuleRoot; |
{ | ||
"name": "root-require", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "require() using a relative path from the root directory of the present module", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,6 +13,30 @@ root-require | ||
## Use Cases | ||
1. A file needs to move, and you want to easily be able to find/replace the references to it. | ||
## Usage | ||
Just once: | ||
```javascript | ||
var Sails = require('root-require')('lib/app'); | ||
``` | ||
More than once: | ||
```javascript | ||
var rootRequire = require('root-require'); | ||
var Sails = rootRequire('lib/app'); | ||
var Router = rootRequire('lib/router'); | ||
var MiddlewareLibrary = rootRequire('lib/middleware'); | ||
``` | ||
## Why is this a good thing? | ||
It's easier to reason about the structure of your module when the paths are consistent. The structure of your project becomes more declarative- dependencies are consistently referenced, irrespective of the user file's home in the directory structure. | ||
#### Problems w/ `require()` | ||
1. When you move a dependency file (_A<sub>x</sub>_) required by multiple files (_B<sub>i</sub>_), you have to find/replace the all references to _A<sub>x</sub>_. This is normally hard, because the argument to the `require(...)` function depends on where the user file (_B<sub>i</sub>_) is located. | ||
2. When you move a file (_B<sub>x</sub>_) which depends on another file (_A<sub>x</sub>_), you normally have to update the `require()` call in _B<sub>x</sub>_ to reflect the new relative path from _B<sub>x</sub>_ to _A<sub>x</sub>_. | ||
e.g. Consider trying to change the path to `giggle.js` in an automated way: | ||
@@ -49,21 +73,8 @@ | ||
## Usage | ||
Just once: | ||
```javascript | ||
var Sails = require('root-require')('lib/app'); | ||
``` | ||
More than once: | ||
```javascript | ||
var rootRequire = require('root-require'); | ||
var Sails = rootRequire('lib/app'); | ||
var Router = rootRequire('lib/router'); | ||
var MiddlewareLibrary = rootRequire('lib/middleware'); | ||
``` | ||
## Credit where credit is due | ||
This is mainly a thin wrapper around `packpath` (https://github.com/jprichardson/node-packpath)-- this module just exists so we can do it in one line. | ||
This module is literally a 3-line wrapper around the awesome `packpath` module (https://github.com/jprichardson/node-packpath). I just made this for convenience/ so I could have it in one line because I always forget how `path.join` works w/ Windows and all that. | ||
@@ -70,0 +81,0 @@ |
5022
19
83