Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone github repo using node.js

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.

like image 742
Siddharth Agrawal Avatar asked Aug 27 '19 06:08

Siddharth Agrawal


2 Answers

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')
like image 190
Jasper Bernales Avatar answered Sep 19 '22 06:09

Jasper Bernales


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
})
like image 23
robi932 Avatar answered Sep 19 '22 06:09

robi932