versionNumberManager.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Dependecies.
  2. const fs = require('fs-extra');
  3. const path = require('path');
  4. const colorConsole = require("../../NodeHelpers/colorConsole");
  5. // Global Variables.
  6. const config = require("../../Config/config.js");
  7. const enginePath = path.join(config.core.computed.srcDirectory, "Engines/engine.ts");
  8. /**
  9. * Get the version from the engine class for Babylon
  10. */
  11. function getEngineVersion() {
  12. colorConsole.log("Get version from engine.ts");
  13. const engineContent = fs.readFileSync(enginePath).toString();
  14. const versionRegex = new RegExp(`public static get Version\\(\\): string {[\\s\\S]*return "([\\s\\S]*?)";[\\s\\S]*}`, "gm");
  15. const match = versionRegex.exec(engineContent);
  16. if (match && match.length) {
  17. const version = match[1];
  18. colorConsole.log("Version found: " + version.green);
  19. colorConsole.emptyLine();
  20. return version;
  21. }
  22. colorConsole.error("Version not found in engine.ts");
  23. process.exit(1);
  24. }
  25. /**
  26. * Update the version in the engine class for Babylon
  27. */
  28. function updateEngineVersion(newVersion) {
  29. colorConsole.log("Updating version in engine.ts to: " + newVersion.green);
  30. let engineContent = fs.readFileSync(enginePath).toString();
  31. let replaced = engineContent.replace(/(public static get Version\(\): string {\s*return ")(.*)(";\s*})/g, "$1" + newVersion + "$3");
  32. fs.writeFileSync(enginePath, replaced);
  33. colorConsole.emptyLine();
  34. }
  35. /**
  36. * Update the root package.json version
  37. */
  38. function updateRootPackageVersion(newVersion) {
  39. colorConsole.log("Updating version in /package.json to: " + newVersion.green);
  40. const packageJSONPath = config.core.computed.packageJSONPath;
  41. const packageJson = require(packageJSONPath);
  42. packageJson.version = newVersion;
  43. fs.writeFileSync(packageJSONPath, JSON.stringify(packageJson, null, 4));
  44. colorConsole.emptyLine();
  45. }
  46. /**
  47. * Main function driving the publication.
  48. */
  49. module.exports = {
  50. getEngineVersion,
  51. updateEngineVersion,
  52. updateRootPackageVersion
  53. };