UNPKG

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