UNPKG

5.03 kBTypeScriptView Raw
1import * as fs from "fs";
2import * as ReaddirGlob from "readdir-glob";
3import * as stream from "stream";
4import { ZlibOptions } from "zlib";
5
6type Partial<T> = {
7 [P in keyof T]?: T[P];
8};
9
10// This library adds `cwd` to the options
11type GlobOptions = ReaddirGlob.Options & { cwd?: string };
12
13// eslint-disable-next-line @typescript-eslint/ban-types -- support for ConstructorFn function and classes
14type ConstructorFn<T> = Function | (new(...params: any[]) => T);
15
16declare function archiver(format: archiver.Format, options?: archiver.ArchiverOptions): archiver.Archiver;
17
18declare namespace archiver {
19 type Format = "zip" | "tar";
20
21 function create(format: string, options?: ArchiverOptions): Archiver;
22
23 /** Check if the format is already registered. */
24 function isRegisteredFormat(format: string): boolean;
25 // eslint-disable-next-line @typescript-eslint/ban-types -- Function
26 function registerFormat(format: string, module: Function): void;
27
28 interface EntryData {
29 /** Sets the entry name including internal path */
30 name: string;
31 /** Sets the entry date */
32 date?: Date | string | undefined;
33 /** Sets the entry permissions */
34 mode?: number | undefined;
35 /**
36 * Sets a path prefix for the entry name.
37 * Useful when working with methods like `directory` or `glob`
38 */
39 prefix?: string | undefined;
40 /**
41 * Sets the fs stat data for this entry allowing
42 * for reduction of fs stat calls when stat data is already known
43 */
44 stats?: fs.Stats | undefined;
45 }
46
47 interface ZipEntryData extends EntryData {
48 /** Sets the compression method to STORE */
49 store?: boolean | undefined;
50 }
51
52 type TarEntryData = EntryData;
53
54 interface ProgressData {
55 entries: {
56 total: number;
57 processed: number;
58 };
59 fs: {
60 totalBytes: number;
61 processedBytes: number;
62 };
63 }
64
65 /** A function that lets you either opt out of including an entry (by returning false), or modify the contents of an entry as it is added (by returning an EntryData) */
66 type EntryDataFunction = (entry: EntryData) => false | EntryData;
67
68 class ArchiverError extends Error {
69 code: string; // Since archiver format support is modular, we cannot enumerate all possible error codes, as the modules can throw arbitrary ones.
70 data: any;
71 path?: any;
72
73 constructor(code: string, data: any);
74 }
75
76 interface Archiver extends stream.Transform {
77 abort(): this;
78 append(source: stream.Readable | Buffer | string, data?: EntryData | ZipEntryData | TarEntryData): this;
79
80 /** if false is passed for destpath, the path of a chunk of data in the archive is set to the root */
81 directory(dirpath: string, destpath: false | string, data?: Partial<EntryData> | EntryDataFunction): this;
82 file(filename: string, data: EntryData): this;
83 glob(pattern: string, options?: GlobOptions, data?: Partial<EntryData>): this;
84 finalize(): Promise<void>;
85
86 setFormat(format: string): this;
87 // eslint-disable-next-line @typescript-eslint/ban-types -- Function
88 setModule(module: Function): this;
89
90 pointer(): number;
91 // eslint-disable-next-line @typescript-eslint/ban-types -- Function
92 use(plugin: Function): this;
93
94 symlink(filepath: string, target: string, mode?: number): this;
95
96 on(event: "error" | "warning", listener: (error: ArchiverError) => void): this;
97 on(event: "data", listener: (data: Buffer) => void): this;
98 on(event: "progress", listener: (progress: ProgressData) => void): this;
99 on(event: "close" | "drain" | "finish", listener: () => void): this;
100 on(event: "pipe" | "unpipe", listener: (src: stream.Readable) => void): this;
101 on(event: "entry", listener: (entry: EntryData) => void): this;
102 on(event: string, listener: (...args: any[]) => void): this;
103 }
104
105 type ArchiverOptions = CoreOptions & TransformOptions & ZipOptions & TarOptions;
106
107 interface CoreOptions {
108 statConcurrency?: number | undefined;
109 }
110
111 interface TransformOptions {
112 allowHalfOpen?: boolean | undefined;
113 readableObjectMode?: boolean | undefined;
114 writeableObjectMode?: boolean | undefined;
115 decodeStrings?: boolean | undefined;
116 encoding?: string | undefined;
117 highWaterMark?: number | undefined;
118 objectmode?: boolean | undefined;
119 }
120
121 interface ZipOptions {
122 comment?: string | undefined;
123 forceLocalTime?: boolean | undefined;
124 forceZip64?: boolean | undefined;
125 /** @default false */
126 namePrependSlash?: boolean | undefined;
127 store?: boolean | undefined;
128 zlib?: ZlibOptions | undefined;
129 }
130
131 interface TarOptions {
132 gzip?: boolean | undefined;
133 gzipOptions?: ZlibOptions | undefined;
134 }
135}
136
137export = archiver;