-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
106 lines (90 loc) · 2.56 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { writeFileSync } from 'fs';
import { join, resolve } from 'path';
import { markdownMagic } from 'markdown-magic';
import { markdownTable } from 'markdown-table';
import pkg from './package.json' with { type: 'json' }
import badgeConfig from './badge.json' with { type: 'json' }
const key = pkg.stats?.user;
const __dirname = resolve();
if (!key) {
throw new Error('Please add `user` in the `stats` field of your package.json');
}
const names = [
'@three11/accordion',
'@three11/animate-top-offset',
'@three11/debounce',
'@three11/dom-helpers',
'@three11/extract-query-arg',
'@three11/infinite-scroll',
'@three11/istouch',
'@three11/optisize',
'@three11/scrollspy',
'animateme',
'async-array-prototype',
'attr-i18n',
'create-pwa',
'create-react-app-ts',
'dator',
'gitlab-calendar',
'hover-media-query',
'html-head-component',
'html5-form-validator',
'introscroll',
'itcss',
'itscss',
'lastfm-ts-api',
'localga',
'node-mysql-client',
'npm-maintainer',
'pass-score',
'postcss-watch-folder',
'random-splice',
'react-accordion-ts',
'react-dropper',
'react-round-carousel',
'react-svg-donuts',
'round-carousel-component',
'scriptex-socials',
'scss-goodies',
'simple-calendar-widget',
'svg-symbol-sprite',
'svg64',
'svgo-add-viewbox',
'svgo-viewbox',
'touchsweep',
'typed-usa-states',
'universal-github-client',
'webpack-mpa',
'webpack-mpa-next',
'webpack-mpa-ts'
];
(async () => {
console.log(`Fetching data for user ${key} from NPM. Please wait...`);
const today = new Date();
const endDate = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
const data = [];
for (const name of names) {
const count = await fetch(`https://api.npmjs.org/downloads/range/${pkg.stats.startDate}:${endDate}/${name}`)
.then(r => r.json())
.then(response => response.downloads.map(item => item.downloads).reduce((sum, curr) => sum + curr, 0));
data.push({
name,
count
});
}
const sum = data.reduce((sum, curr) => sum + curr.count, 0);
const sortedStats = data
.sort(({ name: aName }, { name: bName }) => (aName > bName ? 1 : aName < bName ? -1 : 0))
.map(item => {
const { name, count } = item;
return [`[${name}](https://www.npmjs.com/package/${name})`, count];
});
badgeConfig.message = `${sum} Downloads`;
await writeFileSync('./badge.json', JSON.stringify(badgeConfig, null, 2));
await markdownMagic(join(__dirname, 'README.md'), {
matchWord: 'AUTO-GENERATED-CONTENT',
transforms: {
customTransform: () => markdownTable([['Name', 'Downloads'], ...sortedStats, ['**Sum**', `**${sum}**`]])
}
});
})();