{"size": 1710, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "afzaalb/pr-notifier", "max_stars_repo_stars_event_min_datetime": "2020-12-26T15:13:57.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-26T15:13:57.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "afzaalb/pr-notifier", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "afzaalb/pr-notifier", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.5294117647, "max_line_length": 196, "alphanum_fraction": 0.6964912281}
#!/usr/bin/env node
const childProcess = require("child_process");
const notifier = require("node-notifier");
const getUserName = require("git-user-name");
const parse = require("parse-diff");
const args = require("minimist")(process.argv.slice(2));

const getWorkingDirectoryChanges = (gitDiff) => {
  const files = parse(gitDiff);
  let numberOfChanges = 0;
  files.forEach(function (file) {
    // numberOfChanges += file.deletions;
    // numberOfChanges += file.additions;
    numberOfChanges += file.chunks[0].changes.length;
  });

  return numberOfChanges;
};

const notifyAuthorForPullRequest = (allowedPRLines) => {
  const firstName = getUserName().split(" ")[0];
  const notificationIcon =
    args["icon"] || "https://afzaalb.vercel.app/common/afzaalb.jpg";

  notifier.notify({
    title: firstName + ", Time for Pull Request",
    message: "PR limit for " + allowedPRLines + " line(s) has exceeded.",
    icon: notificationIcon, // Absolute path (doesn't work on balloons)
    sound: true, // Only Notification Center or Windows Toasters
    wait: true, // Wait with callback, until user action is taken against notification, does not apply to Windows Toasters as they always wait or notify-send as it does not support the wait option
  });
};

const pullRequestNotifier = () => {
  const allowedPRLines = args["allowedLines"] || 100;

  childProcess.exec("git diff", (err, stdout) => {
    if (err) {
      return;
    }

    const numberOfChanges = getWorkingDirectoryChanges(stdout);
    // Only Notify for PR when limit is reached/exceeded
    if (numberOfChanges >= allowedPRLines) {
      notifyAuthorForPullRequest(allowedPRLines);
    }
  });
};

module.exports = pullRequestNotifier();
