Skip to content

Commit

Permalink
Merge pull request #889 from electron-userland/prune-user-namespaced-…
Browse files Browse the repository at this point in the history
…modules

Prune user-namespaced modules
  • Loading branch information
malept committed Sep 13, 2018
2 parents a49c08d + 9506805 commit ce16805
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
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'))
)

0 comments on commit ce16805

Please sign in to comment.