Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openURLMiddleware #383

Merged
merged 2 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"morgan": "^1.9.0",
"node-fetch": "^2.2.0",
"node-notifier": "^5.2.1",
"opn": "^3.0.2",
"open": "^6.2.0",
"ora": "^3.4.0",
"plist": "^3.0.0",
"semver": "^5.0.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/src/commands/server/launchBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

import open from 'open';
import {logger} from '@react-native-community/cli-tools';

function launchBrowser(url: string) {
open(url, err => {
if (err) {
logger.error('Browser exited with error:', err);
}
});
}

export default launchBrowser;
4 changes: 2 additions & 2 deletions packages/cli/src/commands/server/launchChrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

import opn from 'opn';
import open from 'open';
import {execSync} from 'child_process';
import {logger} from '@react-native-community/cli-tools';

Expand Down Expand Up @@ -45,7 +45,7 @@ function getChromeAppName(): string {
}

function launchChrome(url: string) {
opn(url, {app: [getChromeAppName()]}, err => {
open(url, {app: [getChromeAppName()]}, err => {
if (err) {
logger.error('Google Chrome exited with error:', err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import copyToClipBoardMiddleware from './copyToClipBoardMiddleware';
import getSecurityHeadersMiddleware from './getSecurityHeadersMiddleware';
import loadRawBodyMiddleware from './loadRawBodyMiddleware';
import openStackFrameInEditorMiddleware from './openStackFrameInEditorMiddleware';
import openURLMiddleware from './openURLMiddleware';
import statusPageMiddleware from './statusPageMiddleware';
import systraceProfileMiddleware from './systraceProfileMiddleware';
import getDevToolsMiddleware from './getDevToolsMiddleware';
Expand Down Expand Up @@ -51,6 +52,7 @@ export default class MiddlewareManager {
.use(compression())
.use('/debugger-ui', serveStatic(debuggerUIFolder))
.use(openStackFrameInEditorMiddleware(this.options))
.use(openURLMiddleware)
.use(copyToClipBoardMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/src/commands/server/middleware/openURLMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Facebook, Inc. and its 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 launchBrowser from '../launchBrowser';
import {logger} from '@react-native-community/cli-tools';

/**
* Handle request from JS to open an arbitrary URL in Chrome
*/
export default function openURLMiddleware(req, res, next) {
if (req.url === '/open-url') {
const {url} = JSON.parse(req.rawBody);
logger.info(`Opening ${url}...`);
launchBrowser(url);
res.end('OK');
} else {
next();
}
}
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6823,11 +6823,12 @@ onetime@^2.0.0:
dependencies:
mimic-fn "^1.0.0"

opn@^3.0.2:
version "3.0.3"
resolved "http://registry.npmjs.org/opn/-/opn-3.0.3.tgz#b6d99e7399f78d65c3baaffef1fb288e9b85243a"
open@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/open/-/open-6.2.0.tgz#7cf92cb961b5d8498b071e64098bf5e27f57230c"
integrity sha512-Vxf6HJkwrqmvh9UAID3MnMYXntbTxKLOSfOnO7LJdzPf3NE3KQYFNV0/Lcz2VAndbRFil58XVCyh8tiX11fiYw==
dependencies:
object-assign "^4.0.1"
is-wsl "^1.1.0"

optimist@^0.6.1:
version "0.6.1"
Expand Down