generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.ts
60 lines (51 loc) · 1.51 KB
/
main.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
import {debug, endGroup, getInput, setFailed, startGroup} from '@actions/core'
import {context} from '@actions/github'
import tinify from 'tinify'
import Git from './git'
import Images from './images'
async function run(): Promise<void> {
try {
tinify.key = getInput('api_key', {required: true})
const git = new Git({
token: getInput('github_token', {required: true}),
context
})
startGroup('Collecting affected images')
const files = await git.getFiles()
const images = new Images()
for (const file of files) {
images.addFile(file.filename)
}
endGroup()
startGroup('Compressing images')
const compressedImages = []
const resizeWidth = Number(getInput('resize_width')) || undefined
const resizeHeight = Number(getInput('resize_height')) || undefined
for (const image of images) {
const compressed = await image.compress({
resizeWidth,
resizeHeight
})
if (compressed) {
compressedImages.push(image)
}
}
endGroup()
if (compressedImages.length) {
startGroup('Committing changes')
await git.commit({
files: compressedImages,
userName: getInput('commit_user_name'),
userEmail: getInput('commit_user_email'),
message: getInput('commit_message')
})
endGroup()
}
} catch (error) {
setFailed(error instanceof Error ? error.message : String(error))
if (error instanceof Error && error.stack) {
debug(error.stack)
}
}
}
run()