report-optional-failure.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. 'use strict'
  2. var path = require('path')
  3. var moduleName = require('../utils/module-name.js')
  4. module.exports = reportOptionalFailure
  5. function top (tree) {
  6. if (tree.parent) return top(tree.parent)
  7. return tree
  8. }
  9. function reportOptionalFailure (tree, what, error) {
  10. var topTree = top(tree)
  11. if (!topTree.warnings) topTree.warnings = []
  12. var id
  13. if (what) {
  14. var depVer = tree.package.dependencies && tree.package.dependencies[what]
  15. var optDepVer = tree.package.optionalDependencies && tree.package.optionalDependencies[what]
  16. var devDepVer = tree.package.devDependencies && tree.package.devDependencies[what]
  17. var version = depVer || optDepVer || devDepVer
  18. id = what + (version ? '@' + version : '')
  19. } else {
  20. id = tree._id || moduleName(tree) + (tree.package.version ? '@' + tree.package.version : '')
  21. }
  22. var location = path.relative(topTree.path, tree.path)
  23. if (what) location = path.join(location, 'node_modules', what)
  24. error.optional = id
  25. error.location = location
  26. topTree.warnings.push(error)
  27. }