diff --git a/static/index.js b/static/index.js index c79b32470..0e9e7e537 100644 --- a/static/index.js +++ b/static/index.js @@ -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(); diff --git a/static/launcher.js b/static/launcher.js new file mode 100644 index 000000000..79980d5c0 --- /dev/null +++ b/static/launcher.js @@ -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; +}; diff --git a/static/setup.js b/static/setup.js index 29d86e600..4e6434795 100644 --- a/static/setup.js +++ b/static/setup.js @@ -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'; }