From 91e235465b5ed288988b3548e7af647d1b7f63a5 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Wed, 9 Sep 2015 08:50:15 -0700 Subject: [PATCH] Move systrace helper out of the packager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: @​public The profiler helper shouldn't live inside the packager itself, move it to the packager.js file with other middlewares. Reviewed By: @martinbigio Differential Revision: D2424878 --- packager.js | 46 ++++++++++++++++++++++++++++-- react-packager/src/Server/index.js | 41 -------------------------- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/packager.js b/packager.js index b5f78969..2a95dfc9 100644 --- a/packager.js +++ b/packager.js @@ -10,7 +10,7 @@ var fs = require('fs'); var path = require('path'); -var execFile = require('child_process').execFile; +var childProcess = require('child_process'); var http = require('http'); var isAbsolutePath = require('absolute-path'); @@ -198,7 +198,7 @@ function getDevToolsLauncher(options) { 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) { + childProcess.execFile(path.join(__dirname, script), [debuggerURL], function(err, stdout, stderr) { if (err) { console.log('Failed to run ' + script, err); } @@ -223,6 +223,47 @@ function statusPageMiddleware(req, res, next) { } } +function systraceProfileMiddleware(req, res, next) { + if (req.url !== '/profile') { + next(); + return; + } + + console.log('Dumping profile information...'); + const dumpName = '/tmp/dump_' + Date.now() + '.json'; + const prefix = process.env.TRACE_VIEWER_PATH || ''; + const cmd = path.join(prefix, 'trace2html') + ' ' + dumpName; + fs.writeFileSync(dumpName, req.rawBody); + childProcess.exec(cmd, function(error) { + if (error) { + if (error.code === 127) { + console.error( + '\n** Failed executing `' + cmd + '` **\n\n' + + 'Google trace-viewer is required to visualize the data, do you have it installled?\n\n' + + 'You can get it at:\n\n' + + ' https://github.com/google/trace-viewer\n\n' + + 'If it\'s not in your path, you can set a custom path with:\n\n' + + ' TRACE_VIEWER_PATH=/path/to/trace-viewer\n\n' + + 'NOTE: Your profile data was kept at:\n\n' + + ' ' + dumpName + ); + } else { + console.error('Unknown error', error); + } + res.end(); + return; + } else { + childProcess.exec('rm ' + dumpName); + childProcess.exec('open ' + dumpName.replace(/json$/, 'html'), function(err) { + if (err) { + console.error(err); + } + res.end(); + }); + } + }); +} + function getAppMiddleware(options) { var transformerPath = options.transformer; if (!isAbsolutePath(transformerPath)) { @@ -255,6 +296,7 @@ function runServer( .use(openStackFrameInEditor) .use(getDevToolsLauncher(options)) .use(statusPageMiddleware) + .use(systraceProfileMiddleware) // Temporarily disable flow check until it's more stable //.use(getFlowTypeCheckMiddleware(options)) .use(getAppMiddleware(options)); diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index e889a357..da8ba005 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -16,8 +16,6 @@ const Promise = require('promise'); const _ = require('underscore'); const declareOpts = require('../lib/declareOpts'); -const exec = require('child_process').exec; -const fs = require('fs'); const path = require('path'); const url = require('url'); @@ -290,42 +288,6 @@ class Server { ).done(() => Activity.endEvent(assetEvent)); } - _processProfile(req, res) { - console.log('Dumping profile information...'); - const dumpName = '/tmp/dump_' + Date.now() + '.json'; - const prefix = process.env.TRACE_VIEWER_PATH || ''; - const cmd = path.join(prefix, 'trace2html') + ' ' + dumpName; - fs.writeFileSync(dumpName, req.rawBody); - exec(cmd, error => { - if (error) { - if (error.code === 127) { - console.error( - '\n** Failed executing `' + cmd + '` **\n\n' + - 'Google trace-viewer is required to visualize the data, do you have it installled?\n\n' + - 'You can get it at:\n\n' + - ' https://github.com/google/trace-viewer\n\n' + - 'If it\'s not in your path, you can set a custom path with:\n\n' + - ' TRACE_VIEWER_PATH=/path/to/trace-viewer\n\n' + - 'NOTE: Your profile data was kept at:\n\n' + - ' ' + dumpName - ); - } else { - console.error('Unknown error', error); - } - res.end(); - return; - } else { - exec('rm ' + dumpName); - exec('open ' + dumpName.replace(/json$/, 'html'), err => { - if (err) { - console.error(err); - } - res.end(); - }); - } - }); - } - processRequest(req, res, next) { const urlObj = url.parse(req.url, true); var pathname = urlObj.pathname; @@ -346,9 +308,6 @@ class Server { } else if (pathname.match(/^\/assets\//)) { this._processAssetsRequest(req, res); return; - } else if (pathname.match(/^\/profile\/?$/)) { - this._processProfile(req, res); - return; } else { next(); return;