Skip to content

Commit

Permalink
Ignore clone stdio and use applescript to openBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
lukejacksonn committed May 18, 2020
1 parent de861d3 commit 051c7cb
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
const fs = require('fs');
const servor = require('./servor.js');
const openBrowser = require('./utils/openBrowser.js');

const readCredentials = () => ({
cert: fs.readFileSync(__dirname + '/servor.crt'),
Expand Down Expand Up @@ -30,7 +31,8 @@ const open =
if (!fs.existsSync(dest)) {
try {
require('child_process').execSync(
`git clone https://github.com/${repo}`
`git clone https://github.com/${repo}`,
{ stdio: 'ignore' }
);
} catch (e) {
console.log('\n ⚠️ Could not clone from https://github.com/', repo);
Expand Down Expand Up @@ -91,6 +93,5 @@ const open =

// Browser the server index

!!~process.argv.indexOf('--browse') &&
require('child_process').execSync(`${open} ${url}`);
!!~process.argv.indexOf('--browse') && openBrowser(url);
})();
29 changes: 29 additions & 0 deletions utils/openBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const childProcess = require('child_process');

module.exports = (url) => {
let cmd;
const args = [];

if (process.platform === 'darwin') {
try {
childProcess.execSync(
`osascript openChrome.applescript "${encodeURI(url)}"`,
{
cwd: __dirname,
stdio: 'ignore',
}
);
return true;
} catch (err) {}
cmd = 'open';
} else if (process.platform === 'win32') {
cmd = 'cmd.exe';
args.push('/c', 'start', '""', '/b');
url = url.replace(/&/g, '^&');
} else {
cmd = 'xdg-open';
}

args.push(url);
childProcess.spawn(cmd, args);
};
82 changes: 82 additions & 0 deletions utils/openChrome.applescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(*
Copyright (c) 2015-present, Facebook, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*)

property targetTab: null
property targetTabIndex: -1
property targetWindow: null

on run argv
set theURL to item 1 of argv

tell application "Chrome"

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

-- 1: Looking for tab running debugger
-- then, Reload debugging tab if found
-- then return
set found to my lookupTabWithUrl(theURL)
if found then
set targetWindow's active tab index to targetTabIndex
tell targetTab to reload
tell targetWindow to activate
set index of targetWindow to 1
return
end if

-- 2: Looking for Empty tab
-- In case debugging tab was not found
-- We try to find an empty tab instead
set found to my lookupTabWithUrl("chrome://newtab/")
if found then
set targetWindow's active tab index to targetTabIndex
set URL of targetTab to theURL
tell targetWindow to activate
return
end if

-- 3: Create new tab
-- both debugging and empty tab were not found
-- make a new tab with url
tell window 1
activate
make new tab with properties {URL:theURL}
end tell
end tell
end run

-- Function:
-- Lookup tab with given url
-- if found, store tab, index, and window in properties
-- (properties were declared on top of file)
on lookupTabWithUrl(lookupUrl)
tell application "Chrome"
-- Find a tab with the given url
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 as string) contains lookupUrl then
-- assign tab, tab index, and window to properties
set targetTab to theTab
set targetTabIndex to theTabIndex
set targetWindow to theWindow
set found to true
exit repeat
end if
end repeat

if found then
exit repeat
end if
end repeat
end tell
return found
end lookupTabWithUrl

0 comments on commit 051c7cb

Please sign in to comment.