import crypto from 'crypto'; import { promises as fs } from 'fs'; import { tmpdir } from 'os'; import { join } from 'path'; import * as tar from 'tar'; export const createAppTarball = async ( files: Record, ): Promise => { const tempId = crypto.randomUUID(); const sourceDir = join(tmpdir(), `test-app-tarball-src-${tempId}`); const tarballPath = join(tmpdir(), `test-app-tarball-${tempId}.tar.gz`); await fs.mkdir(sourceDir, { recursive: true }); for (const [name, content] of Object.entries(files)) { const filePath = join(sourceDir, name); const dir = filePath.substring(0, filePath.lastIndexOf('/')); if (dir !== sourceDir) { await fs.mkdir(dir, { recursive: true }); } await fs.writeFile(filePath, content); } await tar.create( { file: tarballPath, gzip: true, cwd: sourceDir, }, Object.keys(files), ); const buffer = await fs.readFile(tarballPath); await fs.rm(sourceDir, { recursive: true, force: true }); await fs.rm(tarballPath, { force: true }); return buffer; };