|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import archiver, { Archiver, ArchiverOptions } from 'archiver'; |
| 4 | +export type { ArchiverOptions } from 'archiver'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Supported archive formats. |
| 8 | + */ |
| 9 | +export type ArchiveFormat = 'zip' | 'tar'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Configuration options. |
| 13 | + */ |
| 14 | +export interface CreateArchiveOptions { |
| 15 | + /** |
| 16 | + * Archive format to use {@link ArchiveFormat}. |
| 17 | + */ |
| 18 | + format: ArchiveFormat; |
| 19 | + |
| 20 | + /** |
| 21 | + * Path to the source directory that should be archived. |
| 22 | + */ |
| 23 | + source: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * Destination file path where the archive will be written |
| 27 | + * @example |
| 28 | + * - For `zip` format: `dist.zip` |
| 29 | + * - For `tar` format: `dist.tar` |
| 30 | + */ |
| 31 | + destination: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * Additional options passed directly to the archiver library. See {@link ArchiverOptions}. |
| 35 | + */ |
| 36 | + options?: ArchiverOptions; |
| 37 | + |
| 38 | + /** |
| 39 | + * Optional flag to enable internal logging. Useful for CLI mode. |
| 40 | + */ |
| 41 | + log?: boolean; |
| 42 | + |
| 43 | + /** |
| 44 | + * Called after archiving is complete — receives total size in bytes. |
| 45 | + */ |
| 46 | + onSuccess?: (bytes: number) => void; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Creates a {@link ArchiveFormat } archive from a specified directory. |
| 51 | + * |
| 52 | + * This function uses the `archiver` library to package a directory into an archive file. |
| 53 | + * It supports optional compression for `zip` and returns a Promise that resolves |
| 54 | + * when the archive is successfully created. |
| 55 | + * |
| 56 | + * @example |
| 57 | + * |
| 58 | + * Function usage: |
| 59 | + * |
| 60 | + * ```ts |
| 61 | + * await createArchive({ |
| 62 | + * format: "zip", |
| 63 | + * source: "dist/", |
| 64 | + * destination: "dist.zip", |
| 65 | + * }); |
| 66 | + * ``` |
| 67 | + * |
| 68 | + * CLI usage: |
| 69 | + * ```sh |
| 70 | + * npx js-utils-kit createArchive -f zip -s dist -d dist.zip |
| 71 | + * ``` |
| 72 | + * |
| 73 | + * @param options - Archive creation options |
| 74 | + * @returns A Promise that resolves when the archive is created |
| 75 | + * @throws If an error occurs during the archiving process |
| 76 | + */ |
| 77 | +export function createArchive({ |
| 78 | + format, |
| 79 | + source, |
| 80 | + destination, |
| 81 | + options = {}, |
| 82 | + log = true, |
| 83 | + onSuccess, |
| 84 | +}: CreateArchiveOptions): Promise<void> { |
| 85 | + const resolvedSource = path.resolve(source); |
| 86 | + |
| 87 | + if ( |
| 88 | + !fs.existsSync(resolvedSource) || |
| 89 | + !fs.statSync(resolvedSource).isDirectory() |
| 90 | + ) { |
| 91 | + throw new Error( |
| 92 | + `Source directory "${source}" does not exist or is not a directory.`, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + const output = fs.createWriteStream(destination); |
| 97 | + |
| 98 | + if (format === 'zip') { |
| 99 | + options = { |
| 100 | + ...options, |
| 101 | + /** |
| 102 | + * Maximum compression level for zip is 9. |
| 103 | + */ |
| 104 | + zlib: { level: 9 }, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + const archive: Archiver = archiver(format, options); |
| 109 | + |
| 110 | + return new Promise((resolve, reject) => { |
| 111 | + output.on('close', () => { |
| 112 | + const size = archive.pointer(); |
| 113 | + |
| 114 | + if (log) { |
| 115 | + console.log(`${destination} created: ${size} total bytes`); |
| 116 | + } |
| 117 | + if (onSuccess) { |
| 118 | + onSuccess(size); |
| 119 | + } |
| 120 | + |
| 121 | + resolve(); |
| 122 | + }); |
| 123 | + |
| 124 | + archive.on('error', (err: Error) => { |
| 125 | + reject(err); |
| 126 | + }); |
| 127 | + |
| 128 | + archive.pipe(output); |
| 129 | + archive.directory(source, false); |
| 130 | + archive.finalize(); |
| 131 | + }); |
| 132 | +} |
0 commit comments