mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-06-07 06:50:48 +00:00
7a08a2d676
* Catch promise errors better * Move subFunctions to bottom of createNewWindow * Use parents when creating child BrowserWindow instances * Some about:blank pages have an anchor (for some reason) * Inject browserWindowOptions better * Interim refactor to MainWindow object * Split up the window functions/helpers/events some * Further separate out window functions + tests * Add a mock for unit testing functions that access electron * Add unit tests for onWillPreventUnload * Improve windowEvents tests * Add the first test for windowHelpers * Move WebRequest event handling to node * insertCSS completely under test * clearAppData completely under test * Fix contextMenu require bug * More tests + fixes * Fix + add to createNewTab tests * Convert createMainWindow back to func + work out gremlins * Move setupWindow away from main since its shared * Make sure contextMenu is handling promises * Fix issues with fullscreen not working + menu refactor * Run jest against app/dist so that we can hit app unit tests as well * Requested PR changes * Add strict typing; tests currently failing * Fix failing unit tests * Add some more eslint warnings and fixes * More eslint fixes * Strict typing on (still issues with the lib dir) * Fix the package.json import/require * Fix some funky test errors * Warn -> Error for eslint rules * @ts-ignore -> @ts-expect-error * Add back the ext code I removed
202 lines
5.2 KiB
TypeScript
202 lines
5.2 KiB
TypeScript
import { spawnSync } from 'child_process';
|
|
import * as crypto from 'crypto';
|
|
import * as os from 'os';
|
|
import * as path from 'path';
|
|
|
|
import axios from 'axios';
|
|
import * as dns from 'dns';
|
|
import * as hasbin from 'hasbin';
|
|
import * as log from 'loglevel';
|
|
import { ncp } from 'ncp';
|
|
import * as tmp from 'tmp';
|
|
|
|
import { parseJson } from '../utils/parseUtils';
|
|
|
|
tmp.setGracefulCleanup(); // cleanup temp dirs even when an uncaught exception occurs
|
|
|
|
const now = new Date();
|
|
const TMP_TIME = `${now.getHours()}-${now.getMinutes()}-${now.getSeconds()}`;
|
|
|
|
export type DownloadResult = {
|
|
data: Buffer;
|
|
ext: string;
|
|
};
|
|
|
|
type ProcessEnvs = Record<string, unknown>;
|
|
|
|
export function hasWine(): boolean {
|
|
return hasbin.sync('wine');
|
|
}
|
|
|
|
export function isOSX(): boolean {
|
|
return os.platform() === 'darwin';
|
|
}
|
|
|
|
export function isWindows(): boolean {
|
|
return os.platform() === 'win32';
|
|
}
|
|
|
|
export function isWindowsAdmin(): boolean {
|
|
if (process.platform !== 'win32') {
|
|
return false;
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
|
|
// https://stackoverflow.com/questions/57009374/check-admin-or-non-admin-users-in-nodejs-or-javascript
|
|
return spawnSync('fltmc').status === 0;
|
|
}
|
|
|
|
/**
|
|
* Create a temp directory with a debug-friendly name, and return its path.
|
|
* Will be automatically deleted on exit.
|
|
*/
|
|
export function getTempDir(prefix: string, mode?: number): string {
|
|
return tmp.dirSync({
|
|
mode,
|
|
unsafeCleanup: true, // recursively remove tmp dir on exit, even if not empty.
|
|
prefix: `nativefier-${TMP_TIME}-${prefix}-`,
|
|
}).name;
|
|
}
|
|
|
|
export async function copyFileOrDir(
|
|
sourceFileOrDir: string,
|
|
dest: string,
|
|
): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
ncp(sourceFileOrDir, dest, (error: Error[] | null): void => {
|
|
if (error) {
|
|
reject(error);
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
export function downloadFile(
|
|
fileUrl: string,
|
|
): Promise<DownloadResult | undefined> {
|
|
log.debug(`Downloading ${fileUrl}`);
|
|
return axios
|
|
.get<Buffer>(fileUrl, {
|
|
responseType: 'arraybuffer',
|
|
})
|
|
.then((response) => {
|
|
if (!response.data) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
data: response.data,
|
|
ext: path.extname(fileUrl),
|
|
};
|
|
});
|
|
}
|
|
|
|
export function getAllowedIconFormats(platform: string): string[] {
|
|
const hasIdentify = hasbin.sync('identify') || hasbin.sync('gm');
|
|
const hasConvert = hasbin.sync('convert') || hasbin.sync('gm');
|
|
const hasIconUtil = hasbin.sync('iconutil');
|
|
|
|
const pngToIcns = hasConvert && hasIconUtil;
|
|
const pngToIco = hasConvert;
|
|
const icoToIcns = pngToIcns && hasIdentify;
|
|
const icoToPng = hasConvert;
|
|
|
|
// Unsupported
|
|
const icnsToPng = false;
|
|
const icnsToIco = false;
|
|
|
|
const formats: string[] = [];
|
|
|
|
// Shell scripting is not supported on windows, temporary override
|
|
if (isWindows()) {
|
|
switch (platform) {
|
|
case 'darwin':
|
|
formats.push('.icns');
|
|
break;
|
|
case 'linux':
|
|
formats.push('.png');
|
|
break;
|
|
case 'win32':
|
|
formats.push('.ico');
|
|
break;
|
|
default:
|
|
throw new Error(`Unknown platform ${platform}`);
|
|
}
|
|
log.debug(
|
|
`Allowed icon formats when building for ${platform} (limited on Windows):`,
|
|
formats,
|
|
);
|
|
return formats;
|
|
}
|
|
|
|
switch (platform) {
|
|
case 'darwin':
|
|
formats.push('.icns');
|
|
if (pngToIcns) {
|
|
formats.push('.png');
|
|
}
|
|
if (icoToIcns) {
|
|
formats.push('.ico');
|
|
}
|
|
break;
|
|
case 'linux':
|
|
formats.push('.png');
|
|
if (icoToPng) {
|
|
formats.push('.ico');
|
|
}
|
|
if (icnsToPng) {
|
|
formats.push('.icns');
|
|
}
|
|
break;
|
|
case 'win32':
|
|
formats.push('.ico');
|
|
if (pngToIco) {
|
|
formats.push('.png');
|
|
}
|
|
if (icnsToIco) {
|
|
formats.push('.icns');
|
|
}
|
|
break;
|
|
default:
|
|
throw new Error(`Unknown platform ${platform}`);
|
|
}
|
|
log.debug(`Allowed icon formats when building for ${platform}:`, formats);
|
|
return formats;
|
|
}
|
|
|
|
/**
|
|
* Refuse args like '--n' or '-name', we accept either short '-n' or long '--name'
|
|
*/
|
|
export function isArgFormatInvalid(arg: string): boolean {
|
|
return (
|
|
(arg.startsWith('---') ||
|
|
/^--[a-z]$/i.exec(arg) !== null ||
|
|
/^-[a-z]{2,}$/i.exec(arg) !== null) &&
|
|
!['--x', '--y'].includes(arg) // exception for long args --{x,y}
|
|
);
|
|
}
|
|
|
|
export function generateRandomSuffix(length = 6): string {
|
|
const hash = crypto.createHash('md5');
|
|
// Add a random salt to help avoid collisions
|
|
hash.update(crypto.randomBytes(256));
|
|
return hash.digest('hex').substring(0, length);
|
|
}
|
|
|
|
export function getProcessEnvs(val: string): ProcessEnvs | undefined {
|
|
if (!val) {
|
|
return undefined;
|
|
}
|
|
return parseJson<ProcessEnvs>(val);
|
|
}
|
|
|
|
export function checkInternet(): void {
|
|
dns.lookup('npmjs.com', (err) => {
|
|
if (err && err.code === 'ENOTFOUND') {
|
|
log.warn(
|
|
'\nNo Internet Connection\nTo offline build, download electron from https://github.com/electron/electron/releases\nand place in ~/AppData/Local/electron/Cache/ on Windows,\n~/.cache/electron on Linux or ~/Library/Caches/electron/ on Mac\nUse --electron-version to specify the version you downloaded.',
|
|
);
|
|
}
|
|
});
|
|
}
|