check-versions.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var semver = require('semver')
  2. var chalk = require('chalk')
  3. var packageConfig = require('../package.json')
  4. var exec = function (cmd) {
  5. return require('child_process')
  6. .execSync(cmd).toString().trim()
  7. }
  8. var versionRequirements = [
  9. {
  10. name: 'node',
  11. currentVersion: semver.clean(process.version),
  12. versionRequirement: packageConfig.engines.node
  13. },
  14. {
  15. name: 'npm',
  16. currentVersion: exec('npm --version'),
  17. versionRequirement: packageConfig.engines.npm
  18. }
  19. ]
  20. module.exports = function () {
  21. var warnings = []
  22. for (var i = 0; i < versionRequirements.length; i++) {
  23. var mod = versionRequirements[i]
  24. if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
  25. warnings.push(mod.name + ': ' +
  26. chalk.red(mod.currentVersion) + ' should be ' +
  27. chalk.green(mod.versionRequirement)
  28. )
  29. }
  30. }
  31. if (warnings.length) {
  32. console.log('')
  33. console.log(chalk.yellow('To use this template, you must update following to modules:'))
  34. console.log()
  35. for (var i = 0; i < warnings.length; i++) {
  36. var warning = warnings[i]
  37. console.log(' ' + warning)
  38. }
  39. console.log()
  40. process.exit(1)
  41. }
  42. }