get-version.js 1.2 KB

123456789101112131415161718192021222324252627282930
  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 (error) {
  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. // Most win32 machines have python.exe but no python3.exe.
  21. const python = process.platform === 'win32' ? 'python.exe' : 'python3';
  22. // Get the version from git tag if it is not defined in gn args.
  23. const output = spawnSync(python, [path.join(ELECTRON_DIR, 'script', 'get-git-version.py')]);
  24. if (output.status !== 0) {
  25. throw new Error(`Failed to get git tag, script quit with ${output.status}: ${output.stdout}`);
  26. }
  27. return output.stdout.toString().trim();
  28. };