|
1 | 1 | import * as fs from "fs"; |
2 | | -import ReaddirGlob = require("readdir-glob"); |
3 | | -import stream = require("stream"); |
| 2 | +import * as ReaddirGlob from "readdir-glob"; |
| 3 | +import * as stream from "stream"; |
4 | 4 | import { ZlibOptions } from "zlib"; |
5 | 5 |
|
6 | | -type Partial<T> = { |
7 | | - [P in keyof T]?: T[P]; |
8 | | -}; |
9 | | - |
10 | | -// This library adds `cwd` to the options |
11 | | -type GlobOptions = ReaddirGlob.Options & { cwd?: string }; |
12 | | - |
13 | | -// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type -- support for ConstructorFn function and classes |
14 | | -type ConstructorFn<T> = Function | (new(...params: any[]) => T); |
15 | | - |
16 | | -declare function archiver(format: archiver.Format, options?: archiver.ArchiverOptions): archiver.Archiver; |
17 | | - |
18 | | -declare 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/no-unsafe-function-type |
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/no-unsafe-function-type |
88 | | - setModule(module: Function): this; |
89 | | - |
90 | | - pointer(): number; |
91 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
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 | | - } |
| 6 | +export type GlobOptions = ReaddirGlob.Options & { cwd?: string }; |
| 7 | + |
| 8 | +export interface EntryData { |
| 9 | + name: string; |
| 10 | + type?: "directory" | "file" | "symlink"; |
| 11 | + date?: Date | string; |
| 12 | + mode?: number; |
| 13 | + prefix?: string; |
| 14 | + stats?: fs.Stats; |
| 15 | +} |
| 16 | + |
| 17 | +export interface ZipEntryData extends EntryData { |
| 18 | + store?: boolean; |
| 19 | + comment?: string; |
| 20 | + namePrependSlash?: boolean; |
| 21 | +} |
| 22 | + |
| 23 | +export type TarEntryData = EntryData; |
| 24 | + |
| 25 | +export interface ProgressData { |
| 26 | + entries: { |
| 27 | + total: number; |
| 28 | + processed: number; |
| 29 | + }; |
| 30 | + fs: { |
| 31 | + totalBytes: number; |
| 32 | + processedBytes: number; |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +export type EntryDataFunction = (entry: EntryData) => false | EntryData; |
| 37 | + |
| 38 | +export interface ArchiverError extends Error { |
| 39 | + code: string; |
| 40 | + data?: any; |
| 41 | +} |
| 42 | + |
| 43 | +export class Archiver extends stream.Transform { |
| 44 | + constructor(options?: CoreOptions & TransformOptions); |
| 45 | + abort(): this; |
| 46 | + append(source: stream.Readable | Buffer | string, data?: EntryData | ZipEntryData | TarEntryData): this; |
| 47 | + directory( |
| 48 | + dirpath: string, |
| 49 | + destpath: false | string, |
| 50 | + data?: Partial<EntryData> | EntryDataFunction, |
| 51 | + ): this; |
| 52 | + /** @param data - entry data (optional) */ |
| 53 | + file(filename: string, data?: EntryData): this; |
| 54 | + glob(pattern: string, options?: GlobOptions, data?: Partial<EntryData>): this; |
| 55 | + finalize(): Promise<void>; |
| 56 | + pointer(): number; |
| 57 | + symlink(filepath: string, target: string, mode?: number): this; |
| 58 | + on(event: "error" | "warning", listener: (error: ArchiverError) => void): this; |
| 59 | + on(event: "data", listener: (data: Buffer) => void): this; |
| 60 | + on(event: "progress", listener: (progress: ProgressData) => void): this; |
| 61 | + on(event: "close" | "drain" | "finish", listener: () => void): this; |
| 62 | + on(event: "pipe" | "unpipe", listener: (src: stream.Readable) => void): this; |
| 63 | + on(event: "entry", listener: (entry: EntryData) => void): this; |
| 64 | + on(event: string, listener: (...args: any[]) => void): this; |
| 65 | +} |
| 66 | + |
| 67 | +export interface CoreOptions { |
| 68 | + statConcurrency?: number; |
135 | 69 | } |
136 | 70 |
|
137 | | -export = archiver; |
| 71 | +export interface TransformOptions { |
| 72 | + allowHalfOpen?: boolean; |
| 73 | + readableObjectMode?: boolean; |
| 74 | + writableObjectMode?: boolean; |
| 75 | + decodeStrings?: boolean; |
| 76 | + encoding?: BufferEncoding; |
| 77 | + highWaterMark?: number; |
| 78 | + objectMode?: boolean; |
| 79 | +} |
| 80 | + |
| 81 | +export interface ZipOptions { |
| 82 | + comment?: string; |
| 83 | + forceLocalTime?: boolean; |
| 84 | + forceZip64?: boolean; |
| 85 | + namePrependSlash?: boolean; |
| 86 | + store?: boolean; |
| 87 | + level?: number; |
| 88 | + zlib?: ZlibOptions; |
| 89 | +} |
| 90 | + |
| 91 | +export interface TarOptions { |
| 92 | + gzip?: boolean; |
| 93 | + gzipOptions?: ZlibOptions; |
| 94 | +} |
| 95 | + |
| 96 | +export type ArchiverOptions = CoreOptions & TransformOptions & ZipOptions & TarOptions; |
| 97 | + |
| 98 | +export class ZipArchive extends Archiver { |
| 99 | + constructor(options?: CoreOptions & TransformOptions & ZipOptions); |
| 100 | +} |
| 101 | + |
| 102 | +export class TarArchive extends Archiver { |
| 103 | + constructor(options?: CoreOptions & TransformOptions & TarOptions); |
| 104 | +} |
| 105 | + |
| 106 | +export class JsonArchive extends Archiver { |
| 107 | + constructor(options?: CoreOptions & TransformOptions); |
| 108 | +} |
0 commit comments