const childProcess = require('child_process');
const compact = require('lodash/compact');
const console = require('console');
const chalk = require('chalk');
const fromPairs = require('lodash/fromPairs');
const reduce = require('lodash/reduce');
const reverse = require('lodash/reverse');
const orderBy = require('lodash/orderBy');

const execSync = childProcess.execSync;

const commitsCommand = 'git shortlog -s -n --all --no-merges';
const commitsCommandResult = execSync(commitsCommand).toString('utf8');
const commitStats = compact(commitsCommandResult.split('\n'));

const commitStatsPerAuthors = fromPairs(commitStats.map(stat => (
  reverse(stat.split('\t'))
)));

const authors = [
  {
    names: [
      'caedes',
    ],
  },
  {
    names: [
      'maxime.degenne',
      'CDBDX\\maxime.degenne',
      'U-CDBDX\\maxime.degenne',
    ],
  },
  {
    names: [
      'Pierre Nedelec',
    ],
  },
  {
    names: [
      'pascal.maria',
    ],
  },
  {
    names: [
      'Adrien Fillon',
    ],
  },
];

const stats = authors.map((author) => {
  const authorStats = author.names.map((name) => {
    const commits = parseInt(commitStatsPerAuthors[name], 10) || 0;

    const lineStatsCommand = `git log --no-merges --author="${name}" --pretty=tformat: --numstat`;
    const lineStatsCommandResult = execSync(lineStatsCommand).toString('utf8');
    const lineStats = lineStatsCommandResult.split('\n');
    const lineStatsResult = lineStats.map(lineStat => (
      lineStat.split('\t')
    ));
    const addedStatsResult = compact(lineStatsResult.map(lineStatResult => (
      parseInt(lineStatResult[0], 10)
    )));
    const removedStatsResult = compact(lineStatsResult.map(lineStatResult => (
      parseInt(lineStatResult[1], 10)
    )));

    const added = reduce(addedStatsResult, (a, b) => (a + b), 0);
    const removed = reduce(removedStatsResult, (a, b) => (a + b), 0);

    return {
      commits,
      added,
      removed,
    };
  });

  const commits = reduce(authorStats.map(stat => stat.commits), (a, b) => (a + b), 0);
  const added = reduce(authorStats.map(stat => stat.added), (a, b) => (a + b), 0);
  const removed = reduce(authorStats.map(stat => stat.removed), (a, b) => (a + b), 0);

  return {
    author: author.names[0],
    commits,
    added,
    removed,
  };
});

const orderedStats = orderBy(stats, ['added', 'removed'], ['desc', 'desc']);

orderedStats.forEach((stat) => {
  const author = chalk.bold(stat.author);
  const commits = `${stat.commits} commits`;
  const added = chalk.green(`${stat.added} ++`);
  const removed = chalk.red(`${stat.removed} --`);

  const authorStats = [commits, added, removed].join(' / ');

  console.log(`${author}: ${authorStats}`);
});

@@ -683,390 +683,193 @@
 hors
- = %5B%0A  %7B%0A    names: %5B%0A      'caedes',%0A    %5D,%0A  %7D,%0A  %7B%0A    names: %5B%0A      'maxime.degenne',%0A      'CDBDX%5C%5Cmaxime.degenne',%0A      'U-CDBDX%5C%5Cmaxime.degenne',%0A    %5D,%0A  %7D,%0A  %7B%0A    names: %5B%0A      'Pierre Nedelec',%0A    %5D,%0A  %7D,%0A  %7B%0A    names: %5B%0A      'pascal.maria',%0A    %5D,%0A  %7D,%0A  %7B%0A    names: %5B%0A      'Adrien Fillon',%0A    %5D,%0A  %7D,%0A%5D;%0A%0Aconst stats = authors.map((author) =%3E %7B%0A  const authorS
+Command = %22git log --format='%25aN' %7C sort -u%22;%0Aconst authorsCommandResult = execSync(authorsCommand).toString('utf8');%0Aconst authors = compact(authorsCommandResult.split('%5Cn'));%0A%0Aconst s
 tats
@@ -881,33 +881,28 @@
 thor
-.name
 s.map((
-name
+author
 ) =%3E %7B%0A
-  
   co
@@ -946,20 +946,22 @@
 Authors%5B
-name
+author
 %5D, 10) %7C
@@ -966,18 +966,16 @@
  %7C%7C 0;%0A%0A
-  
   const 
@@ -1026,20 +1026,22 @@
 thor=%22$%7B
-name
+author
 %7D%22 --pre
@@ -1065,18 +1065,16 @@
 mstat%60;%0A
-  
   const 
@@ -1139,26 +1139,24 @@
 ng('utf8');%0A
-  
   const line
@@ -1197,26 +1197,24 @@
 it('%5Cn');%0A  
- 
- 
 const lineSt
@@ -1257,18 +1257,16 @@
 %3E (%0A    
-  
 lineStat
@@ -1282,18 +1282,16 @@
 t')%0A
-  
   ));%0A  
   co
@@ -1282,26 +1282,24 @@
 t')%0A  ));%0A  
- 
- 
 const addedS
@@ -1355,34 +1355,32 @@
 Result =%3E (%0A    
-  
 parseInt(lineSta
@@ -1399,19 +1399,15 @@
 10)%0A
-  
   )));%0A
-  
   co
@@ -1483,18 +1483,16 @@
 %3E (%0A    
-  
 parseInt
@@ -1519,18 +1519,16 @@
 10)%0A
-  
   )));%0A%0A
     
@@ -1523,18 +1523,16 @@
   )));%0A%0A
-  
   const 
@@ -1587,18 +1587,16 @@
 b), 0);%0A
-  
   const 
@@ -1656,18 +1656,16 @@
 ), 0);%0A%0A
-  
   return
@@ -1675,357 +1675,14 @@
     
-  commits,%0A      added,%0A      removed,%0A    %7D;%0A  %7D);%0A%0A  const commits = reduce(authorStats.map(stat =%3E stat.commits), (a, b) =%3E (a + b), 0);%0A  const added = reduce(authorStats.map(stat =%3E stat.added), (a, b) =%3E (a + b), 0);%0A  const removed = reduce(authorStats.map(stat =%3E stat.removed), (a, b) =%3E (a + b), 0);%0A%0A  return %7B%0A    author: author.names%5B0%5D
+author
 ,%0A  

Add dynamic authors