forked from chronotruck/webpack-stats-diff-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (64 loc) · 1.81 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
const path = require('path')
const fs = require('fs')
const github = require('@actions/github')
const core = require('@actions/core')
const { getStatsDiff } = require('webpack-stats-diff')
const fileSize = require('filesize')
const markdownTable = require('markdown-table')
const doesPathExists = path => {
if (!fs.existsSync(path)) {
throw new Error(`${path} does not exist!`)
}
}
async function run() {
try {
const statsPaths = {
base: core.getInput('base_stats_path'),
head: core.getInput('head_stats_path')
}
const paths = {
base: path.resolve(process.cwd(), statsPaths.base),
head: path.resolve(process.cwd(), statsPaths.head)
}
doesPathExists(paths.base)
doesPathExists(paths.head)
const assets = {
base: require(paths.base).assets,
head: require(paths.head).assets
}
const diff = getStatsDiff(assets.base, assets.head, {})
const summaryTable = markdownTable([
[
'Old size',
'New size',
'Diff'
],
[
fileSize(diff.total.oldSize),
fileSize(diff.total.newSize),
`${fileSize(diff.total.diff)} (${diff.total.diffPercentage.toFixed(2)}%)`
]
])
/**
* Publish a comment in the PR with the diff result.
*/
const octokit = github.getOctokit(core.getInput('token'))
const pullRequestId = github.context.issue.number
if (!pullRequestId) {
throw new Error('Cannot find the PR id.')
}
const commentTitle = core.getInput('comment_title') || 'Bundle difference'
await octokit.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: pullRequestId,
body: `## ${commentTitle}
${summaryTable}
`
})
}
catch (error) {
core.setFailed(error.message)
}
}
run()