11// try to keep this dep-free so we don't have to install deps
2- const { execSync } = require ( 'child_process' )
2+ const { execFileSync } = require ( 'child_process' )
33const https = require ( 'https' )
44
5+ const hostname =
6+ process . env . GITHUB_REF_NAME === 'dev'
7+ ? 'hanihusam-com-staging.fly.dev'
8+ : 'hanihusam.com'
9+
510function fetchJson ( url , { timeoutTime } = { } ) {
611 return new Promise ( ( resolve , reject ) => {
712 const request = https
@@ -12,6 +17,10 @@ function fetchJson(url, { timeoutTime } = {}) {
1217 } )
1318
1419 res . on ( 'end' , ( ) => {
20+ if ( res . statusCode && res . statusCode >= 400 ) {
21+ reject ( new Error ( `Request failed with status ${ res . statusCode } ` ) )
22+ return
23+ }
1524 try {
1625 resolve ( JSON . parse ( data ) )
1726 } catch ( error ) {
@@ -23,9 +32,9 @@ function fetchJson(url, { timeoutTime } = {}) {
2332 reject ( e )
2433 } )
2534 if ( timeoutTime ) {
26- setTimeout ( ( ) => {
35+ request . setTimeout ( timeoutTime , ( ) => {
2736 request . destroy ( new Error ( 'Request timed out' ) )
28- } , timeoutTime )
37+ } )
2938 }
3039 } )
3140}
@@ -34,39 +43,39 @@ const changeTypes = {
3443 M : 'modified' ,
3544 A : 'added' ,
3645 D : 'deleted' ,
37- R : 'moved' ,
3846}
3947
4048async function getChangedFiles ( currentCommitSha , compareCommitSha ) {
41- try {
42- const lineParser = / ^ (?< change > \w ) .* ?\s + (?< filename > .+ $ ) /
43- const gitOutput = execSync (
44- `git diff --name-status ${ currentCommitSha } ${ compareCommitSha } ` ,
45- ) . toString ( )
46- const changedFiles = gitOutput
47- . split ( '\n' )
48- . map ( ( line ) => line . match ( lineParser ) ?. groups )
49- . filter ( Boolean )
50- const changes = [ ]
51- for ( const { change, filename } of changedFiles ) {
52- const changeType = changeTypes [ change ]
53- if ( changeType ) {
54- changes . push ( { changeType : changeTypes [ change ] , filename } )
55- } else {
56- console . error ( `Unknown change type: ${ change } ${ filename } ` )
57- }
49+ const lineParser = / ^ (?< change > \w ) .* ?\s + (?< filename > .+ $ ) /
50+ const gitOutput = execFileSync ( '/usr/bin/git' , [
51+ 'diff' ,
52+ '--name-status' ,
53+ '--no-renames' ,
54+ currentCommitSha ,
55+ compareCommitSha ,
56+ ] ) . toString ( )
57+ const changedFiles = gitOutput
58+ . split ( '\n' )
59+ . map ( ( line ) => line . match ( lineParser ) ?. groups )
60+ . filter ( Boolean )
61+ const changes = [ ]
62+ for ( const { change, filename } of changedFiles ) {
63+ const changeType = changeTypes [ change ]
64+ if ( changeType ) {
65+ changes . push ( { changeType, filename } )
66+ } else {
67+ throw new Error ( `Unknown change type: ${ change } ${ filename } ` )
5868 }
59- return changes
60- } catch ( error ) {
61- console . error ( `Something went wrong trying to get changed files.` , error )
62- return null
6369 }
70+ return changes
6471}
6572
66- const hostname =
67- process . env . GITHUB_REF_NAME === 'dev'
68- ? 'hanihusam-com.fly.dev'
69- : 'hanihusam.com'
73+ function getTrackedContentFiles ( ) {
74+ return execFileSync ( '/usr/bin/git' , [ 'ls-files' , 'contents' ] )
75+ . toString ( )
76+ . split ( '\n' )
77+ . filter ( Boolean )
78+ }
7079
7180// try to keep this dep-free so we don't have to install deps
7281async function postRefreshCache ( {
@@ -102,6 +111,10 @@ async function postRefreshCache({
102111 } )
103112
104113 res . on ( 'end' , ( ) => {
114+ if ( res . statusCode && res . statusCode >= 400 ) {
115+ reject ( new Error ( `Refresh failed with status ${ res . statusCode } ` ) )
116+ return
117+ }
105118 try {
106119 resolve ( JSON . parse ( data ) )
107120 } catch {
@@ -110,6 +123,9 @@ async function postRefreshCache({
110123 } )
111124 } )
112125 . on ( 'error' , reject )
126+ req . setTimeout ( 30_000 , ( ) => {
127+ req . destroy ( new Error ( 'Refresh request timed out' ) )
128+ } )
113129 req . write ( postDataString )
114130 req . end ( )
115131 } catch ( error ) {
@@ -119,4 +135,10 @@ async function postRefreshCache({
119135 } )
120136}
121137
122- module . exports = { fetchJson, getChangedFiles, postRefreshCache }
138+ module . exports = {
139+ fetchJson,
140+ getChangedFiles,
141+ getTrackedContentFiles,
142+ hostname,
143+ postRefreshCache,
144+ }
0 commit comments