split-package-names.js 594 B

1234567891011121314151617181920212223
  1. 'use strict'
  2. const splitPackageNames = (path) => {
  3. return path.split('/')
  4. // combine scoped parts
  5. .reduce((parts, part) => {
  6. if (parts.length === 0)
  7. return [part]
  8. const lastPart = parts[parts.length - 1]
  9. // check if previous part is the first part of a scoped package
  10. if (lastPart[0] === '@' && !lastPart.includes('/'))
  11. parts[parts.length - 1] += '/' + part
  12. else
  13. parts.push(part)
  14. return parts
  15. }, [])
  16. .join('/node_modules/')
  17. .replace(/(\/node_modules)+/, '/node_modules')
  18. }
  19. module.exports = splitPackageNames