-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
packagecontrol.service.js
117 lines (105 loc) · 3.15 KB
/
packagecontrol.service.js
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
107
108
109
110
111
112
113
114
115
116
117
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { downloadCount } from '../color-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
const keywords = ['sublime', 'sublimetext', 'packagecontrol']
const schema = Joi.object({
installs: Joi.object({
total: nonNegativeInteger,
daily: Joi.object({
data: Joi.array()
.items(
Joi.object({
totals: Joi.array().items(nonNegativeInteger).required(),
}).required()
)
.required(),
}).required(),
}).required(),
})
function DownloadsForInterval(interval) {
const { base, messageSuffix, transform, name } = {
day: {
base: 'packagecontrol/dd',
messageSuffix: '/day',
transform: resp => {
const platforms = resp.installs.daily.data
let downloads = 0
platforms.forEach(platform => {
// use the downloads from yesterday
downloads += platform.totals[1]
})
return downloads
},
name: 'PackageControlDownloadsDay',
},
week: {
base: 'packagecontrol/dw',
messageSuffix: '/week',
transform: resp => {
const platforms = resp.installs.daily.data
let downloads = 0
platforms.forEach(platform => {
// total for the first 7 days
for (let i = 0; i < 7; i++) {
downloads += platform.totals[i]
}
})
return downloads
},
name: 'PackageControlDownloadsWeek',
},
month: {
base: 'packagecontrol/dm',
messageSuffix: '/month',
transform: resp => {
const platforms = resp.installs.daily.data
let downloads = 0
platforms.forEach(platform => {
// total for the first 30 days
for (let i = 0; i < 30; i++) {
downloads += platform.totals[i]
}
})
return downloads
},
name: 'PackageControlDownloadsMonth',
},
total: {
base: 'packagecontrol/dt',
messageSuffix: '',
transform: resp => resp.installs.total,
name: 'PackageControlDownloadsTotal',
},
}[interval]
return class PackageControlDownloads extends BaseJsonService {
static name = name
static category = 'downloads'
static route = { base, pattern: ':packageName' }
static examples = [
{
title: 'Package Control',
namedParams: { packageName: 'GitGutter' },
staticPreview: this.render({ downloads: 12000 }),
keywords,
},
]
static defaultBadgeData = { label: 'downloads' }
static render({ downloads }) {
return {
message: `${metric(downloads)}${messageSuffix}`,
color: downloadCount(downloads),
}
}
async fetch({ packageName }) {
const url = `https://packagecontrol.io/packages/${packageName}.json`
return this._requestJson({ schema, url })
}
async handle({ packageName }) {
const data = await this.fetch({ packageName })
return this.constructor.render({ downloads: transform(data) })
}
}
}
export default ['day', 'week', 'month', 'total'].map(DownloadsForInterval)