From 35f9ac89a7554f1fcc0371ae09297245ffd8f24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Bigio?= Date: Thu, 8 Oct 2015 14:15:13 -0700 Subject: [PATCH] Unify oss and internal version of dev-tools middleware Reviewed By: @amasad Differential Revision: D2518928 fb-gh-sync-id: 0c98b16481b524436a2cf4af3e196ac5715b55ee --- packager/getDevToolsMiddleware.js | 50 ++++++++++++++++++++++++ packager/packager.js | 63 ++++++++----------------------- 2 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 packager/getDevToolsMiddleware.js diff --git a/packager/getDevToolsMiddleware.js b/packager/getDevToolsMiddleware.js new file mode 100644 index 000000000..2c1d693ae --- /dev/null +++ b/packager/getDevToolsMiddleware.js @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +'use strict'; + +var execFile = require('child_process').execFile; +var fs = require('fs'); +var path = require('path'); + +module.exports = function(options) { + return function(req, res, next) { + if (req.url === '/debugger-ui') { + var debuggerPath = path.join(__dirname, 'debugger.html'); + res.writeHead(200, {'Content-Type': 'text/html'}); + fs.createReadStream(debuggerPath).pipe(res); + } else if (req.url === '/debuggerWorker.js') { + var workerPath = path.join(__dirname, 'debuggerWorker.js'); + res.writeHead(200, {'Content-Type': 'application/javascript'}); + fs.createReadStream(workerPath).pipe(res); + } else if (req.url === '/launch-safari-devtools') { + // TODO: remove `console.log` and dev tools binary + console.log( + 'We removed support for Safari dev-tools. ' + + 'If you still need this, please let us know.' + ); + } else if (req.url === '/launch-chrome-devtools') { + var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui'; + var script = 'launchChromeDevTools.applescript'; + console.log('Launching Dev Tools...'); + execFile( + path.join(__dirname, script), [debuggerURL], + function(err, stdout, stderr) { + if (err) { + console.log('Failed to run ' + script, err); + } + console.log(stdout); + console.warn(stderr); + } + ); + res.end('OK'); + } else { + next(); + } + }; +}; diff --git a/packager/packager.js b/packager/packager.js index fd040595d..5d9704e83 100644 --- a/packager/packager.js +++ b/packager/packager.js @@ -8,23 +8,22 @@ */ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var childProcess = require('child_process'); -var http = require('http'); -var isAbsolutePath = require('absolute-path'); +const fs = require('fs'); +const path = require('path'); +const childProcess = require('child_process'); +const http = require('http'); +const isAbsolutePath = require('absolute-path'); -var getFlowTypeCheckMiddleware = require('./getFlowTypeCheckMiddleware'); - -var chalk = require('chalk'); -var connect = require('connect'); -var ReactPackager = require('./react-packager'); -var blacklist = require('./blacklist.js'); -var checkNodeVersion = require('./checkNodeVersion'); -var formatBanner = require('./formatBanner'); -var launchEditor = require('./launchEditor.js'); -var parseCommandLine = require('./parseCommandLine.js'); -var webSocketProxy = require('./webSocketProxy.js'); +const blacklist = require('./blacklist.js'); +const chalk = require('chalk'); +const checkNodeVersion = require('./checkNodeVersion'); +const connect = require('connect'); +const formatBanner = require('./formatBanner'); +const getDevToolsMiddleware = require('./getDevToolsMiddleware'); +const launchEditor = require('./launchEditor.js'); +const parseCommandLine = require('./parseCommandLine.js'); +const ReactPackager = require('./react-packager'); +const webSocketProxy = require('./webSocketProxy.js'); var options = parseCommandLine([{ command: 'port', @@ -178,36 +177,6 @@ function openStackFrameInEditor(req, res, next) { } } -function getDevToolsLauncher(options) { - return function(req, res, next) { - if (req.url === '/debugger-ui') { - var debuggerPath = path.join(__dirname, 'debugger.html'); - res.writeHead(200, {'Content-Type': 'text/html'}); - fs.createReadStream(debuggerPath).pipe(res); - - } else if (req.url === '/debuggerWorker.js') { - var workerPath = path.join(__dirname, 'debuggerWorker.js'); - res.writeHead(200, {'Content-Type': 'application/javascript'}); - fs.createReadStream(workerPath).pipe(res); - - } else if (req.url === '/launch-chrome-devtools') { - var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui'; - var script = 'launchChromeDevTools.applescript'; - console.log('Launching Dev Tools...'); - childProcess.execFile(path.join(__dirname, script), [debuggerURL], function(err, stdout, stderr) { - if (err) { - console.log('Failed to run ' + script, err); - } - console.log(stdout); - console.warn(stderr); - }); - res.end('OK'); - } else { - next(); - } - }; -} - // A status page so the React/project.pbxproj build script // can verify that packager is running on 8081 and not // another program / service. @@ -311,7 +280,7 @@ function runServer( var app = connect() .use(loadRawBody) .use(openStackFrameInEditor) - .use(getDevToolsLauncher(options)) + .use(getDevToolsMiddleware(options)) .use(statusPageMiddleware) .use(systraceProfileMiddleware) .use(cpuProfileMiddleware)