This website requires JavaScript.
GitHub Graph

Brightly decorate your GitHub Graph

Impress viewers by sleek profile with just 40 lines of code

3 min read

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

🏗️ 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.

  • git
  • github
Dec 24, 2024
Previous page
Setup Docker environment on Windows
Next page
Download a specific folder on GitHub repository

🍃 Related posts

  • Syncing_Git_Tags_image

    Syncing Git Tags across to other repositories

  • GitHub_Achievements_image

    Unlocking GitHub Achievement badges

  • GitHub_downloaded_specific_folder_img

    Download a specific folder on GitHub repository