hyper/bin/cp-snapshot.js

88 lines
2.9 KiB
JavaScript
Raw Normal View History

2025-08-30 21:52:25 -08:00
2022-04-14 08:50:54 -08:00
const path = require('path');
const fs = require('fs');
2025-04-13 10:11:20 -08:00
const fsPromises = require('fs/promises');
2022-04-14 08:50:54 -08:00
const {Arch} = require('electron-builder');
function copySnapshot(pathToElectron, archToCopy) {
const snapshotFileName = 'snapshot_blob.bin';
const v8ContextFileName = getV8ContextFileName(archToCopy);
const pathToBlob = path.resolve(__dirname, '..', 'cache', archToCopy, snapshotFileName);
const pathToBlobV8 = path.resolve(__dirname, '..', 'cache', archToCopy, v8ContextFileName);
console.log('Copying v8 snapshots from', pathToBlob, 'to', pathToElectron);
2025-08-30 21:52:25 -08:00
fs.mkdirSync(pathToElectron, { recursive: true });
2022-04-14 08:50:54 -08:00
fs.copyFileSync(pathToBlob, path.join(pathToElectron, snapshotFileName));
fs.copyFileSync(pathToBlobV8, path.join(pathToElectron, v8ContextFileName));
}
function getPathToElectron() {
2025-08-30 21:52:25 -08:00
const electronPath = require.resolve('electron');
2022-04-14 08:50:54 -08:00
switch (process.platform) {
case 'darwin':
return path.resolve(
2025-08-30 21:52:25 -08:00
electronPath,
'..',
2022-04-14 08:50:54 -08:00
'..',
2025-08-30 21:52:25 -08:00
'..',
'dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Resources'
2022-04-14 08:50:54 -08:00
);
case 'win32':
case 'linux':
2025-08-30 21:52:25 -08:00
return path.resolve(electronPath, '..', '..', '..', 'dist');
2022-04-14 08:50:54 -08:00
}
}
2025-08-30 21:52:25 -08:00
2022-04-14 08:50:54 -08:00
function getV8ContextFileName(archToCopy) {
return `snapshot_blob.bin`;
2022-04-14 08:50:54 -08:00
}
exports.default = async (context) => {
const archToCopy = Arch[context.arch];
const pathToElectron =
process.platform === 'darwin'
? `${context.appOutDir}/Hyper.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Resources`
: context.appOutDir;
copySnapshot(pathToElectron, archToCopy);
2025-04-13 10:11:20 -08:00
useLoaderScriptFix(context);
2022-04-14 08:50:54 -08:00
};
if (require.main === module) {
const archToCopy = process.env.npm_config_arch;
const pathToElectron = getPathToElectron();
if ((process.arch.startsWith('arm') ? 'arm64' : 'x64') === archToCopy) {
copySnapshot(pathToElectron, archToCopy);
}
}
2025-04-13 10:11:20 -08:00
// copied and modified from https://github.com/gergof/electron-builder-sandbox-fix/blob/master/lib/index.js
// copied and modified from https://github.com/Adamant-im/adamant-im/blob/7b20272a717833ffb0b49b034ab9974118fc59ec/scripts/electron/sandboxFix.js
const useLoaderScriptFix = async (params) => {
if (params.electronPlatformName !== 'linux') {
// this fix is only required on linux
return
}
const executable = path.join(params.appOutDir, params.packager.executableName)
const loaderScript = `#!/usr/bin/env bash
set -u
SCRIPT_DIR="$( cd "$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
exec "$SCRIPT_DIR/${params.packager.executableName}.bin" "--no-sandbox" "$@"
`
try {
await fsPromises.rename(executable, executable + '.bin')
await fsPromises.writeFile(executable, loaderScript)
await fsPromises.chmod(executable, 0o755)
} catch (e) {
console.error('failed to create loader for sandbox fix: ' + e.message)
throw new Error('Failed to create loader for sandbox fix')
}
console.log('sandbox fix successfully applied')
}