UNPKG

10.5 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as fs from "fs";
4
5interface ConstructorOptions {
6 /**
7 * The current working directory of the file. Default: process.cwd()
8 */
9 cwd?: string | undefined;
10
11 /**
12 * Used for relative pathing. Typically where a glob starts. Default: options.cwd
13 */
14 base?: string | undefined;
15
16 /**
17 * Full path to the file.
18 */
19 path?: string | undefined;
20
21 /**
22 * Stores the path history. If `options.path` and `options.history` are both passed,
23 * `options.path` is appended to `options.history`. All `options.history` paths are
24 * normalized by the `file.path` setter.
25 * Default: `[]` (or `[options.path]` if `options.path` is passed)
26 */
27 history?: string[] | undefined;
28
29 /**
30 * The result of an fs.stat call. This is how you mark the file as a directory or
31 * symbolic link. See `isDirectory()`, `isSymbolic()` and `fs.Stats` for more information.
32 * https://nodejs.org/api/fs.html#fs_class_fs_stats
33 */
34 stat?: fs.Stats | undefined;
35
36 /**
37 * File contents.
38 * Type: `Buffer`, `Stream`, or null
39 * Default: null
40 */
41 contents?: Buffer | NodeJS.ReadableStream | null | undefined;
42
43 /**
44 * Any custom option properties will be directly assigned to the new Vinyl object.
45 */
46 [customOption: string]: any;
47}
48
49interface FileConstructor {
50 new(options: ConstructorOptions & { contents: null }): File.NullFile;
51 new(options: ConstructorOptions & { contents: Buffer }): File.BufferFile;
52 new(
53 options: ConstructorOptions & { contents: NodeJS.ReadableStream },
54 ): File.StreamFile;
55 new(options?: ConstructorOptions): File;
56
57 /**
58 * Checks if a given object is a vinyl file.
59 */
60 isVinyl(obj: any): obj is File;
61
62 /**
63 * Checks if a property is not managed internally.
64 */
65 isCustomProp(name: string): boolean;
66
67 prototype: File;
68}
69
70export = File;
71
72declare let File: FileConstructor;
73
74interface File {
75 /**
76 * Gets and sets the contents of the file. If set to a `Stream`, it is wrapped in
77 * a `cloneable-readable` stream.
78 *
79 * Throws when set to any value other than a `Stream`, a `Buffer` or `null`.
80 */
81 contents: Buffer | NodeJS.ReadableStream | null;
82
83 /**
84 * Gets and sets current working directory. Will always be normalized and have trailing
85 * separators removed.
86 *
87 * Throws when set to any value other than non-empty strings.
88 */
89 cwd: string;
90
91 //
92 /**
93 * Gets and sets base directory. Used for relative pathing (typically where a glob starts).
94 * When `null` or `undefined`, it simply proxies the `file.cwd` property. Will always be
95 * normalized and have trailing separators removed.
96 *
97 * Throws when set to any value other than non-empty strings or `null`/`undefined`.
98 *
99 * The setter's type is actually `string | null | undefined`, but TypeScript doesn't allow
100 * get/set accessors to be of different type. The property is declared as `string` for the
101 * compiler not to require useless null checks for the getter. (Hopefully, noone will need
102 * to assign `null` to this property.)
103 */
104 base: string;
105
106 /**
107 * Gets and sets the absolute pathname string or `undefined`. Setting to a different value
108 * appends the new path to `file.history`. If set to the same value as the current path, it
109 * is ignored. All new values are normalized and have trailing separators removed.
110 *
111 * Throws when set to any value other than a string.
112 *
113 * The getter is actually of type `string | undefined` whereas the setter is just `string`,
114 * however TypeScript doesn't allow get/set accessors to be of different type. See the
115 * comment for the `base` properties.
116 */
117 path: string;
118
119 /**
120 * Array of `file.path` values the Vinyl object has had, from `file.history[0]` (original)
121 * through `file.history[file.history.length - 1]` (current). `file.history` and its elements
122 * should normally be treated as read-only and only altered indirectly by setting `file.path`.
123 */
124 readonly history: readonly string[];
125
126 /**
127 * Gets the result of `path.relative(file.base, file.path)`.
128 *
129 * Throws when set or when `file.path` is not set.
130 *
131 * Example:
132 *
133 * ```js
134 * var file = new File({
135 * cwd: '/',
136 * base: '/test/',
137 * path: '/test/file.js'
138 * });
139 *
140 * console.log(file.relative); // file.js
141 * ```
142 */
143 relative: string;
144
145 /**
146 * Gets and sets the dirname of `file.path`. Will always be normalized and have trailing
147 * separators removed.
148 *
149 * Throws when `file.path` is not set.
150 *
151 * Example:
152 *
153 * ```js
154 * var file = new File({
155 * cwd: '/',
156 * base: '/test/',
157 * path: '/test/file.js'
158 * });
159 *
160 * console.log(file.dirname); // /test
161 *
162 * file.dirname = '/specs';
163 *
164 * console.log(file.dirname); // /specs
165 * console.log(file.path); // /specs/file.js
166 * ```
167 */
168 dirname: string;
169
170 /**
171 * Gets and sets the basename of `file.path`.
172 *
173 * Throws when `file.path` is not set.
174 *
175 * Example:
176 *
177 * ```js
178 * var file = new File({
179 * cwd: '/',
180 * base: '/test/',
181 * path: '/test/file.js'
182 * });
183 *
184 * console.log(file.basename); // file.js
185 *
186 * file.basename = 'file.txt';
187 *
188 * console.log(file.basename); // file.txt
189 * console.log(file.path); // /test/file.txt
190 * ```
191 */
192 basename: string;
193
194 /**
195 * Gets and sets stem (filename without suffix) of `file.path`.
196 *
197 * Throws when `file.path` is not set.
198 *
199 * Example:
200 *
201 * ```js
202 * var file = new File({
203 * cwd: '/',
204 * base: '/test/',
205 * path: '/test/file.js'
206 * });
207 *
208 * console.log(file.stem); // file
209 *
210 * file.stem = 'foo';
211 *
212 * console.log(file.stem); // foo
213 * console.log(file.path); // /test/foo.js
214 * ```
215 */
216 stem: string;
217
218 /**
219 * Gets and sets extname of `file.path`.
220 *
221 * Throws when `file.path` is not set.
222 *
223 * Example:
224 *
225 * ```js
226 * var file = new File({
227 * cwd: '/',
228 * base: '/test/',
229 * path: '/test/file.js'
230 * });
231 *
232 * console.log(file.extname); // .js
233 *
234 * file.extname = '.txt';
235 *
236 * console.log(file.extname); // .txt
237 * console.log(file.path); // /test/file.txt
238 * ```
239 */
240 extname: string;
241
242 /**
243 * Gets and sets the path where the file points to if it's a symbolic link. Will always
244 * be normalized and have trailing separators removed.
245 *
246 * Throws when set to any value other than a string.
247 */
248 symlink: string | null;
249
250 stat: fs.Stats | null;
251
252 [customProperty: string]: any;
253
254 /**
255 * Returns `true` if the file contents are a `Buffer`, otherwise `false`.
256 */
257 isBuffer(): this is File.BufferFile;
258
259 /**
260 * Returns `true` if the file contents are a `Stream`, otherwise `false`.
261 */
262 isStream(): this is File.StreamFile;
263
264 /**
265 * Returns `true` if the file contents are `null`, otherwise `false`.
266 */
267 isNull(): this is File.NullFile;
268
269 /**
270 * Returns `true` if the file represents a directory, otherwise `false`.
271 *
272 * A file is considered a directory when:
273 *
274 * - `file.isNull()` is `true`
275 * - `file.stat` is an object
276 * - `file.stat.isDirectory()` returns `true`
277 *
278 * When constructing a Vinyl object, pass in a valid `fs.Stats` object via `options.stat`.
279 * If you are mocking the `fs.Stats` object, you may need to stub the `isDirectory()` method.
280 */
281 isDirectory(): this is File.DirectoryFile;
282
283 /**
284 * Returns `true` if the file represents a symbolic link, otherwise `false`.
285 *
286 * A file is considered symbolic when:
287 *
288 * - `file.isNull()` is `true`
289 * - `file.stat` is an object
290 * - `file.stat.isSymbolicLink()` returns `true`
291 *
292 * When constructing a Vinyl object, pass in a valid `fs.Stats` object via `options.stat`.
293 * If you are mocking the `fs.Stats` object, you may need to stub the `isSymbolicLink()` method.
294 */
295 isSymbolic(): this is File.SymbolicFile;
296
297 /**
298 * Returns a new Vinyl object with all attributes cloned.
299 *
300 * __By default custom attributes are cloned deeply.__
301 *
302 * If `options` or `options.deep` is `false`, custom attributes will not be cloned deeply.
303 *
304 * If `file.contents` is a `Buffer` and `options.contents` is `false`, the `Buffer` reference
305 * will be reused instead of copied.
306 */
307 clone(opts?: { contents?: boolean | undefined; deep?: boolean | undefined } | boolean): this;
308
309 /**
310 * Returns a formatted-string interpretation of the Vinyl object.
311 * Automatically called by node's `console.log`.
312 */
313 inspect(): string;
314
315 /**
316 * @deprecated This method was removed in v2.0.
317 * If file.contents is a Buffer, it will write it to the stream.
318 * If file.contents is a Stream, it will pipe it to the stream.
319 * If file.contents is null, it will do nothing.
320 */
321 pipe<T extends NodeJS.WritableStream>(
322 stream: T,
323 opts?: {
324 /**
325 * If false, the destination stream will not be ended (same as node core).
326 */
327 end?: boolean | undefined;
328 },
329 ): T;
330}
331
332declare namespace File {
333 // See https://github.com/Microsoft/TypeScript/issues/11796
334
335 interface BufferFile extends File {
336 contents: Buffer;
337 isStream(): this is never;
338 isNull(): this is never;
339 isDirectory(): this is never;
340 isSymbolic(): this is never;
341 }
342
343 interface StreamFile extends File {
344 contents: NodeJS.ReadableStream;
345 isBuffer(): this is never;
346 isNull(): this is never;
347 isDirectory(): this is never;
348 isSymbolic(): this is never;
349 }
350
351 interface NullFile extends File {
352 contents: null;
353 isStream(): this is never;
354 isBuffer(): this is never;
355 isDirectory(): this is DirectoryFile;
356 isSymbolic(): this is SymbolicFile;
357 }
358
359 interface DirectoryFile extends NullFile {
360 isSymbolic(): this is never;
361 }
362
363 interface SymbolicFile extends NullFile {
364 isDirectory(): this is never;
365 }
366}