check-versions.js 1.4 KB

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