Pass URL and file arguments to launcher

Summary:
Changes where we delegate to the launcher to a later point so we can successfully intercept file open events and custom URI events.

There's more information in Phase 1 in this paragraph of the doc: https://fb.quip.com/tpqnAbxnJw1w#UNZACAnVVGs

Reviewed By: danielbuechele

Differential Revision: D14563585

fbshipit-source-id: a8757a6072386e56102f15b0668456369a44aad7
This commit is contained in:
Pascal Hartig
2019-03-22 12:14:34 -07:00
committed by Facebook Github Bot
parent 939cc531e2
commit 759329bbc3
3 changed files with 67 additions and 33 deletions

View File

@@ -15,6 +15,7 @@ const fs = require('fs');
const {exec} = require('child_process');
const compilePlugins = require('./compilePlugins.js');
const setup = require('./setup');
const delegateToLauncher = require('./launcher');
const expandTilde = require('expand-tilde');
const yargs = require('yargs');
@@ -81,7 +82,7 @@ let win;
let appReady = false;
let pluginsCompiled = false;
let deeplinkURL = null;
let filePath = argv.file;
let filePath = null;
// tracking
setInterval(() => {
@@ -135,6 +136,7 @@ app.on('will-finish-launching', () => {
app.on('open-url', function(event, url) {
event.preventDefault();
deeplinkURL = url;
argv.url = url;
if (win) {
win.webContents.send('flipper-protocol-handler', deeplinkURL);
}
@@ -143,6 +145,7 @@ app.on('will-finish-launching', () => {
// When flipper app is running, and someone double clicks the import file, `componentDidMount` will not be called again and windows object will exist in that case. That's why calling `win.webContents.send('open-flipper-file', filePath);` again.
event.preventDefault();
filePath = path;
argv.file = path;
if (win) {
win.webContents.send('open-flipper-file', filePath);
filePath = null;
@@ -150,7 +153,13 @@ app.on('will-finish-launching', () => {
});
});
app.on('ready', function() {
app.on('ready', () => {
// If we delegate to the launcher, shut down this instance of the app.
if (delegateToLauncher(argv)) {
app.quit();
return;
}
appReady = true;
app.commandLine.appendSwitch('scroll-bounce');
tryCreateWindow();

56
static/launcher.js Normal file
View File

@@ -0,0 +1,56 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
const os = require('os');
const fs = require('fs');
const {spawn} = require('child_process');
const isProduction = () =>
!/node_modules[\\/]electron[\\/]/.test(process.execPath);
const isLauncherInstalled = () => {
if (os.type() == 'Darwin') {
const receipt = 'com.facebook.flipper.launcher';
const plistLocation = '/Applications/Flipper.app/Contents/Info.plist';
return (
fs.existsSync(plistLocation) &&
fs.readFileSync(plistLocation).indexOf(receipt) > 0
);
}
return false;
};
const startLauncher = argv => {
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));
}
};
/**
* 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.
*/
module.exports = function delegateToLauncher(argv) {
if (argv.launcher && isProduction() && isLauncherInstalled()) {
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

@@ -10,38 +10,7 @@ const os = require('os');
const fs = require('fs');
const {spawn} = require('child_process');
const isProduction = () =>
!/node_modules[\\/]electron[\\/]/.test(process.execPath);
const isLauncherInstalled = () => {
if (os.type() == 'Darwin') {
const receipt = 'com.facebook.flipper.launcher';
const plistLocation = '/Applications/Flipper.app/Contents/Info.plist';
return (
fs.existsSync(plistLocation) &&
fs.readFileSync(plistLocation).indexOf(receipt) > 0
);
}
return false;
};
const startLauncher = () => {
if (os.type() == 'Darwin') {
spawn('open', ['/Applications/Flipper.app']);
}
};
module.exports = function(argv) {
if (argv.launcher && isProduction() && isLauncherInstalled()) {
console.warn('Delegating to Flipper Launcher ...');
console.warn(
`You can disable this behavior by passing '--no-launcher' at startup.`,
);
startLauncher();
process.exit(0);
}
if (!process.env.ANDROID_HOME) {
process.env.ANDROID_HOME = '/opt/android_sdk';
}