From ae530efb1c9f5465fa785430d5e35c3c2cca9d2f Mon Sep 17 00:00:00 2001 From: jlaramie Date: Thu, 29 Jun 2017 11:11:05 -0400 Subject: [PATCH 1/2] Fixed CWD 'node_modules' check for namespaced packages --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0c6ec65..ba59aea 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,16 @@ function postinstallBuild () { // any helpful environment variables to indicate whether we're being // installed as a dependency or not. The best we can do is check whether // the parent directory is `node_modules`. - var isDependency = path.basename(path.dirname(CWD)) === 'node_modules' + var splitName = process.env.npm_package_name.split('/') + // The second part of this condition is for handling packages that are namespaced. + // If we detect the delimiter then we try and figure out how deep we are in and + // check for node_modules again. + var isDependency = path.basename(path.dirname(CWD)) === 'node_modules' || + (splitName.length > 1 && + path.basename(path.join.apply(path, [CWD].concat( + splitName.map(function () { + return '..' + })))) === 'node_modules') if (flags.onlyAsDependency && !isDependency) { log.info( From e76662cdc7b0c45b72af0306edf9eec331913964 Mon Sep 17 00:00:00 2001 From: jlaramie Date: Fri, 7 Jul 2017 09:54:40 -0400 Subject: [PATCH 2/2] Cleaned up the CWD 'node_modules' parent check --- index.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index ba59aea..31c417c 100644 --- a/index.js +++ b/index.js @@ -177,17 +177,10 @@ function postinstallBuild () { // dependency, we should always prune. Unfortunately, npm doesn't set // any helpful environment variables to indicate whether we're being // installed as a dependency or not. The best we can do is check whether - // the parent directory is `node_modules`. - var splitName = process.env.npm_package_name.split('/') - // The second part of this condition is for handling packages that are namespaced. - // If we detect the delimiter then we try and figure out how deep we are in and - // check for node_modules again. - var isDependency = path.basename(path.dirname(CWD)) === 'node_modules' || - (splitName.length > 1 && - path.basename(path.join.apply(path, [CWD].concat( - splitName.map(function () { - return '..' - })))) === 'node_modules') + // the parent directory is `node_modules` but the package can be nested + // in multiple parent directories so we split apart the name of the module. + var parentDirs = CWD.split(path.sep) + var isDependency = parentDirs.indexOf('node_modules') !== -1 if (flags.onlyAsDependency && !isDependency) { log.info(