Skip to content

Commit

Permalink
Add suport to ES6 import
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdouglas committed Sep 30, 2015
1 parent 5e2f0d9 commit 722456a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
var path = require( 'path' );

function rooted( pathTo ) {
function rooted( pathTo, onlyString ) {
var splitBy = '/';
var args;

if ( pathTo.search( /\\/ ) >= 0 ) {
pathTo = pathTo.replace( /\\/g, splitBy );
}

args = pathTo.split( splitBy );
args.unshift( __dirname );

var actualPath = path.join.apply( this, args );
var actualPath = __dirname + '/' + pathTo;
actualPath = actualPath.replace( '/node_modules/rooted', '' );

return require( actualPath );
if (onlyString) {
return actualPath;
} else {
return require( actualPath );
}
};

module.exports = rooted;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "rooted",
"version": "1.0.1",
"description": "Requiring modules/folders/files based on your root directory",
"version": "2.0.0",
"description": "Requiring or import modules/folders/files based on your root directory",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "make test"
},
"repository": {
"type": "git",
Expand Down
17 changes: 12 additions & 5 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var expect = require( 'chai' ).expect;
var path = require( 'path' );
var rooted = require( '../' );
var example = rooted( 'example' );
var folder = rooted( 'example\\folder' );
var expect = require( 'chai' ).expect;
var path = require( 'path' );
var rooted = require( '../' );
var example = rooted( 'example' );
var folder = rooted( 'example\\folder' );
var stringPath = rooted( 'path/to/folder', true );

describe( '#rooted', function() {

Expand All @@ -14,4 +15,10 @@ describe( '#rooted', function() {
expect( folder() ).to.equal( 'example/folder index.js' );
});

it('Should return a string for the path of the file', function() {
var testPath = __dirname + '/path/to/folder';
testPath = testPath.replace( '/test', '' );
expect( stringPath ).to.equal( testPath );
});

});

0 comments on commit 722456a

Please sign in to comment.