validate-args.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict'
  2. var validate = require('aproba')
  3. var asyncMap = require('slide').asyncMap
  4. var chain = require('slide').chain
  5. var npmInstallChecks = require('npm-install-checks')
  6. var iferr = require('iferr')
  7. var checkEngine = npmInstallChecks.checkEngine
  8. var checkPlatform = npmInstallChecks.checkPlatform
  9. var npm = require('../npm.js')
  10. module.exports = function (idealTree, args, next) {
  11. validate('OAF', arguments)
  12. var force = npm.config.get('force')
  13. asyncMap(args, function (pkg, done) {
  14. chain([
  15. [hasMinimumFields, pkg],
  16. [checkSelf, idealTree, pkg, force],
  17. [isInstallable, idealTree, pkg]
  18. ], done)
  19. }, next)
  20. }
  21. function hasMinimumFields (pkg, cb) {
  22. if (pkg.name === '' || pkg.name == null) {
  23. return cb(new Error(`Can't install ${pkg._resolved}: Missing package name`))
  24. } else if (pkg.version === '' || pkg.version == null) {
  25. return cb(new Error(`Can't install ${pkg._resolved}: Missing package version`))
  26. } else {
  27. return cb()
  28. }
  29. }
  30. function setWarnings (idealTree, warn) {
  31. function top (tree) {
  32. if (tree.parent) return top(tree.parent)
  33. return tree
  34. }
  35. var topTree = top(idealTree)
  36. if (!topTree.warnings) topTree.warnings = []
  37. if (topTree.warnings.every(i => (
  38. i.code !== warn.code ||
  39. i.required !== warn.required ||
  40. i.pkgid !== warn.pkgid))) {
  41. topTree.warnings.push(warn)
  42. }
  43. }
  44. var isInstallable = module.exports.isInstallable = function (idealTree, pkg, next) {
  45. var force = npm.config.get('force')
  46. var nodeVersion = npm.config.get('node-version')
  47. if (/-/.test(nodeVersion)) {
  48. // for the purposes of validation, if the node version is a prerelease,
  49. // strip that. We check and warn about this sceanrio over in validate-tree.
  50. nodeVersion = nodeVersion.replace(/-.*/, '')
  51. }
  52. var strict = npm.config.get('engine-strict')
  53. checkEngine(pkg, npm.version, nodeVersion, force, strict, iferr(next, thenWarnEngineIssues))
  54. function thenWarnEngineIssues (warn) {
  55. if (idealTree && warn) setWarnings(idealTree, warn)
  56. checkPlatform(pkg, force, next)
  57. }
  58. }
  59. function checkSelf (idealTree, pkg, force, next) {
  60. if (idealTree.package && idealTree.package.name !== pkg.name) return next()
  61. if (force) {
  62. var warn = new Error("Wouldn't install " + pkg.name + ' as a dependency of itself, but being forced')
  63. warn.code = 'ENOSELF'
  64. idealTree.warnings.push(warn)
  65. next()
  66. } else {
  67. var er = new Error('Refusing to install package with name "' + pkg.name +
  68. '" under a package\n' +
  69. 'also called "' + pkg.name + '". Did you name your project the same\n' +
  70. 'as the dependency you\'re installing?\n\n' +
  71. 'For more information, see:\n' +
  72. ' <https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm>')
  73. er.code = 'ENOSELF'
  74. next(er)
  75. }
  76. }