-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
84 lines (63 loc) · 2.55 KB
/
index.ts
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
const Router = require('express')()
const PORT = process.env.PORT || 3000
interface ResponseProps
{
schemaVersion?: number,
label?: string,
message?: string,
exception?: string
}
interface CodeTabProps
{
[key: string]: any
language: string,
files: number,
lines: number,
blanks: number,
comments: number,
linesOfCode: number,
}
const ReduceItemProps = (responseBuffer: any, whitelistedFiles: Array<string>, targetStatistic: string): Number => {
return responseBuffer.reduce((current: number, proposed: CodeTabProps) => {
/** exclude total count as we want all individual components */
if (proposed.language == "Total")
return current
if (whitelistedFiles) {
return whitelistedFiles.includes(proposed.language) ? current + (proposed?.[targetStatistic] ?? 0) : current;
}
return current + (proposed?.[targetStatistic] ?? 0);
}, 0)
}
const ParseResponse = (request: any, responseBuffer: any): ResponseProps => {
if (responseBuffer?.Error) {
return { schemaVersion: 1, label: "lines", message: responseBuffer?.Error }
}
const whitelistedFiles: Array<string> = request?.query?.languages?.split(",");
const targetStatistic: string = request?.query?.stat ?? "linesOfCode"
return { schemaVersion: 1, label: "lines", message: String(ReduceItemProps(responseBuffer, whitelistedFiles, targetStatistic)) }
}
const ConstructMessageURI = (request: any) => {
const params = request.query
const repository = params?.repo
if (!repository) {
throw Error("missing query paramter 'repo'")
}
const vendor = params?.vendor ?? "github", branch = params?.branch ?? "master", ignoredItems = params?.ignored ?? String()
const baseUrl = new URL('https://api.codetabs.com/v1/loc/');
baseUrl.search = new URLSearchParams({ [vendor]: repository, ignored: ignoredItems, branch: branch }).toString();
return baseUrl.toString()
}
const CalculateLinesOfCode = async (request: any): Promise<ResponseProps> => {
return new Promise((resolve, reject) => {
try {
fetch(ConstructMessageURI(request)).then(text => text.json()).then(json => {
resolve(ParseResponse(request, json))
})
}
catch (exception: any) {
resolve({ exception: exception.toString() })
}
})
}
Router.get('/', async (request: any, response: any) => response.json(await CalculateLinesOfCode(request)))
Router.listen(PORT, () => console.log(`listening on ${PORT}`))