utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const { GitProcess } = require('dugite');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
  5. const SRC_DIR = path.resolve(ELECTRON_DIR, '..');
  6. const RELEASE_BRANCH_PATTERN = /(\d)+-(?:(?:[0-9]+-x$)|(?:x+-y$))/;
  7. // TODO(main-migration): Simplify once main branch is renamed
  8. const MAIN_BRANCH_PATTERN = /^(main|master)$/;
  9. const ORIGIN_MAIN_BRANCH_PATTERN = /^origin\/(main|master)$/;
  10. require('colors');
  11. const pass = '✓'.green;
  12. const fail = '✗'.red;
  13. function getElectronExec () {
  14. const OUT_DIR = getOutDir();
  15. switch (process.platform) {
  16. case 'darwin':
  17. return `out/${OUT_DIR}/Electron.app/Contents/MacOS/Electron`;
  18. case 'win32':
  19. return `out/${OUT_DIR}/electron.exe`;
  20. case 'linux':
  21. return `out/${OUT_DIR}/electron`;
  22. default:
  23. throw new Error('Unknown platform');
  24. }
  25. }
  26. function getOutDir (options = {}) {
  27. const shouldLog = options.shouldLog || false;
  28. const presetDirs = ['Testing', 'Release', 'Default', 'Debug'];
  29. if (options.outDir || process.env.ELECTRON_OUT_DIR) {
  30. const outDir = options.outDir || process.env.ELECTRON_OUT_DIR;
  31. const outPath = path.resolve(SRC_DIR, 'out', outDir);
  32. // Check that user-set variable is a valid/existing directory
  33. if (fs.existsSync(outPath)) {
  34. if (shouldLog) console.log(`OUT_DIR is: ${outDir}`);
  35. return outDir;
  36. }
  37. // Throw error if user passed/set nonexistent directory.
  38. throw new Error(`${outDir} directory not configured on your machine.`);
  39. } else {
  40. for (const buildType of presetDirs) {
  41. const outPath = path.resolve(SRC_DIR, 'out', buildType);
  42. if (fs.existsSync(outPath)) {
  43. if (shouldLog) console.log(`OUT_DIR is: ${buildType}`);
  44. return buildType;
  45. }
  46. }
  47. }
  48. // If we got here, it means process.env.ELECTRON_OUT_DIR was not
  49. // set and none of the preset options could be found in /out, so throw
  50. throw new Error(`No valid out directory found; use one of ${presetDirs.join(',')} or set process.env.ELECTRON_OUT_DIR`);
  51. }
  52. function getAbsoluteElectronExec () {
  53. return path.resolve(SRC_DIR, getElectronExec());
  54. }
  55. async function handleGitCall (args, gitDir) {
  56. const details = await GitProcess.exec(args, gitDir);
  57. if (details.exitCode === 0) {
  58. return details.stdout.replace(/^\*|\s+|\s+$/, '');
  59. } else {
  60. const error = GitProcess.parseError(details.stderr);
  61. console.log(`${fail} couldn't parse git process call: `, error);
  62. process.exit(1);
  63. }
  64. }
  65. async function getCurrentBranch (gitDir) {
  66. let branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], gitDir);
  67. if (!MAIN_BRANCH_PATTERN.test(branch) && !RELEASE_BRANCH_PATTERN.test(branch)) {
  68. const lastCommit = await handleGitCall(['rev-parse', 'HEAD'], gitDir);
  69. const branches = (await handleGitCall([
  70. 'branch',
  71. '--contains',
  72. lastCommit,
  73. '--remote'
  74. ], gitDir)).split('\n');
  75. branch = branches.find(b => MAIN_BRANCH_PATTERN.test(b.trim()) || ORIGIN_MAIN_BRANCH_PATTERN.test(b.trim()) || RELEASE_BRANCH_PATTERN.test(b.trim()));
  76. if (!branch) {
  77. console.log(`${fail} no release branch exists for this ref`);
  78. process.exit(1);
  79. }
  80. if (branch.startsWith('origin/')) branch = branch.substr('origin/'.length);
  81. }
  82. return branch.trim();
  83. }
  84. module.exports = {
  85. getCurrentBranch,
  86. getElectronExec,
  87. getOutDir,
  88. getAbsoluteElectronExec,
  89. ELECTRON_DIR,
  90. SRC_DIR
  91. };