version.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {exec} from 'child_process';
  2. import fs from 'fs';
  3. import path from 'path';
  4. /* eslint no-console: 0 */
  5. const pkg = require(path.join(__dirname, '../package.json'));
  6. /**
  7. * Determines whether or not the project has the CHANGELOG setup by checking
  8. * for the presence of a CHANGELOG.md file and the necessary dependency and
  9. * npm script.
  10. *
  11. * @return {Boolean}
  12. */
  13. const hasChangelog = () => {
  14. try {
  15. fs.statSync(path.join(__dirname, '../CHANGELOG.md'));
  16. } catch (x) {
  17. return false;
  18. }
  19. return pkg.devDependencies.hasOwnProperty('chg') &&
  20. pkg.scripts.hasOwnProperty('change');
  21. };
  22. /**
  23. * Determines whether or not the project has the Bower setup by checking for
  24. * the presence of a bower.json file.
  25. *
  26. * @return {Boolean}
  27. */
  28. const hasBower = () => {
  29. try {
  30. fs.statSync(path.join(__dirname, '../bower.json'));
  31. return true;
  32. } catch (x) {
  33. return false;
  34. }
  35. };
  36. const commands = [];
  37. // If the project has a CHANGELOG, update it for the new release.
  38. if (hasChangelog()) {
  39. commands.push(`chg release "${pkg.version}"`);
  40. commands.push('git add CHANGELOG.md');
  41. }
  42. // If the project supports Bower, perform special extra versioning step.
  43. if (hasBower()) {
  44. commands.push('git add package.json');
  45. commands.push(`git commit -m "${pkg.version}"`);
  46. // We only need a build in the Bower-supported case because of the
  47. // temporary addition of the dist/ directory.
  48. commands.push('npm run build');
  49. commands.push('git add -f dist');
  50. }
  51. if (commands.length) {
  52. exec(commands.join(' && '), (err, stdout, stderr) => {
  53. if (err) {
  54. process.stdout.write(err.stack);
  55. process.exit(err.status || 1);
  56. } else {
  57. process.stdout.write(stdout);
  58. }
  59. });
  60. }