check-versions.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 +
  33. ': ' +
  34. chalk.red(mod.currentVersion) +
  35. ' should be ' +
  36. chalk.green(mod.versionRequirement)
  37. )
  38. }
  39. }
  40. if (warnings.length) {
  41. console.log('')
  42. console.log(
  43. chalk.yellow(
  44. 'To use this template, you must update following to modules:'
  45. )
  46. )
  47. console.log()
  48. for (let i = 0; i < warnings.length; i++) {
  49. const warning = warnings[i]
  50. console.log(' ' + warning)
  51. }
  52. console.log()
  53. process.exit(1)
  54. }
  55. }