UNPKG

5.65 kBTypeScriptView Raw
1import Item = require('./item');
2import File = require('./file');
3import Directory = require('./directory');
4import SymbolicLink = require('./symlink');
5
6export = FileSystem;
7
8declare class FileSystem {
9 /**
10 * Create a new file system.
11 *
12 * @param options Any filesystem options.
13 * @param options.createCwd Create a directory for `process.cwd()` (defaults
14 * to `true`).
15 * @param options.createTmp Create a directory for `os.tmpdir()` (defaults
16 * to `true`).
17 */
18 constructor(options?: FileSystem.Options);
19
20 /** Get the root directory. */
21 getRoot(): Directory;
22
23 /**
24 * Get a file system item.
25 *
26 * @param filepath Path to item.
27 * @return The item (or null if not found).
28 */
29 getItem(filepath: string): Item;
30
31 /**
32 * Configure a mock file system.
33 *
34 * @param paths Config object.
35 * @param options Any filesystem options.
36 * @param options.createCwd Create a directory for `process.cwd()` (defaults
37 * to `true`).
38 * @param options.createTmp Create a directory for `os.tmpdir()` (defaults
39 * to `true`).
40 * @return Mock file system.
41 */
42 static create(paths?: FileSystem.DirectoryItems, options?: FileSystem.Options): FileSystem;
43
44 /**
45 * Generate a factory for new files.
46 *
47 * @param config File config.
48 * @return Factory that creates a new file.
49 */
50 static file(config?: FileSystem.FileOptions): () => File;
51
52 /**
53 * Generate a factory for new symbolic links.
54 *
55 * @param config File config.
56 * @return Factory that creates a new symbolic link.
57 */
58 static directory(config?: FileSystem.DirectoryOptions): () => Directory;
59
60 /**
61 * Generate a factory for new directories.
62 *
63 * @param config File config.
64 * @return Factory that creates a new directory.
65 */
66 static symlink(config: FileSystem.SymlinkOptions): () => SymbolicLink;
67}
68
69declare namespace FileSystem {
70 type DirectoryItem =
71 | string
72 | Buffer
73 | (() => File)
74 | (() => Directory)
75 | (() => SymbolicLink)
76 | DirectoryItems;
77
78 interface DirectoryItems {
79 [name: string]: DirectoryItem;
80 }
81
82 interface Options {
83 /**
84 * Create a directory for `process.cwd()`. This is `true` by default.
85 */
86 createCwd?: boolean;
87 /**
88 * Create a directory for `os.tmpdir()`. This is `true` by default.
89 */
90 createTmp?: boolean;
91 }
92
93 interface FileOptions {
94 /** File contents */
95 content?: string | Buffer;
96 /** File mode (permission and sticky bits). Defaults to `0666`. */
97 mode?: number;
98 /** The user id. Defaults to `process.getuid()`. */
99 uid?: number;
100 /** The group id. Defaults to `process.getgid()`. */
101 gid?: number;
102 /**
103 * The last file access time. Defaults to `new Date()`. Updated when
104 * file contents are accessed.
105 */
106 atime?: Date;
107 /**
108 * The last file change time. Defaults to `new Date()`. Updated when
109 * file owner or permissions change.
110 */
111 ctime?: Date;
112 /**
113 * The last file modification time. Defaults to `new Date()`. Updated
114 * when file contents change.
115 */
116 mtime?: Date;
117 /**
118 * The time of file creation. Defaults to `new Date()`.
119 */
120 birthtime?: Date;
121 }
122
123 interface DirectoryOptions {
124 /** Directory mode (permission and sticky bits). Defaults to `0777`. */
125 mode?: number;
126 /** The user id. Defaults to `process.getuid()`. */
127 uid?: number;
128 /** The group id. Defaults to `process.getgid()`. */
129 gid?: number;
130 /**
131 * The last directory access time. Defaults to `new Date()`.
132 */
133 atime?: Date;
134 /**
135 * The last directory change time. Defaults to `new Date()`. Updated
136 * when owner or permissions change.
137 */
138 ctime?: Date;
139 /**
140 * The last directory modification time. Defaults to `new Date()`.
141 * Updated when an item is added, removed, or renamed.
142 */
143 mtime?: Date;
144 /**
145 * The time of directory creation. Defaults to `new Date()`.
146 */
147 birthtime?: Date;
148 /**
149 * Directory contents. Members will generate additional files,
150 * directories, or symlinks.
151 */
152 items?: DirectoryItems;
153 }
154
155 interface SymlinkOptions {
156 /** Path to the source (required). */
157 path: string;
158 /** Symlink mode (permission and sticky bits). Defaults to `0666`. */
159 mode?: number;
160 /** The user id. Defaults to `process.getuid()`. */
161 uid?: number;
162 /** The group id. Defaults to `process.getgid()`. */
163 gid?: number;
164 /** The last symlink access time. Defaults to `new Date()`. */
165 atime?: Date;
166 /** The last symlink change time. Defaults to `new Date()`. */
167 ctime?: Date;
168 /** The last symlink modification time. Defaults to `new Date()`. */
169 mtime?: Date;
170 /** The time of symlink creation. Defaults to `new Date()`. */
171 birthtime?: Date;
172 }
173
174 interface LoaderOptions {
175 /** File content isn't loaded until explicitly read. */
176 lazy?: boolean;
177 /** Load all files and directories recursively. */
178 recursive?: boolean;
179 }
180
181 function getPathParts(filepath: string): string[];
182}
183
\No newline at end of file