check-versions.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. name: 'node',
  14. currentVersion: semver.clean(process.version),
  15. versionRequirement: packageConfig.engines.node
  16. }]
  17. if (shell.which('npm')) {
  18. versionRequirements.push({
  19. name: 'npm',
  20. currentVersion: exec('npm --version'),
  21. versionRequirement: packageConfig.engines.npm
  22. })
  23. }
  24. module.exports = function() {
  25. const warnings = []
  26. for (let i = 0; i < versionRequirements.length; i++) {
  27. const mod = versionRequirements[i]
  28. if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
  29. warnings.push(
  30. mod.name +
  31. ': ' +
  32. chalk.red(mod.currentVersion) +
  33. ' should be ' +
  34. chalk.green(mod.versionRequirement)
  35. )
  36. }
  37. }
  38. if (warnings.length) {
  39. console.log(
  40. chalk.yellow(
  41. 'To use this template, you must update following to modules:'
  42. )
  43. )
  44. for (let i = 0; i < warnings.length; i++) {
  45. const warning = warnings[i]
  46. console.log(' ' + warning)
  47. }
  48. console.log()
  49. process.exit(1)
  50. }
  51. }