2024-07-23 16:50:12 +00:00
|
|
|
/*!
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
|
2025-06-26 12:50:03 +00:00
|
|
|
import type fflate from 'fflate';
|
|
|
|
|
|
2024-07-23 16:50:12 +00:00
|
|
|
import {FileAndContent} from '../interfaces';
|
|
|
|
|
|
2025-06-26 12:50:03 +00:00
|
|
|
let zip: typeof fflate.zip;
|
|
|
|
|
let strToU8: typeof fflate.strToU8;
|
2024-07-23 16:50:12 +00:00
|
|
|
|
|
|
|
|
export async function generateZip(files: FileAndContent[]): Promise<Uint8Array> {
|
2025-06-26 12:50:03 +00:00
|
|
|
if (zip === undefined || strToU8 === undefined) {
|
|
|
|
|
const fflate = require('fflate');
|
|
|
|
|
zip = fflate.zip;
|
|
|
|
|
strToU8 = fflate.strToU8;
|
|
|
|
|
}
|
2024-07-23 16:50:12 +00:00
|
|
|
const filesObj: Record<string, Uint8Array> = {};
|
|
|
|
|
files.forEach(({path, content}) => {
|
|
|
|
|
filesObj[path] = typeof content === 'string' ? strToU8(content) : content;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2025-06-26 12:50:03 +00:00
|
|
|
zip(filesObj, (err: any, data: any) => {
|
2024-07-23 16:50:12 +00:00
|
|
|
if (err) {
|
|
|
|
|
reject(err);
|
|
|
|
|
} else {
|
|
|
|
|
resolve(data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|