From 950680510965b35df036f080045e56a5d9b9d174 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Wed, 12 Sep 2018 08:24:31 -0700 Subject: [PATCH] Prune user-namespaced modules --- .gitignore | 1 + prune.js | 8 +++++++- .../node_modules/@user/namespaced/package.json | 0 test/prune.js | 7 +++++-- 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/prune-is-module/node_modules/@user/namespaced/package.json diff --git a/.gitignore b/.gitignore index 515b1d87..cd9f8d61 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.pem node_modules test/fixtures/basic/main-link.js +!test/fixtures/prune-is-module/node_modules test/work .DS_Store .nyc_output diff --git a/prune.js b/prune.js index faf49042..a6b81551 100644 --- a/prune.js +++ b/prune.js @@ -56,10 +56,16 @@ class Pruner { } } +function isNodeModuleFolder (pathToCheck) { + return path.basename(path.dirname(pathToCheck)) === 'node_modules' || + // TODO: Change to startsWith in Node 6 + (path.basename(path.dirname(pathToCheck))[0] === '@' && path.basename(path.resolve(pathToCheck, `..${path.sep}..`)) === 'node_modules') +} + module.exports = { isModule: function isModule (pathToCheck) { return fs.pathExists(path.join(pathToCheck, 'package.json')) - .then(exists => exists && path.basename(path.dirname(pathToCheck)) === 'node_modules') + .then(exists => exists && isNodeModuleFolder(pathToCheck)) }, Pruner: Pruner } diff --git a/test/fixtures/prune-is-module/node_modules/@user/namespaced/package.json b/test/fixtures/prune-is-module/node_modules/@user/namespaced/package.json new file mode 100644 index 00000000..e69de29b diff --git a/test/prune.js b/test/prune.js index 42d9bb13..6a297244 100644 --- a/test/prune.js +++ b/test/prune.js @@ -56,10 +56,13 @@ util.testSinglePlatform('prune electron in dependencies', (t, baseOpts) => { util.testSinglePlatform('prune: false test', createPruneOptionTest, false, 'package.json devDependency should exist under app/node_modules') -test('isModule only detects modules inside a node_modules parent folder', t => +test('isModule properly detects module folders', t => prune.isModule(util.fixtureSubdir(path.join('prune-is-module', 'node_modules', 'module'))) .then(isModule => { t.true(isModule, 'module folder should be detected as module') return prune.isModule(util.fixtureSubdir(path.join('prune-is-module', 'node_modules', 'module', 'not-module'))) - }).then(isModule => t.false(isModule, 'not-module folder should not be detected as module')) + }).then(isModule => { + t.false(isModule, 'not-module subfolder should not be detected as module') + return prune.isModule(util.fixtureSubdir(path.join('prune-is-module', 'node_modules', '@user', 'namespaced'))) + }).then(isModule => t.true(isModule, '@user/namespaced folder should be detected as module')) )