From b8a33367c96d8e0e5356e21ab6a690d250c4d6a1 Mon Sep 17 00:00:00 2001 From: Manuel Di Iorio Date: Tue, 5 Sep 2017 22:09:16 +0200 Subject: [PATCH 1/2] Add optional options parameter This parameter accepts an optional setting forceRoot: boolean, which allows to force the provided root path without searching for a package.json file --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e4cf932..ff91ff8 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,9 @@ function pkgRequireFactory(packageDir) { return requireInPkg; } -function createInstance(currentDirectory) { +function createInstance(currentDirectory, options) { + options = options || {}; + if ( !currentDirectory || typeof currentDirectory !== 'string' @@ -52,7 +54,7 @@ function createInstance(currentDirectory) { ); } - var packageDir = findPackageDir(currentDirectory); + var packageDir = !options.forceRoot ? findPackageDir(currentDirectory) : currentDirectory; return pkgRequireFactory(packageDir); } From 43f90990fa982bc8c0be3af744c90ee8b19614c4 Mon Sep 17 00:00:00 2001 From: Manuel Di Iorio Date: Tue, 5 Sep 2017 22:23:36 +0200 Subject: [PATCH 2/2] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d395865..41d0538 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,14 @@ now you can go write useful code instead of counting how many levels you need to ### `require` create a new instance for the package based on current file directory ```javascript -const pkgRequire = require('pkg-require')(__dirname); +const pkgRequire = require('pkg-require')(__dirname, options); ``` +use the optional options object to provide the following additional settings + +| option | type | default | description | +| --------- | ------- | ------- | ----------- | +| forceRoot | boolean | false | directly use the root path, without looking for a package.json file | + ### `pkgRequire()` require a file by passing its path relative to the directory containing `package.json`