问题
I need a reliable way to clone a github repo and paste it into a local directory using node.js and any necessary npm packages.
This code is using the nodegit library and doesn't work to clone a github repo. it creates a single folder named .git and copies none of the files from the repo. I have tried several libraries most of which have extremely complicated code or don't work. This was working before but now isn't. (it goes on and off as it pleases). pls help, I need a reliable code that clones a github repo from url and pastes it into a local directory. Thank you.
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);
});
this code shows no errors, yet doesn't actually work to clone the repo.
回答1:
You can use shelljs for this.
const shell = require('shelljs')
const path = 'absolute/path/to/folder'
shell.cd(path)
shell.exec('git clone https://github.com/atomicptr/dauntless-builder')
回答2:
Assuming you have git installed on the machine you could simply run the clone command from node.
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
})
回答3:
Try the git-clone npm package
npm i git-clone
var clone = require('git-clone');
clone(repo, targetPath, [options], cb);
Supported options:
git: path to git binary; default: git (optional).
shallow: when true, clone with depth 1 (optional).
checkout: revision/branch/tag to check out (optional).
来源:https://stackoverflow.com/questions/57669037/how-to-clone-github-repo-using-node-js