From 8f97fa87e035391ba505f17b67abf956b746f90d Mon Sep 17 00:00:00 2001 From: cpselvis Date: Wed, 27 May 2020 14:17:00 +0800 Subject: [PATCH] feat: support ssh check before clone repo --- .../feflow-cli/src/core/native/install.ts | 9 ++++- .../src/core/universal-pkg/repository/git.ts | 6 ++- packages/feflow-cli/src/shared/git.ts | 40 +++++++++++++++++++ packages/feflow-cli/src/shared/npm.ts | 6 ++- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 packages/feflow-cli/src/shared/git.ts diff --git a/packages/feflow-cli/src/core/native/install.ts b/packages/feflow-cli/src/core/native/install.ts index b68c0c4c..0052be36 100644 --- a/packages/feflow-cli/src/core/native/install.ts +++ b/packages/feflow-cli/src/core/native/install.ts @@ -16,13 +16,20 @@ import { FEFLOW_BIN, FEFLOW_LIB } from '../../shared/constant'; +import { + transformUrl +} from '../../shared/git'; import { Plugin } from '../universal-pkg/schema/plugin'; import Linker from '../universal-pkg/linker'; import { UniversalPkg } from '../universal-pkg/dep/pkg'; import versionImpl from '../universal-pkg/dep/version'; + async function download(url: string, filepath: string): Promise { - return spawn.sync('git', ['clone', url, filepath], { + const cloneUrl = await transformUrl(url); + + console.log('cloneUrl', cloneUrl); + return spawn.sync('git', ['clone', cloneUrl, filepath], { stdio: 'inherit' }); } diff --git a/packages/feflow-cli/src/core/universal-pkg/repository/git.ts b/packages/feflow-cli/src/core/universal-pkg/repository/git.ts index 08134069..ab8535dd 100644 --- a/packages/feflow-cli/src/core/universal-pkg/repository/git.ts +++ b/packages/feflow-cli/src/core/universal-pkg/repository/git.ts @@ -2,17 +2,21 @@ import spawn from 'cross-spawn'; import childProcess from 'child_process'; import { promisify } from 'util'; import versionImpl from '../dep/version'; +import { + transformUrl +} from '../../../shared/git'; export async function getTag( repoUrl: string, version?: string ): Promise { const execFile = promisify(childProcess.execFile); + const url = await transformUrl(repoUrl); const { stdout } = await execFile('git', [ 'ls-remote', '--tags', '--refs', - repoUrl + url ]); const tagListStr = stdout?.trim(); diff --git a/packages/feflow-cli/src/shared/git.ts b/packages/feflow-cli/src/shared/git.ts new file mode 100644 index 00000000..de0e1e87 --- /dev/null +++ b/packages/feflow-cli/src/shared/git.ts @@ -0,0 +1,40 @@ +import spawn from 'cross-spawn'; + +async function isSupportSSH(url: string): Promise { + 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 { + 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 }/`); + } + } +} \ No newline at end of file diff --git a/packages/feflow-cli/src/shared/npm.ts b/packages/feflow-cli/src/shared/npm.ts index 46641990..215fb25f 100644 --- a/packages/feflow-cli/src/shared/npm.ts +++ b/packages/feflow-cli/src/shared/npm.ts @@ -2,6 +2,9 @@ import spawn from 'cross-spawn'; import childProcess from 'child_process'; import { promisify } from 'util'; import semver from 'semver'; +import { + transformUrl +} from './git'; export function getRegistryUrl(packageManager: string) { return new Promise((resolve, reject) => { @@ -66,11 +69,12 @@ export function install( async function listRepoTag(repoUrl: string): Promise { const execFile = promisify(childProcess.execFile); + const url = await transformUrl(repoUrl); const { stdout } = await execFile('git', [ 'ls-remote', '--tags', '--refs', - repoUrl + url ]); const tagStr = stdout?.trim(); let tagList: string[] = [];