|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; |
| 2 | +import fs from 'fs'; |
| 3 | +import os from 'os'; |
| 4 | +import path from 'path'; |
| 5 | +import { open as openZipFile } from 'yauzl'; |
| 6 | +import { packBundle } from '../src/bundle-pack'; |
| 7 | + |
| 8 | +function mkTempDir(prefix: string): string { |
| 9 | + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 10 | +} |
| 11 | + |
| 12 | +async function listZipEntries(zipPath: string): Promise<string[]> { |
| 13 | + const entries: string[] = []; |
| 14 | + await new Promise<void>((resolve, reject) => { |
| 15 | + openZipFile(zipPath, { lazyEntries: true }, (error, zipfile) => { |
| 16 | + if (error || !zipfile) { |
| 17 | + reject(error ?? new Error('Failed to open zip file')); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + zipfile.on('entry', (entry) => { |
| 22 | + entries.push(entry.fileName); |
| 23 | + zipfile.readEntry(); |
| 24 | + }); |
| 25 | + zipfile.once('end', () => resolve()); |
| 26 | + zipfile.once('error', reject); |
| 27 | + |
| 28 | + zipfile.readEntry(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + return entries; |
| 32 | +} |
| 33 | + |
| 34 | +describe('bundle-pack', () => { |
| 35 | + let tempRoot = ''; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + tempRoot = mkTempDir('rn-update-pack-bundle-'); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + if (tempRoot && fs.existsSync(tempRoot)) { |
| 43 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + test('packs bundle and excludes ignored files', async () => { |
| 48 | + const sourceDir = path.join(tempRoot, 'bundle'); |
| 49 | + const nestedDir = path.join(sourceDir, 'assets'); |
| 50 | + fs.mkdirSync(nestedDir, { recursive: true }); |
| 51 | + |
| 52 | + fs.writeFileSync(path.join(sourceDir, 'index.bundlejs'), 'bundle'); |
| 53 | + fs.writeFileSync(path.join(sourceDir, 'index.bundlejs.map'), 'ignored-map'); |
| 54 | + fs.writeFileSync( |
| 55 | + path.join(sourceDir, 'bundle.harmony.js.map'), |
| 56 | + 'ignored-map', |
| 57 | + ); |
| 58 | + fs.writeFileSync(path.join(sourceDir, 'keep.txt'), 'keep'); |
| 59 | + fs.writeFileSync(path.join(sourceDir, 'ignored.txt.map'), 'ignored-map'); |
| 60 | + fs.writeFileSync(path.join(sourceDir, '.DS_Store'), 'ignored'); |
| 61 | + fs.writeFileSync(path.join(nestedDir, 'image.png'), 'image-content'); |
| 62 | + |
| 63 | + const outputZip = path.join(tempRoot, 'dist', 'output.ppk'); |
| 64 | + await packBundle(sourceDir, outputZip); |
| 65 | + |
| 66 | + expect(fs.existsSync(outputZip)).toBe(true); |
| 67 | + |
| 68 | + const entries = await listZipEntries(outputZip); |
| 69 | + expect(entries).toContain('index.bundlejs'); |
| 70 | + expect(entries).toContain('keep.txt'); |
| 71 | + expect(entries).toContain('assets/'); |
| 72 | + expect(entries).toContain('assets/image.png'); |
| 73 | + |
| 74 | + expect(entries).not.toContain('index.bundlejs.map'); |
| 75 | + expect(entries).not.toContain('bundle.harmony.js.map'); |
| 76 | + expect(entries).not.toContain('ignored.txt.map'); |
| 77 | + expect(entries).not.toContain('.DS_Store'); |
| 78 | + }); |
| 79 | +}); |
0 commit comments