Skip to content

Commit a5601a7

Browse files
authored
feat: consume local dev API for on-demand site bundle generation (#264)
1 parent c5c87f2 commit a5601a7

File tree

1 file changed

+78
-13
lines changed

1 file changed

+78
-13
lines changed

src/shared/experience/expSite.ts

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,28 +226,93 @@ export class ExperienceSite {
226226
* @returns path of downloaded site zip
227227
*/
228228
public async downloadSite(): Promise<string> {
229-
const remoteMetadata = await this.getRemoteMetadata();
230-
if (!remoteMetadata) {
229+
if (process.env.STATIC_MODE !== 'true') {
230+
const retVal = await this.downloadSiteV2();
231+
return retVal;
232+
} else {
233+
const remoteMetadata = await this.getRemoteMetadata();
234+
if (!remoteMetadata) {
235+
throw new SfError(`No published site found for: ${this.siteDisplayName}`);
236+
}
237+
238+
// Download the site from static resources
239+
// eslint-disable-next-line no-console
240+
console.log('[local-dev] Downloading site...'); // TODO spinner
241+
const resourcePath = this.getSiteZipPath(remoteMetadata);
242+
const staticresource = await this.org.getConnection().metadata.read('StaticResource', remoteMetadata.bundleName);
243+
if (staticresource?.content) {
244+
// Save the static resource
245+
fs.mkdirSync(this.getSiteDirectory(), { recursive: true });
246+
const buffer = Buffer.from(staticresource.content, 'base64');
247+
fs.writeFileSync(resourcePath, buffer);
248+
249+
// Save the site's metadata
250+
this.saveMetadata(remoteMetadata);
251+
} else {
252+
throw new SfError(`Error occurred downloading your site: ${this.siteDisplayName}`);
253+
}
254+
255+
return resourcePath;
256+
}
257+
}
258+
259+
/**
260+
* Generate a site bundle on demand and download it
261+
*
262+
* @returns path of downloaded site zip
263+
*/
264+
public async downloadSiteV2(): Promise<string> {
265+
const remoteMetadata = await this.org
266+
.getConnection()
267+
.query<{ Id: string; Name: string; LastModifiedDate: string; MasterLabel: string }>(
268+
`Select Id, Name, LastModifiedDate, MasterLabel, UrlPathPrefix, SiteType, Status from Site WHERE Name like '${this.siteName}1'`
269+
);
270+
if (!remoteMetadata || remoteMetadata.records.length === 0) {
231271
throw new SfError(`No published site found for: ${this.siteDisplayName}`);
232272
}
273+
const theSite = remoteMetadata.records[0];
233274

234-
// Download the site from static resources
275+
// Download the site via API
235276
// eslint-disable-next-line no-console
236277
console.log('[local-dev] Downloading site...'); // TODO spinner
237-
const resourcePath = this.getSiteZipPath(remoteMetadata);
238-
const staticresource = await this.org.getConnection().metadata.read('StaticResource', remoteMetadata.bundleName);
239-
if (staticresource?.content) {
240-
// Save the static resource
278+
const conn = this.org.getConnection();
279+
const metadata = {
280+
bundleName: theSite.Name,
281+
bundleLastModified: theSite.LastModifiedDate,
282+
};
283+
const siteId = theSite.Id;
284+
const siteIdMinus3 = siteId.substring(0, siteId.length - 3);
285+
const accessToken = conn.accessToken;
286+
const instanceUrl = conn.instanceUrl; // Org URL
287+
if (!accessToken) {
288+
throw new SfError(`Error occurred downloading your site: ${this.siteDisplayName}`);
289+
}
290+
const resourcePath = this.getSiteZipPath(metadata);
291+
try {
292+
const apiUrl = `${instanceUrl}/services/data/v63.0/sites/${siteIdMinus3}/preview`;
293+
const response = await axios.get(apiUrl, {
294+
headers: {
295+
Authorization: `Bearer ${accessToken}`,
296+
},
297+
responseType: 'stream',
298+
});
241299
fs.mkdirSync(this.getSiteDirectory(), { recursive: true });
242-
const buffer = Buffer.from(staticresource.content, 'base64');
243-
fs.writeFileSync(resourcePath, buffer);
244300

245-
// Save the site's metadata
246-
this.saveMetadata(remoteMetadata);
247-
} else {
248-
throw new SfError(`Error occurred downloading your site: ${this.siteDisplayName}`);
301+
const fileStream = fs.createWriteStream(resourcePath);
302+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
303+
response.data.pipe(fileStream);
304+
305+
await new Promise((resolve, reject) => {
306+
fileStream.on('finish', resolve);
307+
fileStream.on('error', reject);
308+
});
309+
this.saveMetadata(metadata);
310+
} catch (e) {
311+
// eslint-disable-next-line no-console
312+
console.error('failed to download site', e);
249313
}
250314

315+
// Save the site's metadata
251316
return resourcePath;
252317
}
253318

0 commit comments

Comments
 (0)