Skip to content

Commit

Permalink
feat: support ssh check before clone repo
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed May 27, 2020
1 parent d1a09d1 commit 8f97fa8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/feflow-cli/src/core/native/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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'
});
}
Expand Down
6 changes: 5 additions & 1 deletion packages/feflow-cli/src/core/universal-pkg/repository/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> {
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();
Expand Down
40 changes: 40 additions & 0 deletions packages/feflow-cli/src/shared/git.ts
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 }/`);
}
}
}
6 changes: 5 additions & 1 deletion packages/feflow-cli/src/shared/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>((resolve, reject) => {
Expand Down Expand Up @@ -66,11 +69,12 @@ export function install(

async function listRepoTag(repoUrl: string): Promise<string[]> {
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[] = [];
Expand Down

0 comments on commit 8f97fa8

Please sign in to comment.