Skip to content

Commit

Permalink
Attempt to reuse Chrome tab on OS X
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
gaearon committed Jul 21, 2016
1 parent 300cdc7 commit 7ff8cf1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
13 changes: 9 additions & 4 deletions scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi
path.join('config', 'webpack.config.dev.js'),
path.join('config', 'webpack.config.prod.js'),
path.join('scripts', 'build.js'),
path.join('scripts', 'start.js')
path.join('scripts', 'start.js'),
path.join('scripts', 'openChrome.applescript')
];

// Ensure that the host folder is clean and we won't override any files
Expand All @@ -68,9 +69,13 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi

files.forEach(function(file) {
console.log('Copying ' + file + ' to ' + hostPath);
var content = fs.readFileSync(path.join(selfPath, file), 'utf8');
// Remove license header
content = content.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '').trim() + '\n';
content = fs
.readFileSync(path.join(selfPath, file), 'utf8')
// Remove license header from JS
.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '')
// Remove license header from AppleScript
.replace(/^--.*\n/gm, '')
.trim() + '\n';
fs.writeFileSync(path.join(hostPath, file), content);
});
console.log();
Expand Down
45 changes: 45 additions & 0 deletions scripts/openChrome.applescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- 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.
on run argv
set theURL to item 1 of argv

tell application "Chrome"

if (count every window) = 0 then
make new window
end if

-- Find a tab currently running the debugger
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if theTab's URL is theURL then
set found to true
exit repeat
end if
end repeat

if found then
exit repeat
end if
end repeat

if found then
tell theTab to reload
set index of theWindow to 1
set theWindow's active tab index to theTabIndex
else
tell window 1
activate
make new tab with properties {URL:theURL}
end tell
end if
end tell
end run
24 changes: 23 additions & 1 deletion scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

process.env.NODE_ENV = 'development';

var path = require('path');
var chalk = require('chalk');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
Expand Down Expand Up @@ -117,6 +118,27 @@ compiler.plugin('done', function (stats) {
}
});

function openBrowser() {
if (process.platform === 'darwin') {
try {
// Try our best to reuse existing tab
// on OS X Google Chrome with AppleScript
execSync('ps cax | grep "Google Chrome"');
execSync(
'osascript ' +
path.resolve(__dirname, './openChrome.applescript') +
' http://localhost:3000/'
);
return;
} catch (err) {
// Ignore errors.
}
}
// Fallback to opn
// (It will always open new tab)
opn('http://localhost:3000/');
}

new WebpackDevServer(compiler, {
historyApiFallback: true,
hot: true, // Note: only CSS is currently hot reloaded
Expand All @@ -128,5 +150,5 @@ new WebpackDevServer(compiler, {
}

console.log('Starting the development server...');
opn('http://localhost:3000/');
openBrowser();
});

0 comments on commit 7ff8cf1

Please sign in to comment.