From 0ba1465be73b891f0092b0cfb050b7c7bd6baa45 Mon Sep 17 00:00:00 2001 From: FliPPeDround <734243792@qq.com> Date: Thu, 29 Jun 2023 17:58:55 +0800 Subject: [PATCH] feat: cloneRepo --- src/utils/cloneRepo.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/utils/cloneRepo.ts diff --git a/src/utils/cloneRepo.ts b/src/utils/cloneRepo.ts new file mode 100644 index 0000000..1dff159 --- /dev/null +++ b/src/utils/cloneRepo.ts @@ -0,0 +1,31 @@ +import { exec } from 'node:child_process' +import { promises as fs } from 'node:fs' +import { join } from 'node:path' + +async function removeGitFolder(localPath: string): Promise { + const gitFolderPath = join(localPath, '.git') + await fs.rm(gitFolderPath, { recursive: true, force: true }) +} + +export function cloneRepo(gitUrl: string, localPath: string): Promise { + return new Promise((resolve, reject) => { + exec(`git clone ${gitUrl} ${localPath}`, async (error, stdout, stderr) => { + if (error) { + console.error(`exec error: ${error}`) + reject(error) + return + } + console.log(`stdout: ${stdout}`) + console.error(`stderr: ${stderr}`) + + try { + await removeGitFolder(localPath) + resolve() + } + catch (error) { + console.error('Failed to remove .git folder: ', error) + reject(error) + } + }) + }) +}