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.1.0 to 1.1.1

54

lib/rfr.js

@@ -14,3 +14,3 @@ /**

function coerceRoot(root) {
var coerced = root.toString().trim();
var coerced = root.substring().trim();
var len = coerced.length;

@@ -31,3 +31,3 @@ if (len > 0 && coerced[len - 1] !== '/') {

function normalizeId(idFromRoot, root) {
var id = idFromRoot.toString().trim();
var id = idFromRoot.substring().trim();
if (root && id.length > 0 && id[0] === '/') {

@@ -44,12 +44,54 @@ id = id.substring(1);

* @param {String} root The root for rfr require.
* @param {Boolean} isMaster Whether this is a master rfr.
* @private
*/
var createRfr = function(callable, root) {
var createRfr = function(callable, root, isMaster) {
rfr = callable.bind(callable);
/**
* A read-only property tells whether this rfr instance is a master one.
* Call `require('rfr')` to get a master rfr instance.
* User created rfr instances, such as `require('rfr')({ root: '...' })` are
* not master ones.
* @type {Boolean}
*/
Object.defineProperty(rfr, 'isMaster', {
configurable: false,
enumerable: true,
value: !!isMaster,
writable: false
});
/**
* The root of a rfr instance.
* @type {String}
*/
Object.defineProperty(rfr, 'root', {
configurable: false,
enumerable: true,
get: function() {
return callable.root;
},
set: function(root) {
callable.root = coerceRoot(root);
}
});
/**
* Set the root.
* @param {String} root The new root.
*/
rfr.setRoot = function(root) {
callable.root = coerceRoot(root);
this.root = root;
};
rfr.setRoot(root);
/**
* Get the filename that will be loaded when this rfr is called.
* @returns {String} module filename.
*/
rfr.resolve = function(idFromRoot) {
return require.resolve(normalizeId(idFromRoot, this.root));
};
rfr.root = root;
return rfr;

@@ -93,4 +135,4 @@ };

}
}, process.env.RFR_ROOT || process.env.PWD || process.cwd() || '/');
}, process.env.RFR_ROOT || process.env.PWD || process.cwd() || '/', true);
module.exports = rfr;

2

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

@@ -6,0 +6,0 @@ "license": "MIT",

node-rfr
========
**node-rfr** is a *<b>R</b>equire <b>F</b>rom project <b>R</b>oot* tool for Node.js.
**node-rfr** is a *<b>R</b>equire <b>F</b>rom (project) <b>R</b>oot* tool for
Node.js.
**node-rfr** allows you to require modules in your project with ```rfr('/lib/module1.js')``` instead of something like ```require('../../lib/module1.js')```.
**node-rfr** allows you to require modules in your project with
`rfr('lib/module1.js')` instead of something like
`require('../../lib/module1.js')`.

@@ -24,8 +27,9 @@ Install

|--run.js
|--lib
`--lib
|--module1.js
|--module2.js
`--module2.js
```
If we run ```run.js``` in the project folder, we can require modules relatively like this:
If we run `run.js` in the project folder, we can require modules relatively
like this:

@@ -41,3 +45,6 @@ ```bash

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:
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:

@@ -48,5 +55,14 @@ ```bash

Or set it with the following code:
Or set (or get) it with the `.root` property:
```javascript
var rfr = require('rfr');
rfr.root = '/usr/local'; // rfr adds a tailing slash if needed.
rfr.root; // Gets "/usr/local/"
```
Or set it with the `.setRoot()` function:
```javascript
var rfr = require('rfr');
rfr.setRoot('some_path');

@@ -57,32 +73,55 @@ ```

Details about Module Path
-------------------------
Use `.resolve()` to find the absolute path of a module without actually
importing it.
```
var rfr = require('rfr');
var path = rfr.resolve('models');
// Returns an absolute path, for example, "/project/lib/models/index.js"
```
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.
Sometimes you may want more than one RFR. For example, one for
"<project_root>/lib/" and one for "<project_root>/src/". Multi-version RFR
helps. In the following example, `rfr`, `rUsr` and `rEtc` could have different
roots.
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 rUsr = require('rfr')({
root: '/usr'
});
var rfr2 = require('rfr')({
root: '/include'
var rEtc = require('rfr')({
root: '/etc'
});
rfr.setRoot('/'); // Only changes the root of rfr
rfr.setRoot('/'); // Only changes the root of the master rfr
rfr('/module'); // Requires '/module'
rfr1('/module'); // Requires '/lib/module'
rfr2('/module'); // Requires '/include/module'
rUsr('/module'); // Requires '/usr/module'
rEtc('/module'); // Requires '/etc/module'
```
It is strongly recommended to use a versioned RFR in a project, or a module, that might be a dependency of another one.
You can use `.isMaster` property to check whether a RFR instance is the master
one.
```javascript
rfr.isMaster; // true
rUsr.isMaster; // false
rEtc.isMaster; // false
```
Change Log
----------
**2014-10-06 v1.1.0** Add multi-version RFR support.
**2014-10-24 v1.1.1** Adds `.root` and `.isMaster` and `.resolve()`.
**2014-10-07 v1.1.0** Adds multi-version RFR support.
**2014-05-01 v1.0.0** First release with require from root support.
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