-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support ssh check before clone repo
- Loading branch information
Showing
4 changed files
with
58 additions
and
3 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
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
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,40 @@ | ||
import spawn from 'cross-spawn'; | ||
|
||
async function isSupportSSH(url: string): Promise<any> { | ||
const ret = spawn.sync('ssh', ['-vT', url]); | ||
const stderr = ret?.stderr?.toString(); | ||
|
||
if (/Authentication succeeded/.test(stderr)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function getHostname(url: string): string { | ||
if (/https?/.test(url)) { | ||
const match: any = url.match(/^http(s)?:\/\/(.*?)\//); | ||
return match[2]; | ||
} else { | ||
const match: any = url.match(/@(.*):/); | ||
return match[1]; | ||
} | ||
} | ||
|
||
export async function transformUrl(url: string): Promise<any> { | ||
const hostname = getHostname(url); | ||
const isSSH = await isSupportSSH(`git@${hostname}`); | ||
if (isSSH) { | ||
if (/https?/.test(url)) { | ||
return url.replace(/https?:\/\//, 'git@').replace(/\//, ':'); | ||
} else { | ||
return url; | ||
} | ||
} else { | ||
if (/https?/.test(url)) { | ||
return url; | ||
} else { | ||
return url.replace(`git@${ hostname }:`, `http://${ hostname }/`); | ||
} | ||
} | ||
} |
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