Skip to content

Commit 8e42da4

Browse files
committed
πŸ› μ›Œν¬ν”Œλ‘œμš° μˆ˜μ •
1 parent ac3aae5 commit 8e42da4

3 files changed

Lines changed: 56 additions & 16 deletions

File tree

β€Ž.github/workflows/process-images.yamlβ€Ž

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,23 @@ jobs:
2424
restore-keys: |
2525
${{ runner.os }}-primes-
2626
- name: Install dependencies
27-
if: steps.cache.outputs.cache-hit != 'true'
2827
run: npm ci
28+
- name: Create tsconfig.build.json
29+
run: |
30+
echo '{
31+
"extends": "./tsconfig.json",
32+
"compilerOptions": {
33+
"target": "ES2021",
34+
"module": "ES2020",
35+
"outDir": "./dist",
36+
"noEmit": false
37+
},
38+
"include": ["scripts/**/*"],
39+
"exclude": ["src/**/*", "dist"]
40+
}' > tsconfig.build.json
2941
- name: Build TypeScript
3042
run: |
31-
npx tsc ./scripts/action-process-images.ts --outDir ./dist --esModuleInterop --target ES2021 --jsx react --skipLibCheck
43+
npx tsc ./scripts/action-process-images.ts ./scripts/github-api.ts ./scripts/sharp-api.ts --outDir ./dist --esModuleInterop --target ES2021 --module ES2020 --moduleResolution bundler --allowJs --isolatedModules --skipLibCheck --jsx react
3244
- name: ✧ Process images and Report results
3345
run: node ./dist/action-process-images.js
3446
env:

β€Žscripts/action-process-images.tsβ€Ž

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createComment, createCommit, imageToTreeBlob } from './github-api'
2-
import { sharpImages } from './sharp-api'
1+
import { createComment, createCommit, createTreeBlobs } from './github-api.js'
2+
import { sharpImages } from './sharp-api.js'
33

44
const formatByte = (byte: number) => {
55
let num = +byte
@@ -25,12 +25,13 @@ const formatImages = (num: number) => {
2525
}
2626

2727
console.log('\n::✧:: Generating Blobs…')
28-
const imageBlobs = await Promise.all(sharpedImageList.map(imageToTreeBlob))
28+
const imageBlobs = await Promise.all(sharpedImageList.map(createTreeBlobs))
29+
const flattenedBlobs = imageBlobs.flat()
2930

3031
console.log('\n::✧:: Committing files…')
3132
const commit = await createCommit({
32-
message: '::✧:: process images',
33-
treeBlobs: imageBlobs,
33+
message: 'πŸ–ΌοΈ 이미지 μ΅œμ ν™”',
34+
treeBlobs: flattenedBlobs,
3435
})
3536

3637
console.log('\n::✧:: Writing comment on PR…')

β€Žscripts/github-api.tsβ€Ž

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'node:fs/promises'
22

33
import { Octokit, type RestEndpointMethodTypes } from '@octokit/rest'
44

5-
import type { ProcessedResult } from './sharp-api'
5+
import type { ProcessedResult } from './sharp-api.js'
66

77
export type GitTreeBlob = RestEndpointMethodTypes['git']['createTree']['parameters']['tree'][number]
88

@@ -12,6 +12,8 @@ export const [owner, repo] = GITHUB_REPOSITORY.split('/')
1212

1313
export const GITHUB_PULL_REQUEST = JSON.parse(process.env['GITHUB_PULL_REQUEST']!)
1414

15+
type PullRequestFile = RestEndpointMethodTypes['pulls']['listFiles']['response']['data'][number]
16+
1517
if (!GITHUB_TOKEN) {
1618
console.log('::error:: There is no GITHUB_TOKEN environment variable')
1719
process.exit(1)
@@ -36,23 +38,48 @@ const imageToBase64 = async (path: string) => {
3638
return base64
3739
}
3840

39-
export const imageToTreeBlob = async (image: ProcessedResult) => {
40-
const encodedImage = await imageToBase64(image.path)
41+
export const createTreeBlobs = async (image: ProcessedResult) => {
42+
const blobs: GitTreeBlob[] = []
43+
44+
const filePath = image.convertedToAvif ? image.avifPath! : image.path
45+
const encodedImage = await imageToBase64(filePath)
4146

42-
const blob = await api.rest.git.createBlob({
47+
const imageBlob = await api.rest.git.createBlob({
4348
owner,
4449
repo,
4550
content: encodedImage,
4651
encoding: 'base64',
4752
})
48-
console.log('✧', image.name, blob.data.url)
4953

50-
return {
51-
path: image.path,
54+
blobs.push({
55+
path: filePath,
5256
mode: '100644',
5357
type: 'blob',
54-
sha: blob.data.sha,
55-
} satisfies GitTreeBlob
58+
sha: imageBlob.data.sha,
59+
})
60+
61+
const modifiedMdxFiles = (GITHUB_PULL_REQUEST.changed_files as PullRequestFile[])
62+
.map((file) => file.filename)
63+
.filter((filename) => filename.endsWith('.mdx'))
64+
65+
for (const mdxPath of modifiedMdxFiles) {
66+
const content = await fs.readFile(mdxPath, 'utf-8')
67+
const mdxBlob = await api.rest.git.createBlob({
68+
owner,
69+
repo,
70+
content: content,
71+
encoding: 'utf-8',
72+
})
73+
74+
blobs.push({
75+
path: mdxPath,
76+
mode: '100644',
77+
type: 'blob',
78+
sha: mdxBlob.data.sha,
79+
})
80+
}
81+
82+
return blobs
5683
}
5784

5885
export const createCommit = async ({

0 commit comments

Comments
Β (0)