|
| 1 | +import { error } from 'console'; |
| 2 | +import { createWriteStream, promises as fs } from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as glob from 'glob'; |
| 5 | + |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 7 | +const archiver = require('archiver'); |
| 8 | + |
| 9 | +// Adapted from cdk-assets |
| 10 | +export async function zipDirectory(directory: string, outputFile: string): Promise<void> { |
| 11 | + // We write to a temporary file and rename at the last moment. This is so that if we are |
| 12 | + // interrupted during this process, we don't leave a half-finished file in the target location. |
| 13 | + const temporaryOutputFile = `${outputFile}.${randomString()}._tmp`; |
| 14 | + await writeZipFile(directory, temporaryOutputFile); |
| 15 | + await moveIntoPlace(temporaryOutputFile, outputFile); |
| 16 | +} |
| 17 | + |
| 18 | +function writeZipFile(directory: string, outputFile: string): Promise<void> { |
| 19 | + return new Promise(async (ok, fail) => { |
| 20 | + // The below options are needed to support following symlinks when building zip files: |
| 21 | + // - nodir: This will prevent symlinks themselves from being copied into the zip. |
| 22 | + // - follow: This will follow symlinks and copy the files within. |
| 23 | + const globOptions = { |
| 24 | + dot: true, |
| 25 | + nodir: true, |
| 26 | + follow: true, |
| 27 | + cwd: directory, |
| 28 | + }; |
| 29 | + const files = glob.sync('**', globOptions); // The output here is already sorted |
| 30 | + |
| 31 | + const output = createWriteStream(outputFile); |
| 32 | + |
| 33 | + const archive = archiver('zip'); |
| 34 | + archive.on('warning', fail); |
| 35 | + archive.on('error', fail); |
| 36 | + |
| 37 | + // archive has been finalized and the output file descriptor has closed, resolve promise |
| 38 | + // this has to be done before calling `finalize` since the events may fire immediately after. |
| 39 | + // see https://www.npmjs.com/package/archiver |
| 40 | + output.once('close', ok); |
| 41 | + |
| 42 | + archive.pipe(output); |
| 43 | + |
| 44 | + // Append files serially to ensure file order |
| 45 | + for (const file of files) { |
| 46 | + const fullPath = path.resolve(directory, file); |
| 47 | + const [data, stat] = await Promise.all([fs.readFile(fullPath), fs.stat(fullPath)]); |
| 48 | + archive.append(data, { |
| 49 | + name: file, |
| 50 | + mode: stat.mode, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + await archive.finalize(); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Rename the file to the target location, taking into account: |
| 60 | + * |
| 61 | + * - That we may see EPERM on Windows while an Antivirus scanner still has the |
| 62 | + * file open, so retry a couple of times. |
| 63 | + * - This same function may be called in parallel and be interrupted at any point. |
| 64 | + */ |
| 65 | +async function moveIntoPlace(source: string, target: string) { |
| 66 | + let delay = 100; |
| 67 | + let attempts = 5; |
| 68 | + while (true) { |
| 69 | + try { |
| 70 | + // 'rename' is guaranteed to overwrite an existing target, as long as it is a file (not a directory) |
| 71 | + await fs.rename(source, target); |
| 72 | + return; |
| 73 | + } catch (e: any) { |
| 74 | + if (e.code !== 'EPERM' || attempts-- <= 0) { |
| 75 | + throw e; |
| 76 | + } |
| 77 | + error(e.message); |
| 78 | + await sleep(Math.floor(Math.random() * delay)); |
| 79 | + delay *= 2; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function sleep(ms: number) { |
| 85 | + return new Promise(ok => setTimeout(ok, ms)); |
| 86 | +} |
| 87 | + |
| 88 | +function randomString() { |
| 89 | + return Math.random().toString(36).replace(/[^a-z0-9]+/g, ''); |
| 90 | +} |
0 commit comments