@@ -2,7 +2,7 @@ import * as fs from 'node:fs/promises'
22
33import { Octokit , type RestEndpointMethodTypes } from '@octokit/rest'
44
5- import type { ProcessedResult } from './sharp-api'
5+ import type { ProcessedResult } from './sharp-api.js '
66
77export type GitTreeBlob = RestEndpointMethodTypes [ 'git' ] [ 'createTree' ] [ 'parameters' ] [ 'tree' ] [ number ]
88
@@ -12,6 +12,8 @@ export const [owner, repo] = GITHUB_REPOSITORY.split('/')
1212
1313export const GITHUB_PULL_REQUEST = JSON . parse ( process . env [ 'GITHUB_PULL_REQUEST' ] ! )
1414
15+ type PullRequestFile = RestEndpointMethodTypes [ 'pulls' ] [ 'listFiles' ] [ 'response' ] [ 'data' ] [ number ]
16+
1517if ( ! 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
5885export const createCommit = async ( {
0 commit comments