簡體   English   中英

如何使用 node.js 克隆 github repo

[英]How to clone github repo using node.js

我需要一種可靠的方法來克隆 github 存儲庫並使用 node.js 和任何必要的 npm 包將其粘貼到本地目錄中。

此代碼使用 nodegit 庫,無法克隆 github 存儲庫。 它創建了一個名為 .git 的文件夾,並且不從 repo 復制任何文件。 我嘗試了幾個庫,其中大多數庫都具有極其復雜的代碼或不起作用。 這以前有效,但現在不行。 (它隨心所欲地打開和關閉)。 請幫助,我需要一個可靠的代碼來從 url 克隆一個 github 存儲庫並將其粘貼到本地目錄中。 謝謝你。

var nodegit = require('nodegit'),
    path = require('path');

var url = "https://github.com/atomicptr/dauntless-builder", //also tried https://github.com/atomicptr/dauntless-builder.git
    local = "C:/data",
    cloneOpts = {};

nodegit.Clone(url, local, cloneOpts).then(function (repo) {
    console.log("cloning succesful!");
    console.log("Cloned " + path.basename(url) + " to " + repo.workdir());
}).catch(function (err) {
    console.log(err);
});

此代碼未顯示任何錯誤,但實際上無法克隆存儲庫。

您可以為此使用shelljs

const shell = require('shelljs')
const path = 'absolute/path/to/folder'
shell.cd(path)
shell.exec('git clone https://github.com/atomicptr/dauntless-builder')

假設您在機器上安裝了 git,您可以簡單地從 node 運行 clone 命令。

const path = require('path');
const{ execSync } = require('child_process');

execSync('git clone repolink', {
  stdio: [0, 1, 2], // we need this so node will print the command output
  cwd: path.resolve(__dirname, ''), // path to where you want to save the file
})

試試git-clone npm 包

npm i git-clone
var clone = require('git-clone');

clone(repo, targetPath, [options], cb);

支持的選項:

git: git 二進制文件的路徑; 默認值:git(可選)。

淺:當為真時,克隆深度為 1(可選)。

結帳:要結帳的修訂/分支/標簽(可選)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM