|
const tablify = require('tablify'); |
|
const path = require('path'); |
|
const fs = require('fs'); |
|
|
|
const modules = fs.readdirSync('node_modules'); |
|
const pkgAndAuthor = modules |
|
.map(m => path.join(__dirname, 'node_modules', m, 'package.json')) |
|
.filter(pkg => fs.existsSync(pkg)) |
|
.map(pkg => require(pkg)) |
|
.map(p => ({author: p.author, pkgName: p.name})) |
|
.filter(d => d.author) |
|
.map(d => ({author: d.author.name ? d.author.name : d.author, pkgName: d.pkgName})) |
|
; |
|
|
|
const total = pkgAndAuthor.length; |
|
const groupedByAuthor = groupBy(pkgAndAuthor, 'author'); |
|
const howMany = mapObject(groupedByAuthor, modules => modules.length); |
|
|
|
const table = []; |
|
Object.keys(howMany).forEach(author => { |
|
const count = howMany[author]; |
|
table.push({author, count}) |
|
}); |
|
|
|
table |
|
.sort((d1, d2) => d2.count - d1.count); |
|
|
|
const percents = table.map(r => { |
|
const percent = ((r.count / total) * 100).toFixed(2); |
|
return { |
|
author: r.author, |
|
modules: `${r.count}/${total} (${percent}%)` |
|
} |
|
}); |
|
console.log(tablify(percents)); |
|
|
|
|
|
function mapObject(obj, fn) { |
|
return Object.keys(obj).reduce( |
|
(res, key) => { |
|
res[key] = fn(obj[key]); |
|
return res; |
|
}, |
|
{} |
|
) |
|
} |
|
|
|
function groupBy(collection, attribute) { |
|
return collection.reduce( |
|
(res, item) => { |
|
const value = item[attribute]; |
|
if (!res[value]) { |
|
res[value] = []; |
|
} |
|
res[value].push(item); |
|
return res; |
|
}, |
|
{} |
|
) |
|
} |