11#!/usr/bin/env node
22const gitRemoteOriginUrl = require ( "git-remote-origin-url" ) ;
3- const branchName = require ( ' current-git-branch' ) ;
3+ const branchName = require ( " current-git-branch" ) ;
44const open = require ( "open" ) ;
5- const chalk = require ( 'chalk' ) ;
6- const warning = chalk . keyword ( 'orange' ) ;
5+ const chalk = require ( "chalk" ) ;
6+ const warning = chalk . keyword ( "orange" ) ;
7+ const error = chalk . keyword ( "red" ) ;
78
89( async ( ) => {
9- const remoteOriginUrl = await gitRemoteOriginUrl ( ) ;
10+ try {
11+ const remoteOriginUrl = await gitRemoteOriginUrl ( ) ;
1012
11- const repoUrl = remoteOriginUrl
12- . replace ( ":" , "/" )
13- . replace ( / ^ g i t @ / , "https://" )
14- . replace ( / \. g i t $ / , "" ) ;
13+ const repoUrl = remoteOriginUrl
14+ . replace ( ":" , "/" )
15+ . replace ( / ^ g i t @ / , "https://" )
16+ . replace ( / \. g i t $ / , "" ) ;
1517
16- const pullRequestURL = getPullRequestUrl ( repoUrl ) ;
17- if ( ! pullRequestURL ) {
18- return ;
19- }
18+ const pullRequestURL = getPullRequestUrl ( repoUrl ) ;
19+ if ( ! pullRequestURL ) {
20+ return ;
21+ }
2022
21- await open ( pullRequestURL , { app : getBrowser ( ) } ) ;
23+ await open ( pullRequestURL , { app : getBrowser ( ) } ) ;
24+ } catch ( err ) {
25+ console . log (
26+ error ( err . message . charAt ( 0 ) . toUpperCase ( ) + err . message . slice ( 1 ) )
27+ ) ;
28+ }
2229} ) ( ) ;
2330
2431/**
@@ -30,7 +37,7 @@ function getPullRequestUrl(repoUrl) {
3037 if ( url . host === "github.com" ) {
3138 return `${ url } /pull/new/${ branchName ( ) } ` ;
3239 } else if ( url . host === "bitbucket.org" ) {
33- return `${ url } /pull-requests/new?source=${ branchName ( ) } &t=1`
40+ return `${ url } /pull-requests/new?source=${ branchName ( ) } &t=1` ;
3441 }
3542
3643 return "" ;
@@ -41,20 +48,24 @@ function getPullRequestUrl(repoUrl) {
4148 */
4249function getBrowser ( ) {
4350 const browsers = {
44- " chrome" : "chrome" ,
45- " firefox" : "firefox" ,
46- " edge" : "msedge" ,
47- " safari" : "safari" ,
48- "ie" : "iexplore" ,
49- " opera" : "opera"
50- }
51+ chrome : "chrome" ,
52+ firefox : "firefox" ,
53+ edge : "msedge" ,
54+ safari : "safari" ,
55+ ie : "iexplore" ,
56+ opera : "opera" ,
57+ } ;
5158
5259 const [ , , userInput ] = process . argv ;
5360 const browser = browsers [ userInput ] ;
5461
5562 if ( userInput && ! browser ) {
56- console . log ( warning ( `Browser "${ userInput } " was not found, we will use your default browser.` ) ) ;
63+ console . log (
64+ warning (
65+ `Browser "${ userInput } " was not found, we will use your default browser.`
66+ )
67+ ) ;
5768 }
5869
5970 return browser || "" ;
60- }
71+ }
0 commit comments