DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
KartikJha
KartikJha

Posted on

Bundle github repos for offline sharing

Looking for a script that will archive the huge repo you've been wanting to archive well here it is...

bundle-repo.js

const chalk = require("chalk");
const { execSync, exec } = require("child_process");
// const fs = require('fs');

if (!process.argv[2] || !process.argv[3]) {
  console.log(
    chalk.red(
      "Usage: node bundle-repo OWNER REPO-STRING [BUNDLE-TARGET-DIR]\nExample: node bundle-repo 'twitter' 'main, side, boom'"
    )
  );
  return;
}

const prefix = "git@github.com:";

const finalDir = process.argv[4] ? process.argv[4] + "/" : "";

const getPath = (r) => (!finalDir ? "" : finalDir + r);

process.argv[3].split(",").forEach((r) => {
  execSync(
    `git clone ${prefix}${process.argv[2]}/${r.trim()} ${getPath(r.trim())}`
  );
  const a = execSync(`ls -la ${getPath(r.trim())}`);
  console.log(a.toString());
  if (a.toString().split('\n')[0].split(' ')[1] != 0) {
    execSync(
      `cd ${getPath(
        r.trim()
      )} && git bundle create ${r.trim()}.bundle --all && mv ${r.trim()}.bundle .. && cd .. && rm -rf ${r.trim()}`
    );
  }
});

package.json

{
  "type": "module",
  "dependencies": {
    "chalk": "^3.0.0"
  }
}

Before you get busy with it please follow this setup to avoid any operational glitches

  1. Setup SSH access for your github account which has access to the repos. Connect to github via SSH

  2. Create a string of comma seprated repo names like "repo1, repo2", the script has a usage example built in

  3. Pass the repo string and owner/organization string as input to the string like

node bundle-repo 'twitter' 'main, side, boom' [optional target directory]

That's it you are good to go. Checkout the code on github

Thank you for reading :)

Top comments (0)

🤯

"I made 10x faster JSON.stringify() functions, even type safe"

☝️ Must read for JS devs