utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const { GitProcess } = require('dugite');
  2. const fs = require('node:fs');
  3. const klaw = require('klaw');
  4. const os = require('node:os');
  5. const path = require('node:path');
  6. const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
  7. const SRC_DIR = path.resolve(ELECTRON_DIR, '..');
  8. require('colors');
  9. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  10. const pass = '✓'.green;
  11. const fail = '✗'.red;
  12. function getElectronExec () {
  13. const OUT_DIR = getOutDir();
  14. switch (process.platform) {
  15. case 'darwin':
  16. return `out/${OUT_DIR}/Electron.app/Contents/MacOS/Electron`;
  17. case 'win32':
  18. return `out/${OUT_DIR}/electron.exe`;
  19. case 'linux':
  20. return `out/${OUT_DIR}/electron`;
  21. default:
  22. throw new Error('Unknown platform');
  23. }
  24. }
  25. function getOutDir (options = {}) {
  26. const shouldLog = options.shouldLog || false;
  27. const presetDirs = ['Testing', 'Release', 'Default', 'Debug'];
  28. if (options.outDir || process.env.ELECTRON_OUT_DIR) {
  29. const outDir = options.outDir || process.env.ELECTRON_OUT_DIR;
  30. const outPath = path.resolve(SRC_DIR, 'out', outDir);
  31. // Check that user-set variable is a valid/existing directory
  32. if (fs.existsSync(outPath)) {
  33. if (shouldLog) console.log(`OUT_DIR is: ${outDir}`);
  34. return outDir;
  35. }
  36. // Throw error if user passed/set nonexistent directory.
  37. throw new Error(`${outDir} directory not configured on your machine.`);
  38. } else {
  39. for (const buildType of presetDirs) {
  40. const outPath = path.resolve(SRC_DIR, 'out', buildType);
  41. if (fs.existsSync(outPath)) {
  42. if (shouldLog) console.log(`OUT_DIR is: ${buildType}`);
  43. return buildType;
  44. }
  45. }
  46. }
  47. // If we got here, it means process.env.ELECTRON_OUT_DIR was not
  48. // set and none of the preset options could be found in /out, so throw
  49. throw new Error(`No valid out directory found; use one of ${presetDirs.join(',')} or set process.env.ELECTRON_OUT_DIR`);
  50. }
  51. function getAbsoluteElectronExec () {
  52. return path.resolve(SRC_DIR, getElectronExec());
  53. }
  54. async function handleGitCall (args, gitDir) {
  55. const details = await GitProcess.exec(args, gitDir);
  56. if (details.exitCode === 0) {
  57. return details.stdout.replace(/^\*|\s+|\s+$/, '');
  58. } else {
  59. const error = GitProcess.parseError(details.stderr);
  60. console.log(`${fail} couldn't parse git process call: `, error);
  61. process.exit(1);
  62. }
  63. }
  64. async function getCurrentBranch (gitDir) {
  65. const RELEASE_BRANCH_PATTERN = /^\d+-x-y$/;
  66. const MAIN_BRANCH_PATTERN = /^main$/;
  67. const ORIGIN_MAIN_BRANCH_PATTERN = /^origin\/main$/;
  68. let branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], gitDir);
  69. if (!MAIN_BRANCH_PATTERN.test(branch) && !RELEASE_BRANCH_PATTERN.test(branch)) {
  70. const lastCommit = await handleGitCall(['rev-parse', 'HEAD'], gitDir);
  71. const branches = (await handleGitCall([
  72. 'branch',
  73. '--contains',
  74. lastCommit,
  75. '--remote'
  76. ], gitDir)).split('\n');
  77. branch = branches.find(b => MAIN_BRANCH_PATTERN.test(b.trim()) || ORIGIN_MAIN_BRANCH_PATTERN.test(b.trim()) || RELEASE_BRANCH_PATTERN.test(b.trim()));
  78. if (!branch) {
  79. console.log(`${fail} no release branch exists for this ref`);
  80. process.exit(1);
  81. }
  82. if (branch.startsWith('origin/')) branch = branch.substr('origin/'.length);
  83. }
  84. return branch.trim();
  85. }
  86. function chunkFilenames (filenames, offset = 0) {
  87. // Windows has a max command line length of 2047 characters, so we can't
  88. // provide too many filenames without going over that. To work around that,
  89. // chunk up a list of filenames such that it won't go over that limit when
  90. // used as args. Other platforms may have higher limits, but 4095 might be
  91. // the limit on Linux systems according to `termios(3)`, so cap it there.
  92. const MAX_FILENAME_ARGS_LENGTH =
  93. (os.platform() === 'win32' ? 2047 : 4095) - offset;
  94. return filenames.reduce(
  95. (chunkedFilenames, filename) => {
  96. const currChunk = chunkedFilenames[chunkedFilenames.length - 1];
  97. const currChunkLength = currChunk.reduce(
  98. (totalLength, _filename) => totalLength + _filename.length + 1,
  99. 0
  100. );
  101. if (currChunkLength + filename.length + 1 > MAX_FILENAME_ARGS_LENGTH) {
  102. chunkedFilenames.push([filename]);
  103. } else {
  104. currChunk.push(filename);
  105. }
  106. return chunkedFilenames;
  107. },
  108. [[]]
  109. );
  110. }
  111. /**
  112. * @param {string} top
  113. * @param {(filename: string) => boolean} test
  114. * @returns {Promise<string[]>}
  115. */
  116. async function findMatchingFiles (top, test) {
  117. return new Promise(resolve => {
  118. const matches = [];
  119. klaw(top, {
  120. filter: f => path.basename(f) !== '.bin'
  121. })
  122. .on('end', () => resolve(matches))
  123. .on('data', item => {
  124. if (test(item.path)) {
  125. matches.push(item.path);
  126. }
  127. });
  128. });
  129. }
  130. module.exports = {
  131. chunkFilenames,
  132. findMatchingFiles,
  133. getCurrentBranch,
  134. getElectronExec,
  135. getOutDir,
  136. getAbsoluteElectronExec,
  137. handleGitCall,
  138. ELECTRON_DIR,
  139. SRC_DIR
  140. };