Files
flipper/desktop/static/launcher.tsx
Anton Kastritskiy b4fc108c2e Rename first batch for '.ts' files to '.tsx' in doctor, static and test-utils
Summary: Rename first batch for '.ts' files to '.tsx'

Reviewed By: nikoant

Differential Revision: D33843051

fbshipit-source-id: 68dd2dc6538afce2daaf24c6412dc5db8b38acbc
2022-01-28 08:32:19 -08:00

101 lines
2.6 KiB
TypeScript

/**
* 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;
}