check-versions.js 1.3 KB

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