Siddharth Agrawal
Siddharth Agrawal

Reputation: 366

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.

Upvotes: 14

Views: 41180

Answers (4)

jacktrader
jacktrader

Reputation: 689

I didn't have access to installing Git on a Linux machine I was developing on (running on a web server), so running into this issue enough, I wrote a script in Node to use the Git API and do just that. And it honestly isn't that crazy at <100 lines. Also, it should work with vanilla node (which happened to be installed).

P.S. I've tested it on both Windows and Linux.

const fs = require('fs');
const path = require('path');
const https = require('https');

// Parse command-line arguments
const [repoOwner, repoName, branch] = process.argv.slice(2);

if (!repoOwner || !repoName || !branch) {
    console.error('Usage: node clone-repo.js <repoOwner> <repoName> <branch>');
    process.exit(1);
}

const baseUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contents`;

function fetchRepoContents(url) {
    return new Promise((resolve, reject) => {
        https.get(url, { headers: { 'User-Agent': 'node.js' } }, (res) => {
            let data = '';
            res.on('data', (chunk) => {
                data += chunk;
            });
            res.on('end', () => {
                resolve(JSON.parse(data));
            });
        }).on('error', (err) => {
            reject(err);
        });
    });
}

async function downloadFile(url, filePath) {
    return new Promise((resolve, reject) => {
        https.get(url, { headers: { 'User-Agent': 'node.js' } }, (res) => {
            const fileStream = fs.createWriteStream(filePath);
            res.pipe(fileStream);
            fileStream.on('finish', () => {
                fileStream.close();
                resolve();
            });
        }).on('error', (err) => {
            fs.unlink(filePath, () => {}); // Delete the file async. (avoid using callback)
            reject(err);
        });
    });
}

async function processContents(contents, basePath) {
    for (const item of contents) {
        const fullPath = path.join(basePath, item.name);
        if (item.type === 'dir') {
            fs.mkdirSync(fullPath, { recursive: true });
            console.log(`Created directory: ${fullPath}`);
            const dirContents = await fetchRepoContents(item.url);
            await processContents(dirContents, fullPath);
        } else if (item.type === 'file') {
            try {
                // Ensure the directory exists before downloading the file
                const dirPath = path.dirname(fullPath);
                if (!fs.existsSync(dirPath)) {
                    fs.mkdirSync(dirPath, { recursive: true });
                    console.log(`Created directory: ${dirPath}`);
                }
                await downloadFile(item.download_url, fullPath);
                console.log(`Downloaded file: ${fullPath}`);
            } catch (err) {
                console.error(`Error downloading file ${fullPath}:`, err);
            }
        } else {
            console.warn(`Unknown type for ${fullPath}: ${item.type}`);
        }
    }
}

async function main() {
    try {
        const contents = await fetchRepoContents(`${baseUrl}?ref=${branch}`);
        await processContents(contents, repoName);
        console.log('Repository cloned successfully');
    } catch (err) {
        console.error('Error cloning repository:', err);
    }
}

main();

Usage:

node clone-repo.js repoOwner repoName branch

Upvotes: 0

Akash Sengar
Akash Sengar

Reputation: 149

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).

Upvotes: 7

Jasper Bernales
Jasper Bernales

Reputation: 1681

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')

Upvotes: 21

robi932
robi932

Reputation: 169

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
})

Upvotes: 10

Related Questions