This website requires JavaScript.
GitHub Graph

Brightly decorate your GitHub Graph

Impress viewers with just 40 lines of code

3 min read

I. Motivation

In the past I was searching for good libraries of npm and I stumbled upon a library called simple-git. This is a library that runs git commands on nodejs. And then I immediately thought of the contributions chart on github, is there a way to make this chart green? And so this article was born.

II. Setup

1. Initial Nodejs project and install dependencies

npm
yarn
pnpm
bun
npm init
npm
yarn
pnpm
bun
npm install jsonfile moment random simple-git

2. Fake data

Create a file named data.json at the root of the project to store changes when committing new code.

data.json
add something here

3. Insert script

Create a javascript file name index.js with the following content at the root of the project.

index.js
import jsonfile from 'jsonfile';
import moment from 'moment';
import simpleGit from 'simple-git';
import random from 'random';

const path = './data.json';

function isValidDate(date) {
  const startDate = moment('2019-01-01');
  const endDate = moment('2024-12-13');
  return date.isBetween(startDate, endDate, null, '[]');
}

async function markCommit(date) {
  const data = { date: date.toISOString() };
  await jsonfile.writeFile(path, data);

  const git = simpleGit();
  await git.add([path]);
  await git.commit(date.toISOString(), { '-d': date.toISOString() });
}

async function makeCommits(n) {
  const git = simpleGit();

  for (let i = 0; i < n; i++) {
    const randomDays = random.int(0, 6);
    const randomWeeks = random.int(0, 54 * 4);

    const randomDate = moment('2019-01-01')
      .add(randomDays, 'days')
      .add(randomWeeks, 'weeks');

    if (isValidDate(randomDate)) {
      console.log(`Creating commit: ${randomDate.toISOString()}`);
      await markCommit(randomDate);
    } else {
      console.log(`Invalid date: ${randomDate.toISOString()}, skipping...`);
    }
  }

  console.log('Pushing all commits...');
  await git.push();
}

makeCommits(50000);

4. Action

Initialize the git repo (if empty), skip this step if you already have a git repo.

git init

Run the script to create commit.

node index.js

Finally, push code to remote then enjoy the result.

  • Tags:
  • git
  • github
Dec 24, 2024
Previous page
Setup Docker on Window
Next page
Download specific folder from GitHub

šŸƒ Related posts