get-version.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. const { spawnSync } = require('node:child_process');
  2. const fs = require('node:fs');
  3. const path = require('node:path');
  4. const { ELECTRON_DIR, getOutDir } = require('./utils');
  5. // Print the value of electron_version set in gn config.
  6. module.exports.getElectronVersion = () => {
  7. // Read the override_electron_version from args.gn file.
  8. try {
  9. const outDir = path.resolve(ELECTRON_DIR, '..', 'out', getOutDir());
  10. const content = fs.readFileSync(path.join(outDir, 'args.gn'));
  11. const regex = /override_electron_version\s*=\s*["']([^"']+)["']/;
  12. const match = content.toString().match(regex);
  13. if (match) {
  14. return match[1];
  15. }
  16. } catch {
  17. // Error may happen when trying to get version before running gn, which is a
  18. // valid case and error will be ignored.
  19. }
  20. // Get the version from git tag if it is not defined in gn args.
  21. const output = spawnSync('python3', [path.join(ELECTRON_DIR, 'script', 'get-git-version.py')]);
  22. if (output.status !== 0) {
  23. throw new Error(`Failed to get git tag, script quit with ${output.status}: ${output.stdout}`);
  24. }
  25. return output.stdout.toString().trim();
  26. };