forked from maddhruv/github-readme-npm-downloads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.mjs
51 lines (40 loc) · 1.14 KB
/
collect.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import path from 'path';
import fs from 'fs';
import _ from 'lodash';
import table from 'markdown-table';
import markdownMagic from 'markdown-magic';
import npmtotal from 'npmtotal'
import json from './utils/json.mjs';
const { "npm-stats": key } = json('./package.json');
const badgeStats = json('./stats.json');
if (!key) {
throw new Error("Please add `npm-stats` to your package.json");
}
(async () => {
const stats = await npmtotal(key, {
startDate: "2017-01-01"
});
const sortedStats = stats.stats.map(pkg => {
const [name, count] = pkg;
return [`[${name}](https://www.npmjs.com/package/${name})`, count];
});
badgeStats.message = `${stats.sum} Downloads`;
await fs.writeFileSync("./stats.json", JSON.stringify(badgeStats, null, 2));
generate(sortedStats, stats.sum);
})();
function generate(data, sum) {
const config = {
transforms: {
PACKAGES() {
return table([
["Name", "Downloads"],
...data,
["**Sum**", `**${sum}**`]
]);
}
}
};
markdownMagic(path.join("README.md"), config, () => {
console.log(`Updated total downloads - ${sum}`);
});
}