1// we know it's global and/or not top, so the path has to be
2// {prefix}/node_modules/{name}.  Can't rely on pkg.name, because
3// it might be installed as an alias.
4
5const { dirname, basename } = require('path')
6// this gets called a lot and can't change, so memoize it
7const memo = new Map()
8module.exports = path => {
9  if (memo.has(path)) {
10    return memo.get(path)
11  }
12
13  const scopeOrNm = dirname(path)
14  const nm = basename(scopeOrNm) === 'node_modules' ? scopeOrNm
15    : dirname(scopeOrNm)
16
17  memo.set(path, nm)
18  return nm
19}
20