Debian Codegraph @

fs2/README.md

raw
1 # fs2
2
3 ## Functions that complement and extend [fs](http://nodejs.org/api/all.html#all_file_system) package
4
5 _Originally derived from [node-ext](https://github.com/medikoo/node-ext) package._
6
7 ## Installation
8
9 $ npm install fs2
10
11 ## API
12
13 ### access(path[, mode[, cb]]) _(fs2/access)_
14
15 Same as [fs.access](https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback). Returns promise.
16
17 ### chmod(path, mode[, cb]) _(fs2/chmod)_
18
19 Same as [fs.chmod](http://nodejs.org/api/all.html#all_fs_lchmod_path_mode_callback). Returns promise.
20
21 Not available on Windows.
22
23 Extra options:
24
25 - **append** - If set to true, then given mode is appended to file
26
27 ### copy(src, dest[, options[, cb]]) _(fs2/copy)_
28
29 Copy file, returns promise but accepts as well regular callback.
30 Eventual options are passed to underlying [fs.createWriteStream](http://nodejs.org/api/all.html#all_fs_createwritestream_path_options)
31
32 Extra options:
33
34 - **force** - Overwrite destination if exists
35 - **loose** - Do not error if source file doesn't exits, abort and resolve with `null` instead.
36 - **intermediate** - Whether to create directories recursively (if parent is not created),
37
38 ### copyDir(src, dest[, options[, cb]]) _(fs2/copy-dir)_
39
40 Copy directory recursively, returns promise but accepts as well regular callback.
41
42 Supported options:
43
44 - **loose** - Do not error if file doesn't exits or is inaccessible, return _false_ instead.
45
46 ### descriptorsHandler() _(fs2/descriptors-handler)_
47
48 Initializes _EMFILE_ errors prevention.
49
50 **To be used only in main modules. Never require it in generic module that may be required in others**
51
52 How it works? If limit of descriptors is reached it holds the calls to native functions and releases them when taken descriptors are freed.
53
54 Internally it provides same solution as [fs-graceful](https://github.com/isaacs/node-graceful-fs) module with following differences:
55
56 1. Focuses only on file descriptors limit problem
57 2. Gives access to taken/available descriptors count and allows setting of limit by external module. Thanks to that we can also cover descriptors opened by fs.watch module ([watch](#watchpath) is bound to that module)
58 3. Covers readdir calls (which also happen to throw _EMFILE_ errors)
59 4. More bulletproof (I assume) error handling logic
60
61 ### emptyDirSync(path[, options]) _(fs2/empty-dir-sync)_
62
63 Empty directory synchronously
64
65 Supported options:
66
67 - **recursive** - Attempt to empty directory content recursively (if not set, command will succeed only on removing top level directory files)
68
69 ### hasAccess(path[, options[, cb]]) _(fs2/has-access)_
70
71 Conditional version of [`access`](#accesspath-mode-cb-fs2access). Returns promise.
72
73 Supported options:
74
75 - **mode** - Passed as `mode` argument to [`access`](#accesspath-mode-cb-fs2access)
76 - **loose** - Do not error if file doesn't exits, resolve with `null` instead.
77
78 ### isDirectory(path[, cb]) _(fs2/is-directory)_
79
80 Whether path points to a directory
81 Resolves with `true` if provided path points to a directory, `false` if provided path points to a non-directory file, `null` if there's no file at path. Returns promise.
82
83 ### isFile(path[, cb]) _(fs2/is-file)_
84
85 Whether path points to existing file
86 Resolves with `true` if provided path points to a file, `false` if provided path points to existing non-directory file, `null` if there's no file at path. Returns promise.
87
88 ### isIgnored(mode, path[, options[, cb]]) _(fs2/is-ignored)_
89
90 Whether file is ignored up to predefined rules. Returns promise but regular callback is also supported.
91
92 Rules are decided by _mode_ argument. Currently only `git` mode is supported, in that case rules are searched in _.gitignore_ files (Rules have effect only if placed in valid _git_ repositories).
93 Other modes can be easily configured by extending `_ignoreModes` module (See _lib/fs/\_ignore-modes_ directory to see how it's done).
94
95 Supported options:
96
97 - **globalRules** `string|array` - additional global rules. They will be matched as if placed in filesystem root directory, it means that any rules found in existing ignore files may override them.
98 - **watch** `bool` - whether to watch for changes. If ignore state would change, returned promise would emit `change` event with new value (_true_/_false_)
99
100 ### isSymlink(path[, options[, cb]]) _(fs2/is-symlink)_
101
102 Whether path points to a symlimk
103 Resolves with `true` if provided path points to symlink, `false` if provided path points to a non-symlink file, `null` if there's no file at path. Returns promise.
104
105 Additionally resultion can be fine tuned for specific needs with below options:
106
107 Supported options:
108
109 - **linkPath** `string` - Expected linkPath to which symlink should point. If provided and symlink points elsewhere `false` is returned.
110 - **recursive** `bool` - whether check for matching `linkPath` should be applied to final path (so if symlink points to other symlink we check against the final path, and not linked symlink path)
111
112 ### lchmod(path, mode[, cb]) _(fs2/lchmod)_
113
114 Same as [fs.lchmod](http://nodejs.org/api/all.html#all_fs_lchmod_path_mode_callback). Returns promise.
115
116 Only available on Mac OS X.
117
118 ### lstat(path[, cb]) _(fs2/lstat)_
119
120 Same as [fs.lstat](http://nodejs.org/api/all.html#all_fs_lstat_path_callback). Returns promise.
121
122 ### mkdir(path[, options|mode[, cb]]) _(fs2/mkdir)_
123
124 Extended version of native _mkdir_. Returns promise
125
126 Supported options:
127
128 - **mode** - Reflects _mode_ in [native version](http://nodejs.org/api/all.html#all_fs_mkdir_path_mode_callback)
129 - **intermediate** - Whether to create directories recursively (if parent is not created), reflects `mkir -p`, internal implementation inspired by [Substack's node-mkdirp](https://github.com/substack/node-mkdirp/)
130 - **silent** - Do not throw error if directory already exists
131
132 ### readFile(path[, options][, cb]) _(fs2/read-file)_
133
134 Extended version of native [fs.readFile](http://nodejs.org/api/all.html#all_fs_readfile_filename_encoding_callback). Returns promise
135
136 Supported options:
137
138 - **loose** - Do not error if file doesn't exits or is inaccessible, return _null_ instead.
139 - **watch** - Whether to watch file for changes. Changes are emited via _change_ event on returned promise. If file was removed and _loose_ option is off, _end_ event is emitted and watcher is closed
140
141 ### readdir(path[, options[, cb]]) _(fs2/readdir)_
142
143 Extended version of native [fs.readdir](http://nodejs.org/api/all.html#all_fs_readdir_path_callback). Returns promise
144
145 Suported options:
146
147 - **loose** `boolean`- Resolve with `null` (instead of error) if directory doesn't exist
148 - **depth** `number`- Level of recurse into subdirectories. Defaults to _0_ which resembles behavior of native version. If you want to recurse without any nest limitation just provide _Infinity_
149 - **type** `object`- Which type of files should be returned. By default all files are returned. [Stats methods](http://nodejs.org/api/all.html#all_class_fs_stats) shows how many different types can be returned. To narrow it down provide a hash. e.g. `{ file: true, symbolicLink: true }`.
150 - **pattern** `regexp`- Filter returned files by specific pattern. Pattern should be regular expression that would be matched against full path.
151 - **watch** `bool` - Watch directory for changes. Changes are emitted on returned promise with `change` events. _event_ object states which files were added (`event.added`) and which were removed (`event.removed`), **_Starting from next release (v0.4) this functionality will most likely be provided as valid Node.js [stream](http://nodejs.org/api/all.html#all_stream)_**
152 - **stream** `bool` - Whether to provide data continuously. Currently it's not provided as a [stream](http://nodejs.org/api/all.html#all_stream) per se (it would be starting from next release, v0.4), data is emited as `change` events on returned promise object, structure of _event_ objects described under _watch_ option
153 - **ignoreRules** `string|array` - Whether to obey ignore rules found in ignore files. See _[fs.isIgnored](#isignoredmode-path-options-cb)_ for more information
154 - **globalRules** `string|array` - Global rules that complement ignoreRules. See _[fs.isIgnored](#isignoredmode-path-options-cb)_ for more information.
155 - **dirFilter** `function|regexp` - Filter out directories into which we should not recurse to. Can be provided as function which having directory name should return boolaen (`true` if we should recurse into directory), or as regex which if matches means that directory should be recurse into. Neverthless `depth` setting is supported unconditionally
156
157 ### readlink(path[, options[, cb]]) _(fs2/readlink)_
158
159 Same as [fs.readlink](http://nodejs.org/api/all.html#fs_fs_readlink_path_options_callback). Returns promise.
160 Supports `loose: true` option, which when passed, resolves with `null` (instead of exception) if file at path is not a symlink or it doesn't exist.
161
162 ### realpath(path[, options[, cb]]) _(fs2/realpath)_
163
164 Same as [fs.realpath](http://nodejs.org/api/all.html#fs_fs_realpath_path_options_callback). Returns promise.
165 Supports `loose: true` option, which when passed, resolves with `null` (instead of exception) if path doesn't exist
166
167 ### rename(oldPath, newPath[, cb]) _(fs2/rename)_
168
169 Same as [fs.rename](http://nodejs.org/api/all.html#all_fs_rename_oldpath_newpath_callback). Returns promise.
170
171 Supported options:
172
173 - **intermediate** - Whether to create directories recursively (if parent is not created)
174
175 ### rmdir(path[, options[, cb]]) _(fs2/rmdir)_
176
177 Extended version of native _rmdir_. Returns promise
178
179 Supported options:
180
181 - **recursive** - Attempt to remove directory with subdirectories recursively.
182 - **force** - Attempt to remove other files within directory as well.
183 - **loose** - Do not error if directory doesn't exist
184
185 ### rmdirSync(path[, options]) _(fs2/rmdir-sync)_
186
187 Extended version of native _rmdirSynnc_.
188
189 Supported options:
190
191 - **recursive** - Attempt to remove directory with all its content recursively.
192
193 ### stat(path[, cb]) _(fs2/stat)_
194
195 Same as [fs.stat](http://nodejs.org/api/all.html#all_fs_stat_path_callback). Returns promise.
196
197 ### symlink(srcPath, dstPath[, type[, cb]]) _(fs2/symlink)_
198
199 Same as [fs.symlink](http://nodejs.org/api/all.html#all_fs_symlink_srcpath_dstpath_type_callback). Returns promise that resolves with `true` if symlink was created (`false` can be returned with `loose` option, when symlink already exists)
200
201 Supported options:
202
203 - **intermediate** - Attempt to create directory with subdirectories in which symlink is expected to be placed if they do not exist
204 - **loose** - Do not error if same symlink at path already exists
205 - **force** - If there's other file at the path, remove and retry to create symlink
206
207 ### typeByStats(stats) _(fs2/type-by-stats)_
208
209 Returns type of file according to provided [stats](http://nodejs.org/api/all.html#all_class_fs_stats) object.
210
211 ### unlink(path[, cb]) _(fs2/unlink)_
212
213 Same as [fs.unlink](http://nodejs.org/api/all.html#all_fs_unlink_path_callback). Returns promise.
214
215 Supported options:
216
217 - **loose** - Do not error if file doesn't exist
218
219 ### unlinkSync(path) _(fs2/unlink-sync)_
220
221 Same as [fs.unlinkSync](http://nodejs.org/api/all.html#all_fs_unlink_path_callback), but with support for extra options.
222
223 Supported options:
224
225 - **loose** - Do not error if file doesn't exist
226
227 ### watchPath(path) _(fs2/watch-path)_
228
229 Watch specific path for changes. It's about observing specific file path (not directory content). `change` events are emitted with event object where `event.type` says wether file was created, modified or removed.
230
231 ### watch(path) _(fs2/watch)_
232
233 Watch file for changes.
234 [fs.watch](http://nodejs.org/api/all.html#all_fs_watch_filename_options_listener) wrapper that works same way on every platform, always configured in _persistent: false_ mode.
235 It's aware of open file descriptors limitations, if _EMFILE_ error is approach, switch to alternative mode that pings file stats (see [fs.watchFile](http://nodejs.org/api/all.html#all_fs_watchfile_filename_options_listener)) is made.
236
237 ### writeFile(filename, data[, options|encoding[, callback]]) _(fs2/write-file)_
238
239 Same as native [fs.writeFile](http://nodejs.org/api/all.html#all_fs_writefile_filename_data_encoding_callback) but safe for simultaneous calls of write to same file (in such case current write will be abandonded, and new would be started).
240
241 Supported options:
242
243 - **encoding** - Reflects _encoding_ in [native version](http://nodejs.org/api/all.html#all_fs_writefile_filename_data_options_callback)
244 - **intermediate** - In case directory doesn't exist, whether to create full directory path
245
246 ## Tests [![Build Status](https://travis-ci.org/medikoo/fs2.png?branch=master)](https://travis-ci.org/medikoo/fs2)
247
248 $ npm test