Skip to content

Commit 2c1ca18

Browse files
authored
Fix tsc errors after generating d.ts files (teambit#2628)
* fix tsc errors after generating d.ts files * fix eol tsc errors * fix types of network/fs/ssh and old extensions * fix types of commands * fix new commands options types * fix legacy compiler/tester tsc * remove @types/vinyl, add a modified version of this type-def into a new dir "custom-types" and configure tsconfig.json to search types in this dir. the modified version allows extending Vinyl class with no errors * fix tsc errors coming from old bit-javascript code
1 parent eb8114c commit 2c1ca18

File tree

122 files changed

+651
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+651
-413
lines changed

custom-types/vinyl/index.d.ts

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

0 commit comments

Comments
 (0)