-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·77 lines (70 loc) · 1.98 KB
/
index.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
#!/usr/bin/env node
const request = require("request-promise-native");
const utils = require("./utils");
const { pkg } = require("read-pkg-up").sync();
const githubToken = process.env.GITHUB_TOKEN; // eslint-disable-line no-process-env
function postStats(stats) {
const requestOptions = Object.assign({
uri: "https://bascule.now.sh/api/stats",
method: "POST",
simple: false,
resolveWithFullResponse: true,
json: true,
headers: {
"exq-github-token": githubToken
},
body: stats
});
return request(requestOptions).then(r => console.log(r.body));
}
/**
* Request body has the following format:
* {
* "repo": "reponame",
* "sha": "commitid"
* "bundles": {
* "bundle_1": {
* "details": {
* "module1": ..,
* "module2": ..,
* "module3": ..
* },
* "limit": "3kb",
* "size": "2kb",
* "success": true
* }
* }
* }
*/
let prNumber;
if (process.argv.length > 2) {
([,, prNumber] = process.argv);
}
Promise.all([
utils.getRepoName(),
utils.getRepoHeadHash(),
utils.readFiles(pkg.bascule)
]).then(([repo, sha, files]) => {
Promise.all(files.map(entry => (
utils.generateBundleStats(entry.path).then(stats => {
delete stats.files["<unmapped>"];
return Object.assign(entry, { stats });
}).catch(err => {
console.log(err);
})
))).then(entries => (
entries.filter(Boolean).map(entry => ({
file: entry.path,
details: entry.stats,
limit: entry.limit !== Infinity ? entry.limit : null,
size: entry.size,
success: entry.size < entry.limit
}))
)).then(entries => ({
repo,
sha,
bundles: entries
})).then(stats => {
return postStats(Object.assign(stats, { pr: prNumber }));
});
});