Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rfr

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rfr - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

96

lib/rfr.js
/**
* The main file of node-rfr.
*
*
* @author Su Su <s@warmsea.net>
*/
var root = '/';
/**
* Trim a root and add tailing slash if not exists.
* @param {String} root A root.
* @returns {String} The coerced root.
* @private
*/
function coerceRoot(root) {
var coerced = root.toString().trim();
var len = coerced.length;
if (len > 0 && coerced[len - 1] !== '/') {
coerced = coerced + '/';
}
return coerced;
}
var rfr = module.exports = function(idFromRoot) {
var id = idFromRoot.toString();
var len = id.length;
if (len > 0 && id[0] !== '/') {
id = '/' + id;
/**
* Return a module id which can be used by the builtin `require()`.
* @param {String} idFromRoot A RFR module id.
* @param {String} root The root.
* @returns {String} A standard module id.
* @private
*/
function normalizeId(idFromRoot, root) {
var id = idFromRoot.toString().trim();
if (root && id.length > 0 && id[0] === '/') {
id = id.substring(1);
}
id = root + id;
return require(id);
};
return id;
}
rfr.setRoot = function(newRoot) {
root = coerceRoot(newRoot);
/**
* Create a new version of rfr with a function.
* @param {Function} callable The rfr require function.
* @param {String} root The root for rfr require.
* @private
*/
var createRfr = function(callable, root) {
rfr = callable.bind(callable);
rfr.setRoot = function(root) {
callable.root = coerceRoot(root);
};
rfr.setRoot(root);
return rfr;
};
function coerceRoot(root) {
var coerced = root.toString();
var len = coerced.length;
if (len > 0 && coerced[len - 1] === '/') {
coerced = coerced.substring(0, len - 1);
/**
* Create a new version of rfr.
* @param {{root:String}} config config of this version.
* @returns {rfr} a new rfr version.
* @private
*/
function createVersion(config) {
if (!(config || (typeof config.root !== 'string'))) {
throw new Error('"config.root" is required and must be a string');
}
return coerced;
return createRfr(function(idFromRoot) {
if (typeof idFromRoot !== 'string') {
throw new Error('A string is required for the argument of ' +
'a user created RFR version.');
}
return require(normalizeId(idFromRoot, this.root));
}, config.root);
}
rfr.setRoot(process.env.RFR_ROOT || process.env.PWD || process.cwd() || '/');
/**
* Do the RFR require action, or create a new version of RFR.
* If a string is passed as the argument, this function will do the RFR require
* action. Else, this function will create a new version of RFR with the
* argument as config.
* @function
* @param {String|Object} idFromRoot Module id or RFR config.
* @returns {Module|RFR} A module or a RFR version.
*/
var rfr = createRfr(function(idFromRoot) {
if (typeof idFromRoot === 'string') {
return require(normalizeId(idFromRoot, this.root));
} else {
return createVersion(idFromRoot);
}
}, process.env.RFR_ROOT || process.env.PWD || process.cwd() || '/');
module.exports = rfr;

5

package.json
{
"name": "rfr",
"author": "Su Su <s@warmsea.net>",
"version": "1.0.0",
"version": "1.1.0",
"description": "Require From project Root tool for Node.js.",

@@ -10,3 +10,4 @@ "license": "MIT",

"rfr",
"require"
"require",
"root"
],

@@ -13,0 +14,0 @@ "homepage": "https://github.com/warmsea/node-rfr",

@@ -18,3 +18,3 @@ node-rfr

Suppose we have a project structed as the following:
Suppose we have a project with the following structure:

@@ -35,7 +35,7 @@ ```

var module1 = rfr('/lib/module1');
var module2 = rfr('/lib/module2');
var module2 = rfr('lib/module2'); // Leading slash can be omitted.
```
Customize the Root
------------
------------------

@@ -54,2 +54,34 @@ By default, the root path is the current working path where you run the program. If you want to use another path as the root, set it to the environment variable named ```RFR_ROOT``` before you require **node-rfr**. For example, run the program like this:

The root should be an absolute path. Maybe you want to use ```__dirname```.
An absolute path is preferred for the root. Maybe you want to use `__dirname`.
Multi-version RFR
-----------------
If you want to use RFR in your module, and want to publish it to NPM. It is possible that a project depends on your module is also using RFR. And if that project changes the RFR root, your module might fail.
Multi-version RFR helps. In the following example, `rfr`, `rfr1` and `rfr2` could have different roots.
```javascript
var rfr = require('rfr');
var rfr1 = require('rfr')({
root: '/lib'
});
var rfr2 = require('rfr')({
root: '/include'
});
rfr.setRoot('/'); // Only changes the root of rfr
rfr('/module'); // Requires '/module'
rfr1('/module'); // Requires '/lib/module'
rfr2('/module'); // Requires '/include/module'
```
It is strongly recommended to use a versioned RFR in a project, or a module, that might be a dependency of another one.
Change Log
----------
**2014-10-06 v1.1.0** Add multi-version RFR support.
**2014-05-01 v1.0.0** First release with require from root support.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc