const fs = require('fs');
const path = require('path');

const FLEXIBILITY = 0.5;
const ROOT = path.join(__dirname, '..', '..', '..');

function getDirectories(parent) {
  return fs.readdirSync(parent)
        .filter(file => fs.statSync(path.join(parent, file)).isDirectory());
}

function getCoverageSummaryPath(coveragePath) {
  if (fs.existsSync(coveragePath)) {
    const directories = getDirectories(coveragePath);
    directories.filter(dir => !/src|node_modules/.test(dir));
    return directories[0];
  }
}
function getCoverageSummary(coveragePath) {
  const coverageSummaryPath = getCoverageSummaryPath(coveragePath);
  const fileContent = fs.readFileSync(path.join(coveragePath, coverageSummaryPath, 'coverage-summary.json'));
  return JSON.parse(fileContent);
}

function getCoverageSummaryTotal(coveragePath) {
  const coverageSummary = getCoverageSummary(coveragePath);

  const res = {};
  ['statements', 'branches', 'functions', 'lines'].forEach(key => res[key] = coverageSummary.total[key].pct - FLEXIBILITY);
  return res;
}


const coveragePath = path.join(ROOT, 'coverage');
const summaryTotal = getCoverageSummaryTotal(coveragePath);

fs.writeFileSync(path.join(ROOT, 'coverage.conf.json'), `${JSON.stringify(summaryTotal, null, 2)}\n`, 'utf8');

console.log('coverage-updater: updating \"coverage.conf.json\":\n', summaryTotal);

@@ -163,16 +163,55 @@
 rent) %7B%0A
+    if (fs.existsSync(parent)) %7B%0A      
   return
@@ -234,16 +234,20 @@
 parent)%0A
+    
         
@@ -319,78 +319,79 @@
 ));%0A
-%7D%0A%0Afunction getCoverageSummaryPath(coveragePath) %7B%0A  if (fs.existsSync
+    %7D else %7B%0A        return %5B%5D;%0A    %7D%0A%7D%0A%0Afunction getCoverageSummaryDir
 (cov
@@ -400,17 +400,16 @@
 agePath)
-)
  %7B%0A    c
@@ -545,16 +545,38 @@
 ries
-%5B0%5D;%0A  %7D
+.length ? directories%5B0%5D : '';
 %0A%7D%0Af
@@ -612,32 +612,34 @@
 veragePath) %7B%0A  
+ 
+ 
 const coverageSu
@@ -643,20 +643,19 @@
 eSummary
-Path
+Dir
  = getCo
@@ -667,20 +667,19 @@
 eSummary
-Path
+Dir
 (coverag
@@ -686,24 +686,26 @@
 ePath);%0A
+  
   const 
 fileCont
@@ -700,38 +700,30 @@
 nst 
-fileContent = fs.readFileSync(
+coverageSummaryPath = 
 path
@@ -757,20 +757,19 @@
 eSummary
-Path
+Dir
 , 'cover
@@ -786,27 +786,142 @@
 y.json')
+;%0A    const fileContent = fs.existsSync(coverageSummaryPath) && fs.readFileSync(coverageSummaryPath
 );%0A  
+ 
+ 
 return
+ fileContent ?
  JSON.pa
@@ -936,16 +936,21 @@
 Content)
+ : %7B%7D
 ;%0A%7D%0A%0Afun
@@ -995,16 +995,18 @@
 Path) %7B%0A
+  
   const 
@@ -1057,17 +1057,18 @@
 ePath);%0A
-%0A
+  
   const 
@@ -1077,16 +1077,18 @@
 s = %7B%7D;%0A
+  
   %5B'stat
@@ -1155,16 +1155,40 @@
 s%5Bkey%5D =
+ coverageSummary.total ?
  coverag
@@ -1224,19 +1224,25 @@
 XIBILITY
+ : 0
 );%0A
+  
   return

fix nulls