Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prune user-namespaced modules #889

Merged
merged 1 commit into from Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion prune.js
Expand Up @@ -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
}
Empty file.
7 changes: 5 additions & 2 deletions test/prune.js
Expand Up @@ -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'))
)