import AdmZip from 'adm-zip'; import { CollectionModuleCodeFileWithContent } from '../../domain/collection-modules/collection-module-definition'; import { HttpMethod, RootAPIHelper } from '../root-api'; import { NetworkPathWithUrl } from '../../domain/collection-modules/collection-module-definition'; import { S3Operation } from './code-file-helpers'; export const downloadAndExtractSourcePackage = async (params: { collectionModuleKey: string; host: string; apiKey: string; liveVersion: boolean; }): Promise => { const { collectionModuleKey, host, apiKey, liveVersion } = params; const rootApiHelper = new RootAPIHelper({ apiKey, host, throwResponseErrors: true }); // Get signed URL for source.zip const body = { paths: [{ path_segments: ['source.zip'] }], operation: S3Operation.GetObject, }; const response: NetworkPathWithUrl[] = await rootApiHelper.send({ path: `/cli/collection-modules/${collectionModuleKey}/code-files-signed-urls`, method: HttpMethod.Post, body, searchParams: { live_version: liveVersion.toString() }, }); const signedUrl = response[0].signed_url; // Download source.zip const s3Response = await fetch(signedUrl); if (!s3Response.ok) { throw new Error(`Failed to download source package: ${s3Response.statusText}`); } const zipBuffer = Buffer.from(await s3Response.arrayBuffer()); // Extract files from zip const zip = new AdmZip(zipBuffer); const zipEntries = zip.getEntries(); return zipEntries.map((entry) => ({ pathSegments: entry.entryName.split('/'), content: entry.getData().toString('utf8'), })); };