Do not delegate to Launcher

Summary: For the last stable Electron version, do not delegate to Launcher.

Reviewed By: antonk52

Differential Revision: D49821835

fbshipit-source-id: 0a80627cd1da312447b7d98d0351aa8faf2bae89
This commit is contained in:
Lorenzo Blasa
2023-10-02 05:06:03 -07:00
committed by Facebook GitHub Bot
parent dbe07b80ad
commit df8a68b7f1
2 changed files with 42 additions and 151 deletions

View File

@@ -1,100 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import os from 'os';
import fs from 'fs';
import path from 'path';
import {spawn} from 'child_process';
import xdg from 'xdg-basedir';
import mkdirp from 'mkdirp';
const isProduction = () =>
!/node_modules[\\/]electron[\\/]/.test(process.execPath);
const isLauncherInstalled = async () => {
if (os.type() == 'Darwin') {
const receipt = 'com.facebook.flipper.launcher';
const plistLocation = '/Applications/Flipper.app/Contents/Info.plist';
try {
return (
(await fs.promises.stat(plistLocation)) &&
(await fs.promises.readFile(plistLocation)).indexOf(receipt) > 0
);
} catch (e) {
console.error('Error while reading Info.plist', e);
return false;
}
}
return false;
};
const startLauncher = (argv: {file?: string; url?: string}) => {
const args = [];
if (argv.file) {
args.push('--file', argv.file);
}
if (argv.url) {
args.push('--url', argv.url);
}
if (os.type() == 'Darwin') {
spawn('open', ['/Applications/Flipper.app', '--args'].concat(args));
}
};
const checkIsCycle = async () => {
const dir = path.join(xdg.cache!, 'flipper');
const filePath = path.join(dir, 'last-launcher-run');
// This isn't monotonically increasing, so there's a change we get time drift
// between the checks, but the worst case here is that we do two roundtrips
// before this check works.
const rightNow = Date.now();
let backThen;
try {
backThen = parseInt((await fs.promises.readFile(filePath)).toString(), 10);
} catch (e) {
backThen = 0;
}
const delta = rightNow - backThen;
await mkdirp(dir);
await fs.promises.writeFile(filePath, '' + rightNow);
// If the last startup was less than 5s ago, something's not okay.
return Math.abs(delta) < 5000;
};
/**
* Runs the launcher if required and returns a boolean based on whether
* it has. You should shut down this instance of the app in that case.
*/
export default async function delegateToLauncher(argv: {
launcher: boolean;
file?: string;
url?: string;
}) {
if (argv.launcher && isProduction() && (await isLauncherInstalled())) {
if (await checkIsCycle()) {
console.error(
'Launcher cycle detected. Not delegating even though I usually would.',
);
return false;
}
console.warn('Delegating to Flipper Launcher ...');
console.warn(
`You can disable this behavior by passing '--no-launcher' at startup.`,
);
startLauncher(argv);
return true;
}
return false;
}

View File

@@ -28,7 +28,6 @@ import fixPath from 'fix-path';
import {exec} from 'child_process'; import {exec} from 'child_process';
import setup, {Config, configPath} from './setup'; import setup, {Config, configPath} from './setup';
import isFB from './fb-stubs/isFB'; import isFB from './fb-stubs/isFB';
import delegateToLauncher from './launcher';
import yargs from 'yargs'; import yargs from 'yargs';
import {promisify} from 'util'; import {promisify} from 'util';
import process from 'process'; import process from 'process';
@@ -174,58 +173,50 @@ app.on('ready', async () => {
const config = await setup(argv); const config = await setup(argv);
processConfig(config); processConfig(config);
// If we delegate to the launcher, shut down this instance of the app. appReady = true;
delegateToLauncher(argv)
.then(async (hasLauncherInvoked: boolean) => {
if (hasLauncherInvoked) {
app.quit();
return;
}
appReady = true;
app.commandLine.appendSwitch('scroll-bounce');
configureSession();
createWindow(config);
// if in development install the react devtools extension app.commandLine.appendSwitch('scroll-bounce');
if (process.env.NODE_ENV === 'development') { configureSession();
const { createWindow(config);
default: installExtension,
REACT_DEVELOPER_TOOLS, // if in development install the react devtools extension
} = require('electron-devtools-installer'); if (process.env.NODE_ENV === 'development') {
// if set, try to download a newever version of the dev tools const {
const forceDownload = process.env.FLIPPER_UPDATE_DEV_TOOLS === 'true'; default: installExtension,
if (forceDownload) { REACT_DEVELOPER_TOOLS,
console.log('Force updating DevTools'); } = require('electron-devtools-installer');
} // if set, try to download a newever version of the dev tools
// React const forceDownload = process.env.FLIPPER_UPDATE_DEV_TOOLS === 'true';
// Fix for extension loading (see D27685981) if (forceDownload) {
// Work around per https://github.com/electron/electron/issues/23662#issuecomment-787420799 console.log('Force updating DevTools');
const reactDevToolsPath = `${os.homedir()}/Library/Application Support/Electron/extensions/${ }
REACT_DEVELOPER_TOOLS.id // React
}`; // Fix for extension loading (see D27685981)
if (await promisify(fs.exists)(reactDevToolsPath)) { // Work around per https://github.com/electron/electron/issues/23662#issuecomment-787420799
console.log('Loading React devtools from disk ' + reactDevToolsPath); const reactDevToolsPath = `${os.homedir()}/Library/Application Support/Electron/extensions/${
try { REACT_DEVELOPER_TOOLS.id
await session.defaultSession.loadExtension( }`;
reactDevToolsPath, if (await promisify(fs.exists)(reactDevToolsPath)) {
// @ts-ignore only supported (and needed) in Electron 12 console.log('Loading React devtools from disk ' + reactDevToolsPath);
{allowFileAccess: true}, try {
); await session.defaultSession.loadExtension(
} catch (e) { reactDevToolsPath,
console.error('Failed to load React devtools from disk: ', e); // @ts-ignore only supported (and needed) in Electron 12
} {allowFileAccess: true},
} else { );
try { } catch (e) {
await installExtension(REACT_DEVELOPER_TOOLS.id, { console.error('Failed to load React devtools from disk: ', e);
loadExtensionOptions: {allowFileAccess: true, forceDownload},
});
} catch (e) {
console.error('Failed to install React devtools extension', e);
}
}
} }
}) } else {
.catch((e: any) => console.error('Error while delegating app launch', e)); try {
await installExtension(REACT_DEVELOPER_TOOLS.id, {
loadExtensionOptions: {allowFileAccess: true, forceDownload},
});
} catch (e) {
console.error('Failed to install React devtools extension', e);
}
}
}
}); });
app.on('web-contents-created', (_event, contents) => { app.on('web-contents-created', (_event, contents) => {