-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@yueqing/dev-utils': minor | ||
--- | ||
|
||
add dev-utils for cli helpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@yueqing/dev-utils", | ||
"version": "0.0.0", | ||
"description": "yueqing cli utils", | ||
"main": "./lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/simonwong/yueqing.git", | ||
"directory": "packages/webpack" | ||
}, | ||
"author": "Simon <wsj_simon@163.com>", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"build": "rm -rf ./lib && tsc -p tsconfig.json && cp ./src/openChrome.applescript ./lib/openChrome.applescript", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"open": "^8.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function clearConsole() { | ||
process.stdout.write( | ||
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H', | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './clearConsole' | ||
export * from './openBrowser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { execSync } from 'child_process' | ||
import open from 'open' | ||
|
||
export function openBrowser(url: string) { | ||
// If we're on OS X, the user hasn't specifically | ||
// requested a different browser, we can try opening | ||
// Chrome with AppleScript. This lets us reuse an | ||
// existing tab when possible instead of creating a new one. | ||
const shouldTryOpenChromiumWithAppleScript = process.platform === 'darwin' | ||
|
||
if (shouldTryOpenChromiumWithAppleScript) { | ||
// Will use the first open browser found from list | ||
const supportedChromiumBrowsers = [ | ||
'Google Chrome Canary', | ||
'Google Chrome Dev', | ||
'Google Chrome Beta', | ||
'Google Chrome', | ||
'Microsoft Edge', | ||
'Brave Browser', | ||
'Vivaldi', | ||
'Chromium', | ||
] | ||
|
||
for (const chromiumBrowser of supportedChromiumBrowsers) { | ||
try { | ||
// Try our best to reuse existing tab | ||
// on OSX Chromium-based browser with AppleScript | ||
execSync(`ps cax | grep "${chromiumBrowser}"`) | ||
execSync( | ||
`osascript openChrome.applescript "${encodeURI( | ||
url, | ||
)}" "${chromiumBrowser}"`, | ||
{ | ||
cwd: __dirname, | ||
stdio: 'ignore', | ||
}, | ||
) | ||
return true | ||
} catch (err) { | ||
// Ignore errors. | ||
} | ||
} | ||
} | ||
|
||
// Fallback to open | ||
// (It will always open new tab) | ||
try { | ||
open(url).catch(() => {}) // Prevent `unhandledRejection` error. | ||
return true | ||
} catch (err) { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
(* | ||
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 | ||
property theProgram: "Google Chrome" | ||
|
||
on run argv | ||
set theURL to item 1 of argv | ||
|
||
-- Allow requested program to be optional, | ||
-- default to Google Chrome | ||
if (count of argv) > 1 then | ||
set theProgram to item 2 of argv | ||
end if | ||
|
||
using terms from application "Google Chrome" | ||
tell application theProgram | ||
|
||
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 using terms from | ||
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) | ||
using terms from application "Google Chrome" | ||
tell application theProgram | ||
-- 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 | ||
end using terms from | ||
return found | ||
end lookupTabWithUrl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "./lib", | ||
}, | ||
"include": [ | ||
"./src", | ||
], | ||
"exclude": [ | ||
"**/node_modules/**" | ||
] | ||
} |