loading...

Bundle github repos for offline sharing

kartikjha profile image KartikJha ・2 min read

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

Posted on by:

kartikjha profile

KartikJha

@kartikjha

Software engineer specializing in building applications in JavaScript and Node.js ecosystem. Apart from these I also have work experience in Java ecosystem and React.js.

Discussion

markdown guide