diff options
| author | Lukas Eipert <leipert@gitlab.com> | 2018-06-14 18:36:22 +0000 |
|---|---|---|
| committer | Fabien Catteau <fcatteau@gitlab.com> | 2018-06-14 18:36:22 +0000 |
| commit | 2bf9bc8145a881b7b6e17423ab8dfccbfd138fc7 (patch) | |
| tree | cb42f99f76906d6ae652dc0b0dfc1ad5256cbf59 /html2json.js | |
| parent | ae265f62905777bd9df4c6397399413794afb15e (diff) | |
Ensure that license name order is consistent
Diffstat (limited to 'html2json.js')
| -rw-r--r-- | html2json.js | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/html2json.js b/html2json.js index 841149a..8e9f5c6 100644 --- a/html2json.js +++ b/html2json.js @@ -8,6 +8,7 @@ try { htmlContent = fs.readFileSync(process.argv[2], 'utf8'); } catch(e) { console.log('Error:', e.stack); + process.exit(1); } // Get the directory containing the results to make pathes relative to it later. @@ -15,16 +16,8 @@ report_directory = path.dirname(process.argv[2]) const $ = cheerio.load(htmlContent) -// Extract licenses and the number of occurences. -var licenses = []; -$('div.summary div.row').children().first().find('ul li').each(function(i, doc) { - tmp = $(this).text(); - matches = tmp.match(/^([0-9]+) ((\s|\S)*)/m) - licenses.push({ - count: parseInt(matches[1], 10), - name: matches[2] - }) -}) +// Map that keeps the total tally of the license occurrences +var licenses = {}; // Extract dependencies info. var dependencies = [] @@ -33,6 +26,12 @@ $('div.dependencies div').each(function(i, doc) { license = $(this).find('blockquote p').text().trim(); license = license.split("\n")[0]; + if(licenses[license]) { + licenses[license].count += 1; + } else { + licenses[license] = { count: 1, name: license } + } + // Get URL. license_url = $(this).find('blockquote p a[href]').attr('href'); @@ -72,6 +71,15 @@ $('div.dependencies div').each(function(i, doc) { }) }) +// Stable sort of licenses. First license count descending, then license name ascending +licenses = Object.values(licenses) + .sort(function (a, b) { + if (a.count === b.count) { + return a.name > b.name ? 1 : -1; + } + return a.count < b.count ? 1 : -1; +}); + console.log(JSON.stringify({ licenses: licenses, dependencies: dependencies}, null, 4)) |
