Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 37009b1

Browse files
authored
Attempt to reuse Chrome tab on OS X (#62)
Fixes #38
1 parent b941ad2 commit 37009b1

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

scripts/eject.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi
4646
path.join('config', 'webpack.config.dev.js'),
4747
path.join('config', 'webpack.config.prod.js'),
4848
path.join('scripts', 'build.js'),
49-
path.join('scripts', 'start.js')
49+
path.join('scripts', 'start.js'),
50+
path.join('scripts', 'openChrome.applescript')
5051
];
5152

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

6970
files.forEach(function(file) {
7071
console.log('Copying ' + file + ' to ' + hostPath);
71-
var content = fs.readFileSync(path.join(selfPath, file), 'utf8');
72-
// Remove license header
73-
content = content.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '').trim() + '\n';
72+
content = fs
73+
.readFileSync(path.join(selfPath, file), 'utf8')
74+
// Remove license header from JS
75+
.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '')
76+
// Remove license header from AppleScript
77+
.replace(/^--.*\n/gm, '')
78+
.trim() + '\n';
7479
fs.writeFileSync(path.join(hostPath, file), content);
7580
});
7681
console.log();

scripts/openChrome.applescript

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- Copyright (c) 2015-present, Facebook, Inc.
2+
-- All rights reserved.
3+
--
4+
-- This source code is licensed under the BSD-style license found in the
5+
-- LICENSE file in the root directory of this source tree. An additional grant
6+
-- of patent rights can be found in the PATENTS file in the same directory.
7+
on run argv
8+
set theURL to item 1 of argv
9+
10+
tell application "Chrome"
11+
12+
if (count every window) = 0 then
13+
make new window
14+
end if
15+
16+
-- Find a tab currently running the debugger
17+
set found to false
18+
set theTabIndex to -1
19+
repeat with theWindow in every window
20+
set theTabIndex to 0
21+
repeat with theTab in every tab of theWindow
22+
set theTabIndex to theTabIndex + 1
23+
if theTab's URL is theURL then
24+
set found to true
25+
exit repeat
26+
end if
27+
end repeat
28+
29+
if found then
30+
exit repeat
31+
end if
32+
end repeat
33+
34+
if found then
35+
tell theTab to reload
36+
set index of theWindow to 1
37+
set theWindow's active tab index to theTabIndex
38+
else
39+
tell window 1
40+
activate
41+
make new tab with properties {URL:theURL}
42+
end tell
43+
end if
44+
end tell
45+
end run

scripts/start.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
process.env.NODE_ENV = 'development';
1111

12+
var path = require('path');
1213
var chalk = require('chalk');
1314
var webpack = require('webpack');
1415
var WebpackDevServer = require('webpack-dev-server');
@@ -117,6 +118,27 @@ compiler.plugin('done', function (stats) {
117118
}
118119
});
119120

121+
function openBrowser() {
122+
if (process.platform === 'darwin') {
123+
try {
124+
// Try our best to reuse existing tab
125+
// on OS X Google Chrome with AppleScript
126+
execSync('ps cax | grep "Google Chrome"');
127+
execSync(
128+
'osascript ' +
129+
path.resolve(__dirname, './openChrome.applescript') +
130+
' http://localhost:3000/'
131+
);
132+
return;
133+
} catch (err) {
134+
// Ignore errors.
135+
}
136+
}
137+
// Fallback to opn
138+
// (It will always open new tab)
139+
opn('http://localhost:3000/');
140+
}
141+
120142
new WebpackDevServer(compiler, {
121143
historyApiFallback: true,
122144
hot: true, // Note: only CSS is currently hot reloaded
@@ -128,5 +150,5 @@ new WebpackDevServer(compiler, {
128150
}
129151

130152
console.log('Starting the development server...');
131-
opn('http://localhost:3000/');
153+
openBrowser();
132154
});

0 commit comments

Comments
 (0)