@electron/universal
Version:
Utility for creating Universal macOS applications from two x64 and arm64 Electron applications
119 lines • 4.12 kB
JavaScript
import fs from 'node:fs';
import path from 'node:path';
import { promises as stream } from 'node:stream';
import { spawn, ExitCodeError } from '@malept/cross-spawn-promise';
import { minimatch } from 'minimatch';
const MACHO_PREFIX = 'Mach-O ';
const UNPACKED_ASAR_PATH = path.join('Contents', 'Resources', 'app.asar.unpacked');
export var AppFileType;
(function (AppFileType) {
AppFileType[AppFileType["MACHO"] = 0] = "MACHO";
AppFileType[AppFileType["PLAIN"] = 1] = "PLAIN";
AppFileType[AppFileType["INFO_PLIST"] = 2] = "INFO_PLIST";
AppFileType[AppFileType["SNAPSHOT"] = 3] = "SNAPSHOT";
AppFileType[AppFileType["APP_CODE"] = 4] = "APP_CODE";
AppFileType[AppFileType["SINGLE_ARCH"] = 5] = "SINGLE_ARCH";
})(AppFileType || (AppFileType = {}));
const isSingleArchFile = (relativePath, opts) => {
if (opts.singleArchFiles === undefined) {
return false;
}
const unpackedPath = path.relative(UNPACKED_ASAR_PATH, relativePath);
// Outside of app.asar.unpacked
if (unpackedPath.startsWith('..')) {
return false;
}
return minimatch(unpackedPath, opts.singleArchFiles, {
matchBase: true,
});
};
/**
*
* @param appPath Path to the application
*/
export const getAllAppFiles = async (appPath, opts) => {
const unpackedPath = path.join('Contents', 'Resources', 'app.asar.unpacked');
const files = [];
const visited = new Set();
const traverse = async (p) => {
p = await fs.promises.realpath(p);
if (visited.has(p))
return;
visited.add(p);
const info = await fs.promises.stat(p);
if (info.isSymbolicLink())
return;
if (info.isFile()) {
const relativePath = path.relative(appPath, p);
let fileType = AppFileType.PLAIN;
var fileOutput = '';
try {
fileOutput = await spawn('file', ['--brief', '--no-pad', p]);
}
catch (e) {
if (e instanceof ExitCodeError) {
/* silently accept error codes from "file" */
}
else {
throw e;
}
}
if (p.endsWith('.asar')) {
fileType = AppFileType.APP_CODE;
}
else if (isSingleArchFile(relativePath, opts)) {
fileType = AppFileType.SINGLE_ARCH;
}
else if (fileOutput.startsWith(MACHO_PREFIX)) {
fileType = AppFileType.MACHO;
}
else if (p.endsWith('.bin')) {
fileType = AppFileType.SNAPSHOT;
}
else if (path.basename(p) === 'Info.plist') {
fileType = AppFileType.INFO_PLIST;
}
files.push({
relativePath,
type: fileType,
});
}
if (info.isDirectory()) {
for (const child of await fs.promises.readdir(p)) {
await traverse(path.resolve(p, child));
}
}
};
await traverse(appPath);
return files;
};
export const readMachOHeader = async (path) => {
const chunks = [];
// no need to read the entire file, we only need the first 4 bytes of the file to determine the header
await stream.pipeline(fs.createReadStream(path, { start: 0, end: 3 }), async function* (source) {
for await (const chunk of source) {
chunks.push(chunk);
}
});
return Buffer.concat(chunks);
};
export const fsMove = async (oldPath, newPath) => {
try {
await fs.promises.rename(oldPath, newPath);
}
catch (err) {
if (err.code === 'EXDEV') {
// Cross-device link, fallback to copy and delete
await fs.promises.cp(oldPath, newPath, {
force: true,
recursive: true,
verbatimSymlinks: true,
});
await fs.promises.rm(oldPath, { force: true, recursive: true });
}
else {
throw err;
}
}
};
//# sourceMappingURL=file-utils.js.map