2018-12-03 21:28:10 +00:00
|
|
|
const { GitProcess } = require('dugite');
|
2023-06-22 14:21:42 +00:00
|
|
|
const fs = require('node:fs');
|
|
|
|
const os = require('node:os');
|
|
|
|
const path = require('node:path');
|
2018-09-13 16:57:39 +00:00
|
|
|
|
2019-06-24 17:18:04 +00:00
|
|
|
const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
|
|
|
|
const SRC_DIR = path.resolve(ELECTRON_DIR, '..');
|
2019-06-05 23:30:39 +00:00
|
|
|
|
2018-12-03 21:28:10 +00:00
|
|
|
require('colors');
|
2023-06-26 09:51:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2019-08-29 14:46:54 +00:00
|
|
|
const pass = '✓'.green;
|
|
|
|
const fail = '✗'.red;
|
2018-12-03 21:28:10 +00:00
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
function getElectronExec () {
|
2019-08-21 17:41:35 +00:00
|
|
|
const OUT_DIR = getOutDir();
|
2018-09-13 16:57:39 +00:00
|
|
|
switch (process.platform) {
|
|
|
|
case 'darwin':
|
|
|
|
return `out/${OUT_DIR}/Electron.app/Contents/MacOS/Electron`;
|
|
|
|
case 'win32':
|
|
|
|
return `out/${OUT_DIR}/electron.exe`;
|
|
|
|
case 'linux':
|
|
|
|
return `out/${OUT_DIR}/electron`;
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown platform');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:00:42 +00:00
|
|
|
function getOutDir (options = {}) {
|
|
|
|
const shouldLog = options.shouldLog || false;
|
2020-02-27 19:32:09 +00:00
|
|
|
const presetDirs = ['Testing', 'Release', 'Default', 'Debug'];
|
2019-12-17 22:00:42 +00:00
|
|
|
|
|
|
|
if (options.outDir || process.env.ELECTRON_OUT_DIR) {
|
|
|
|
const outDir = options.outDir || process.env.ELECTRON_OUT_DIR;
|
|
|
|
const outPath = path.resolve(SRC_DIR, 'out', outDir);
|
|
|
|
|
|
|
|
// Check that user-set variable is a valid/existing directory
|
|
|
|
if (fs.existsSync(outPath)) {
|
|
|
|
if (shouldLog) console.log(`OUT_DIR is: ${outDir}`);
|
|
|
|
return outDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw error if user passed/set nonexistent directory.
|
|
|
|
throw new Error(`${outDir} directory not configured on your machine.`);
|
2019-08-21 17:41:35 +00:00
|
|
|
} else {
|
2020-02-27 19:32:09 +00:00
|
|
|
for (const buildType of presetDirs) {
|
2019-08-21 17:41:35 +00:00
|
|
|
const outPath = path.resolve(SRC_DIR, 'out', buildType);
|
|
|
|
if (fs.existsSync(outPath)) {
|
|
|
|
if (shouldLog) console.log(`OUT_DIR is: ${buildType}`);
|
|
|
|
return buildType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 19:32:09 +00:00
|
|
|
|
|
|
|
// If we got here, it means process.env.ELECTRON_OUT_DIR was not
|
|
|
|
// set and none of the preset options could be found in /out, so throw
|
|
|
|
throw new Error(`No valid out directory found; use one of ${presetDirs.join(',')} or set process.env.ELECTRON_OUT_DIR`);
|
2019-08-21 17:41:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
function getAbsoluteElectronExec () {
|
2019-06-24 17:18:04 +00:00
|
|
|
return path.resolve(SRC_DIR, getElectronExec());
|
2018-09-13 16:57:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 03:56:43 +00:00
|
|
|
async function handleGitCall (args, gitDir) {
|
|
|
|
const details = await GitProcess.exec(args, gitDir);
|
|
|
|
if (details.exitCode === 0) {
|
2019-06-20 20:12:05 +00:00
|
|
|
return details.stdout.replace(/^\*|\s+|\s+$/, '');
|
2018-12-03 21:28:10 +00:00
|
|
|
} else {
|
2019-06-17 03:56:43 +00:00
|
|
|
const error = GitProcess.parseError(details.stderr);
|
|
|
|
console.log(`${fail} couldn't parse git process call: `, error);
|
2018-12-03 21:28:10 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 03:56:43 +00:00
|
|
|
async function getCurrentBranch (gitDir) {
|
2023-01-12 17:05:26 +00:00
|
|
|
const RELEASE_BRANCH_PATTERN = /^\d+-x-y$/;
|
|
|
|
const MAIN_BRANCH_PATTERN = /^main$/;
|
|
|
|
const ORIGIN_MAIN_BRANCH_PATTERN = /^origin\/main$/;
|
|
|
|
|
2019-06-17 03:56:43 +00:00
|
|
|
let branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], gitDir);
|
2021-05-28 00:32:16 +00:00
|
|
|
if (!MAIN_BRANCH_PATTERN.test(branch) && !RELEASE_BRANCH_PATTERN.test(branch)) {
|
2019-06-17 03:56:43 +00:00
|
|
|
const lastCommit = await handleGitCall(['rev-parse', 'HEAD'], gitDir);
|
2019-06-21 20:59:00 +00:00
|
|
|
const branches = (await handleGitCall([
|
|
|
|
'branch',
|
|
|
|
'--contains',
|
|
|
|
lastCommit,
|
|
|
|
'--remote'
|
|
|
|
], gitDir)).split('\n');
|
|
|
|
|
2021-05-28 00:32:16 +00:00
|
|
|
branch = branches.find(b => MAIN_BRANCH_PATTERN.test(b.trim()) || ORIGIN_MAIN_BRANCH_PATTERN.test(b.trim()) || RELEASE_BRANCH_PATTERN.test(b.trim()));
|
2019-06-18 14:54:32 +00:00
|
|
|
if (!branch) {
|
|
|
|
console.log(`${fail} no release branch exists for this ref`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-06-21 20:59:00 +00:00
|
|
|
if (branch.startsWith('origin/')) branch = branch.substr('origin/'.length);
|
2019-06-17 03:56:43 +00:00
|
|
|
}
|
2019-06-20 20:12:05 +00:00
|
|
|
return branch.trim();
|
2019-06-17 03:56:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:23:11 +00:00
|
|
|
function chunkFilenames (filenames, offset = 0) {
|
|
|
|
// Windows has a max command line length of 2047 characters, so we can't
|
|
|
|
// provide too many filenames without going over that. To work around that,
|
|
|
|
// chunk up a list of filenames such that it won't go over that limit when
|
|
|
|
// used as args. Other platforms may have higher limits, but 4095 might be
|
|
|
|
// the limit on Linux systems according to `termios(3)`, so cap it there.
|
|
|
|
const MAX_FILENAME_ARGS_LENGTH =
|
|
|
|
(os.platform() === 'win32' ? 2047 : 4095) - offset;
|
|
|
|
|
|
|
|
return filenames.reduce(
|
|
|
|
(chunkedFilenames, filename) => {
|
|
|
|
const currChunk = chunkedFilenames[chunkedFilenames.length - 1];
|
|
|
|
const currChunkLength = currChunk.reduce(
|
|
|
|
(totalLength, _filename) => totalLength + _filename.length + 1,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (currChunkLength + filename.length + 1 > MAX_FILENAME_ARGS_LENGTH) {
|
|
|
|
chunkedFilenames.push([filename]);
|
|
|
|
} else {
|
|
|
|
currChunk.push(filename);
|
|
|
|
}
|
|
|
|
return chunkedFilenames;
|
|
|
|
},
|
|
|
|
[[]]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-24 08:00:25 +00:00
|
|
|
/**
|
|
|
|
* @param {string} top
|
|
|
|
* @param {(filename: string) => boolean} test
|
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
|
|
|
async function findMatchingFiles (top, test) {
|
2024-06-18 22:54:26 +00:00
|
|
|
return fs.promises.readdir(top, { encoding: 'utf8', recursive: true })
|
|
|
|
.then(files => {
|
|
|
|
return files
|
|
|
|
.filter(name => path.basename(name) !== '.bin')
|
|
|
|
.filter(name => test(name))
|
|
|
|
.map(name => path.join(top, name));
|
|
|
|
});
|
2023-01-24 08:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
module.exports = {
|
2022-06-22 10:23:11 +00:00
|
|
|
chunkFilenames,
|
2023-01-24 08:00:25 +00:00
|
|
|
findMatchingFiles,
|
2018-12-03 21:28:10 +00:00
|
|
|
getCurrentBranch,
|
2018-09-13 16:57:39 +00:00
|
|
|
getElectronExec,
|
2019-08-21 17:41:35 +00:00
|
|
|
getOutDir,
|
2018-09-13 16:57:39 +00:00
|
|
|
getAbsoluteElectronExec,
|
2022-11-21 15:24:26 +00:00
|
|
|
handleGitCall,
|
2019-06-24 17:18:04 +00:00
|
|
|
ELECTRON_DIR,
|
|
|
|
SRC_DIR
|
2018-09-13 16:57:39 +00:00
|
|
|
};
|