diff --git a/lib/enhanceReport.js b/lib/enhanceReport.js index ec06675..6e67d28 100644 --- a/lib/enhanceReport.js +++ b/lib/enhanceReport.js @@ -158,19 +158,24 @@ function encodeMediaPath(p) { } function extrapolateBaseFolder(results) { - //Snatched from https://rosettacode.org/wiki/Find_common_directory_path#JavaScript - const splitStrings = (a, sep = path.sep) => a.map((i) => i.split(sep)); - const elAt = (i) => (a) => a[i]; - const rotate = (a) => a[0].map((e, i) => a.map(elAt(i))); const allElementsEqual = (arr) => arr.every((e) => e === arr[0]); - const commonPath = (input, sep = path.sep) => - rotate(splitStrings(input, sep)).filter(allElementsEqual).map(elAt(0)).join(sep); + const directoryArrays = results.map((result) => + path.dirname(result.fullFile).split(path.sep) + ); + const minLength = Math.min(...directoryArrays.map((arr) => arr.length)); + let commonPathArray = []; - let directoryArray = []; - results.forEach((result) => { - directoryArray = directoryArray.concat(path.dirname(result.fullFile)); - }); - const baseFolder = commonPath(directoryArray); + for (let i = 0; i < minLength; i++) { + const elementsAtCurrentIndex = directoryArrays.map((arr) => arr[i]); + + if (allElementsEqual(elementsAtCurrentIndex)) { + commonPathArray.push(elementsAtCurrentIndex[0]); + } else { + break; + } + } + + const baseFolder = commonPathArray.join(path.sep); debugLog(`baseFolder: ${baseFolder}`); return baseFolder; }