electron/script/lib/utils.js

103 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const { GitProcess } = require('dugite');
const fs = require('fs');
const path = require('path');
2020-03-20 20:28:31 +00:00
const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
const SRC_DIR = path.resolve(ELECTRON_DIR, '..');
2020-03-20 20:28:31 +00:00
const RELEASE_BRANCH_PATTERN = /(\d)+-(?:(?:[0-9]+-x$)|(?:x+-y$))/;
2020-03-20 20:28:31 +00:00
require('colors');
const pass = '✓'.green;
const fail = '✗'.red;
function getElectronExec () {
2020-03-20 20:28:31 +00:00
const OUT_DIR = getOutDir();
switch (process.platform) {
case 'darwin':
2020-03-20 20:28:31 +00:00
return `out/${OUT_DIR}/Electron.app/Contents/MacOS/Electron`;
case 'win32':
2020-03-20 20:28:31 +00:00
return `out/${OUT_DIR}/electron.exe`;
case 'linux':
2020-03-20 20:28:31 +00:00
return `out/${OUT_DIR}/electron`;
default:
2020-03-20 20:28:31 +00:00
throw new Error('Unknown platform');
}
}
function getOutDir (options = {}) {
2020-03-20 20:28:31 +00:00
const shouldLog = options.shouldLog || false;
const presetDirs = ['Testing', 'Release', 'Default', 'Debug'];
if (options.outDir || process.env.ELECTRON_OUT_DIR) {
2020-03-20 20:28:31 +00:00
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)) {
2020-03-20 20:28:31 +00:00
if (shouldLog) console.log(`OUT_DIR is: ${outDir}`);
return outDir;
}
// Throw error if user passed/set nonexistent directory.
2020-03-20 20:28:31 +00:00
throw new Error(`${outDir} directory not configured on your machine.`);
} else {
for (const buildType of presetDirs) {
2020-03-20 20:28:31 +00:00
const outPath = path.resolve(SRC_DIR, 'out', buildType);
if (fs.existsSync(outPath)) {
2020-03-20 20:28:31 +00:00
if (shouldLog) console.log(`OUT_DIR is: ${buildType}`);
return buildType;
}
}
}
// 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
2020-03-20 20:28:31 +00:00
throw new Error(`No valid out directory found; use one of ${presetDirs.join(',')} or set process.env.ELECTRON_OUT_DIR`);
}
function getAbsoluteElectronExec () {
2020-03-20 20:28:31 +00:00
return path.resolve(SRC_DIR, getElectronExec());
}
async function handleGitCall (args, gitDir) {
2020-03-20 20:28:31 +00:00
const details = await GitProcess.exec(args, gitDir);
if (details.exitCode === 0) {
2020-03-20 20:28:31 +00:00
return details.stdout.replace(/^\*|\s+|\s+$/, '');
} else {
2020-03-20 20:28:31 +00:00
const error = GitProcess.parseError(details.stderr);
console.log(`${fail} couldn't parse git process call: `, error);
process.exit(1);
}
}
async function getCurrentBranch (gitDir) {
2020-03-20 20:28:31 +00:00
let branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], gitDir);
if (branch !== 'master' && !RELEASE_BRANCH_PATTERN.test(branch)) {
2020-03-20 20:28:31 +00:00
const lastCommit = await handleGitCall(['rev-parse', 'HEAD'], gitDir);
const branches = (await handleGitCall([
'branch',
'--contains',
lastCommit,
'--remote'
2020-03-20 20:28:31 +00:00
], gitDir)).split('\n');
2020-03-20 20:28:31 +00:00
branch = branches.filter(b => b.trim() === 'master' || b.trim() === 'origin/master' || RELEASE_BRANCH_PATTERN.test(b.trim()))[0];
if (!branch) {
2020-03-20 20:28:31 +00:00
console.log(`${fail} no release branch exists for this ref`);
process.exit(1);
}
2020-03-20 20:28:31 +00:00
if (branch.startsWith('origin/')) branch = branch.substr('origin/'.length);
}
2020-03-20 20:28:31 +00:00
return branch.trim();
}
module.exports = {
getCurrentBranch,
getElectronExec,
getOutDir,
getAbsoluteElectronExec,
ELECTRON_DIR,
SRC_DIR
2020-03-20 20:28:31 +00:00
};