angular/adev/shared-docs/utils/zip.utils.ts
Joey Perrott b84859073b build: migrate to use web test runner rules (#62292)
Migrate karma tests throughout the repo to use the new web test runner based rule instead

PR Close #62292
2025-06-26 17:19:10 +00:00

36 lines
944 B
TypeScript

/*!
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type fflate from 'fflate';
import {FileAndContent} from '../interfaces';
let zip: typeof fflate.zip;
let strToU8: typeof fflate.strToU8;
export async function generateZip(files: FileAndContent[]): Promise<Uint8Array> {
if (zip === undefined || strToU8 === undefined) {
const fflate = require('fflate');
zip = fflate.zip;
strToU8 = fflate.strToU8;
}
const filesObj: Record<string, Uint8Array> = {};
files.forEach(({path, content}) => {
filesObj[path] = typeof content === 'string' ? strToU8(content) : content;
});
return new Promise((resolve, reject) => {
zip(filesObj, (err: any, data: any) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}