@@ -2,49 +2,62 @@ import config from '@adobe/aio-lib-core-config'
22import Logger from '@adobe/aio-lib-core-logging'
33const aioLogger = Logger ( 'commerce:preview.js' )
44
5+ const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
6+
57/**
6- *
7- * @param files
8+ * Given an array of filenames, uses the hlx admin api to preview the content.
9+ * This function accounts for the rate limiting imposed on the API by previewing
10+ * 10 files every second.
11+ * @param {Array } files the array of files to be previewed
12+ * @returns {Promise<Array> } that resolves to the array of results
813 */
914export async function previewContent ( files ) {
1015 const { org, repo } = config . get ( 'commerce.github' )
1116 if ( ! org || ! repo ) throw new Error ( 'Missing Github Org and Repo' )
1217 console . log ( '⏳ Previewing files, this may take some time...' )
18+
1319 const results = [ ]
14- const rateLimit = 10 // 10 requests per second
15- const interval = 1000 / rateLimit // interval in milliseconds
20+ const batchSize = 10
21+ let i = 0
1622
17- const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
23+ while ( i < files . length ) {
24+ const chunk = files . slice ( i , Math . min ( i + batchSize , files . length ) )
25+ const chunkPromises = chunk . map ( file => previewFile ( file ) )
26+ await Promise . all ( chunkPromises )
27+ await delay ( 1000 )
28+ i += batchSize
29+ }
1830
19- for ( let i = 0 ; i < files . length ; i ++ ) {
20- aioLogger . debug ( 'ORIGIN:' , files [ i ] )
21- let { pathname } = new URL ( files [ i ] )
31+ /**
32+ * TODO refactor this and publish
33+ * @param file
34+ */
35+ async function previewFile ( file ) {
36+ let { pathname } = new URL ( file )
2237 if ( pathname . endsWith ( '/' ) ) {
2338 pathname = pathname . replace ( / \/ $ / , '/index' )
2439 }
25- aioLogger . debug ( 'DESTINATION:' , `https://da.live/#/${ org } /${ repo } ${ pathname . replace ( / \. [ ^ . ] + $ / , '' ) } ` )
2640 const url = new URL ( `https://admin.hlx.page/preview/${ org } /${ repo } /main${ pathname } ` )
27- aioLogger . debug ( `Previewing: ${ url } ` )
41+
42+ aioLogger . debug ( `Previewing {
43+ "origin": "${ file } ",
44+ "destination": "https://da.live/edit#/${ org } /${ repo } ${ pathname . replace ( / \. [ ^ . ] + $ / , '' ) } "
45+ }` )
2846
2947 let result
30- // TODO: refactor this retry/delay logic to be better!
3148 try {
3249 const res = await fetch ( url , { method : 'POST' } )
3350 if ( res . status !== 200 ) {
34- result = { source : files [ i ] , status : 'failed' , message : `Failed to preview https://da.live/#/${ org } /${ repo } ${ pathname . replace ( / \. [ ^ . ] + $ / , '' ) } ` }
51+ result = { source : file , status : 'failed' , message : `Failed to preview https://da.live/edit #/${ org } /${ repo } ${ pathname . replace ( / \. [ ^ . ] + $ / , '' ) } ` }
3552 aioLogger . debug ( res )
3653 } else {
37- result = { source : files [ i ] , status : 'success' }
54+ result = { source : file , status : 'success' }
3855 }
3956 } catch ( error ) {
40- result = { source : files [ i ] , status : 'error' , message : error . message }
57+ result = { source : file , status : 'error' , message : error . message }
4158 aioLogger . debug ( error )
4259 }
4360 results . push ( result )
44-
45- if ( i < files . length - 1 ) {
46- await delay ( interval ) // wait for the interval before making the next request
47- }
4861 }
4962
5063 const successes = results . filter ( ( { status } ) => status === 'success' )
@@ -66,42 +79,54 @@ const filesToPublish = [
6679 '/footer' ,
6780 '/mini-cart'
6881]
82+
6983/**
7084 * For DA Live Preview, we must publish some files. This must be done AFTER preview.
7185 */
7286export async function publishContent ( ) {
7387 const { org, repo } = config . get ( 'commerce.github' )
7488 if ( ! org || ! repo ) throw new Error ( 'Missing Github Org and Repo' )
7589 console . log ( '⏳ Publishing some necessary files...' )
90+
7691 const results = [ ]
77- const rateLimit = 10 // 10 requests per second
78- const interval = 1000 / rateLimit // interval in milliseconds
92+ const batchSize = 10
93+ let i = 0
7994
80- const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
95+ while ( i < filesToPublish . length ) {
96+ const chunk = filesToPublish . slice ( i , Math . min ( i + batchSize , filesToPublish . length ) )
97+ const chunkPromises = chunk . map ( file => publishFile ( file ) )
98+ await Promise . all ( chunkPromises )
99+ await delay ( 1000 )
100+ i += batchSize
101+ }
81102
82- for ( let i = 0 ; i < filesToPublish . length ; i ++ ) {
83- const pathname = filesToPublish [ i ]
103+ /**
104+ *
105+ * @param file
106+ * @param pathname
107+ */
108+ async function publishFile ( pathname ) {
84109 const url = new URL ( `https://admin.hlx.page/live/${ org } /${ repo } /main${ pathname } ` )
85- aioLogger . debug ( `Publishing at ${ url } ` )
110+
111+ aioLogger . debug ( `Publishing {
112+ "origin": "${ pathname } ",
113+ "destination": "https://da.live/edit#/${ org } /${ repo } ${ pathname . replace ( / \. [ ^ . ] + $ / , '' ) } "
114+ }` )
86115
87116 let result
88117 try {
89118 const res = await fetch ( url , { method : 'POST' } )
90119 if ( res . status !== 200 ) {
91- result = { source : filesToPublish [ i ] , status : 'failed' , message : `Failed to publish ${ filesToPublish [ i ] } ` }
120+ result = { source : pathname , status : 'failed' , message : `Failed to publish https://da.live/edit#/ ${ org } / ${ repo } ${ pathname . replace ( / \. [ ^ . ] + $ / , '' ) } ` }
92121 aioLogger . debug ( res )
93122 } else {
94- result = { source : filesToPublish [ i ] , status : 'success' }
123+ result = { source : pathname , status : 'success' }
95124 }
96125 } catch ( error ) {
97- result = { source : filesToPublish [ i ] , status : 'error' , message : error . message }
126+ result = { source : pathname , status : 'error' , message : error . message }
98127 aioLogger . debug ( error )
99128 }
100129 results . push ( result )
101-
102- if ( i < filesToPublish . length - 1 ) {
103- await delay ( interval ) // wait for the interval before making the next request
104- }
105130 }
106131
107132 const successes = results . filter ( ( { status } ) => status === 'success' )
0 commit comments