#!/usr/bin/env node

'use strict';

const cp = require('child_process');
const path = require('path');

const main = function () {
  const entryPointDir = getEntryPoint();
  const gitDirs = getGitDirs(entryPointDir);
  const results = runGit(entryPointDir, gitDirs);
  console.log(JSON.stringify(results, '\n', 2));
};

const getEntryPoint = function () {
  return cp.execSync(
    `cd $HOME/Repository && pwd`,
    { encoding: 'utf8' }
  ).trim();
};

const getGitDirs = function (dir) {
  return cp.execSync(
    `find ${dir} -type d -name ".git" -maxdepth 4`,
    { encoding: 'utf8' }
  ).trim().split('\n').map((line) => {
    return path.relative(dir, line.slice(0, -4));
  });
};

const runGit = function (entryPointDir, gitDirs) {
  const result = {};
  gitDirs.forEach((dir) => {

    result[dir] = {
      branch: cp.execSync(
        `git rev-parse --abbrev-ref HEAD`,
        {
          encoding: 'utf8',
          cwd: `${entryPointDir}/${dir}`
        }
      ).trim()
    };

    cp.execSync(
      `git status --porcelain`,
      {
        encoding: 'utf8',
        cwd: `${entryPointDir}/${dir}`
      }
    ).trim().split('\n').forEach((line) => {
      if (line === '') {
        return;
      }

      const [status, localPath] = line.split(' ');
      if (result[dir][status] === undefined) {
        result[dir][status] = [];
      }
      result[dir][status].push(localPath);
    });
  });
  return result;
};

// ====
main();

#!/usr/bin/env node

'use strict';

const cp = require('child_process');
const path = require('path');

const main = function () {
  const entryPointDir = getEntryPoint();
  const gitDirs = getGitDirs(entryPointDir);
  const results = runGit(entryPointDir, gitDirs);
  console.log(JSON.stringify(results, '\n', 2));
};

const getEntryPoint = function () {
  return cp.execSync(
    `cd $HOME/Repository && pwd`,
    { encoding: 'utf8' }
  ).trim();
};

const getGitDirs = function (dir) {
  return cp.execSync(
    `find ${dir} -type d -name ".git" -maxdepth 4`,
    { encoding: 'utf8' }
  ).trim().split('\n').map((line) => {
    return path.relative(dir, line.slice(0, -4));
  });
};

const runGit = function (entryPointDir, gitDirs) {
  const result = {};
  gitDirs.forEach((dir) => {

    try {
      result[dir] = {
        branch: cp.execSync(
          `git rev-parse --abbrev-ref HEAD`,
          {
            encoding: 'utf8',
            cwd: `${entryPointDir}/${dir}`
          }
        ).trim()
      };
    } catch (err) {
      console.log(`${dir} is a git repo, but no branch yet. (probably just git init)`);
      result[dir] = {
        branch: '(NONE YET)'
      };
      return;
    }

    cp.execSync(
      `git status --porcelain`,
      {
        encoding: 'utf8',
        cwd: `${entryPointDir}/${dir}`
      }
    ).trim().split('\n').forEach((line) => {
      if (line === '') {
        return;
      }

      const [status, localPath] = line.trim().split(' ');
      if (result[dir][status] === undefined) {
        result[dir][status] = [];
      }
      result[dir][status].push(localPath);
    });
  });
  return result;
};

// ====
main();

Fix for find_wip when no branch is there