@@ -224,28 +224,93 @@ export class ExperienceSite {
224224 * @returns path of downloaded site zip
225225 */
226226 public async downloadSite ( ) : Promise < string > {
227- const remoteMetadata = await this . getRemoteMetadata ( ) ;
228- if ( ! remoteMetadata ) {
227+ if ( process . env . API_ENABLED !== 'true' ) {
228+ const retVal = await this . downloadSiteV2 ( ) ;
229+ return retVal ;
230+ } else {
231+ const remoteMetadata = await this . getRemoteMetadata ( ) ;
232+ if ( ! remoteMetadata ) {
233+ throw new SfError ( `No published site found for: ${ this . siteDisplayName } ` ) ;
234+ }
235+
236+ // Download the site from static resources
237+ // eslint-disable-next-line no-console
238+ console . log ( '[local-dev] Downloading site...' ) ; // TODO spinner
239+ const resourcePath = this . getSiteZipPath ( remoteMetadata ) ;
240+ const staticresource = await this . org . getConnection ( ) . metadata . read ( 'StaticResource' , remoteMetadata . bundleName ) ;
241+ if ( staticresource ?. content ) {
242+ // Save the static resource
243+ fs . mkdirSync ( this . getSiteDirectory ( ) , { recursive : true } ) ;
244+ const buffer = Buffer . from ( staticresource . content , 'base64' ) ;
245+ fs . writeFileSync ( resourcePath , buffer ) ;
246+
247+ // Save the site's metadata
248+ this . saveMetadata ( remoteMetadata ) ;
249+ } else {
250+ throw new SfError ( `Error occurred downloading your site: ${ this . siteDisplayName } ` ) ;
251+ }
252+
253+ return resourcePath ;
254+ }
255+ }
256+
257+ /**
258+ * Generate a site bundle on demand and download it
259+ *
260+ * @returns path of downloaded site zip
261+ */
262+ public async downloadSiteV2 ( ) : Promise < string > {
263+ const remoteMetadata = await this . org
264+ . getConnection ( )
265+ . query < { Id : string ; Name : string ; LastModifiedDate : string ; MasterLabel : string } > (
266+ `Select Id, Name, LastModifiedDate, MasterLabel, UrlPathPrefix, SiteType, Status from Site WHERE Name like '${ this . siteName } 1'`
267+ ) ;
268+ if ( ! remoteMetadata || remoteMetadata . records . length === 0 ) {
229269 throw new SfError ( `No published site found for: ${ this . siteDisplayName } ` ) ;
230270 }
271+ const theSite = remoteMetadata . records [ 0 ] ;
231272
232- // Download the site from static resources
273+ // Download the site via API
233274 // eslint-disable-next-line no-console
234275 console . log ( '[local-dev] Downloading site...' ) ; // TODO spinner
235- const resourcePath = this . getSiteZipPath ( remoteMetadata ) ;
236- const staticresource = await this . org . getConnection ( ) . metadata . read ( 'StaticResource' , remoteMetadata . bundleName ) ;
237- if ( staticresource ?. content ) {
238- // Save the static resource
276+ const conn = this . org . getConnection ( ) ;
277+ const metadata = {
278+ bundleName : theSite . Name ,
279+ bundleLastModified : theSite . LastModifiedDate ,
280+ } ;
281+ const siteId = theSite . Id ;
282+ const siteIdMinus3 = siteId . substring ( 0 , siteId . length - 3 ) ;
283+ const accessToken = conn . accessToken ;
284+ const instanceUrl = conn . instanceUrl ; // Org URL
285+ if ( ! accessToken ) {
286+ throw new SfError ( `Error occurred downloading your site: ${ this . siteDisplayName } ` ) ;
287+ }
288+ const resourcePath = this . getSiteZipPath ( metadata ) ;
289+ try {
290+ const apiUrl = `${ instanceUrl } /services/data/v63.0/sites/${ siteIdMinus3 } /preview` ;
291+ const response = await axios . get ( apiUrl , {
292+ headers : {
293+ Authorization : `Bearer ${ accessToken } ` ,
294+ } ,
295+ responseType : 'stream' ,
296+ } ) ;
239297 fs . mkdirSync ( this . getSiteDirectory ( ) , { recursive : true } ) ;
240- const buffer = Buffer . from ( staticresource . content , 'base64' ) ;
241- fs . writeFileSync ( resourcePath , buffer ) ;
242298
243- // Save the site's metadata
244- this . saveMetadata ( remoteMetadata ) ;
245- } else {
246- throw new SfError ( `Error occurred downloading your site: ${ this . siteDisplayName } ` ) ;
299+ const fileStream = fs . createWriteStream ( resourcePath ) ;
300+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
301+ response . data . pipe ( fileStream ) ;
302+
303+ await new Promise ( ( resolve , reject ) => {
304+ fileStream . on ( 'finish' , resolve ) ;
305+ fileStream . on ( 'error' , reject ) ;
306+ } ) ;
307+ this . saveMetadata ( metadata ) ;
308+ } catch ( e ) {
309+ // eslint-disable-next-line no-console
310+ console . error ( 'failed to download site' , e ) ;
247311 }
248312
313+ // Save the site's metadata
249314 return resourcePath ;
250315 }
251316
0 commit comments