@ohos-ports/stdlib-fs 0.0.0-stage → 0.4.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +230 -2
- package/SECURITY.md +5 -0
- package/append-file/README.md +239 -0
- package/append-file/bin/cli +110 -0
- package/append-file/docs/repl.txt +72 -0
- package/append-file/docs/types/index.d.ts +133 -0
- package/append-file/docs/types/test.ts +193 -0
- package/append-file/docs/usage.txt +10 -0
- package/append-file/etc/cli_opts.json +22 -0
- package/append-file/lib/async.js +58 -0
- package/append-file/lib/index.js +61 -0
- package/append-file/lib/sync.js +58 -0
- package/append-file/package.json +69 -0
- package/close/README.md +172 -0
- package/close/docs/repl.txt +45 -0
- package/close/docs/types/index.d.ts +135 -0
- package/close/docs/types/test.ts +89 -0
- package/close/lib/index.js +73 -0
- package/close/lib/main.js +68 -0
- package/close/lib/sync.js +65 -0
- package/close/package.json +64 -0
- package/docs/types/index.d.ts +502 -0
- package/exists/README.md +252 -0
- package/exists/bin/cli +83 -0
- package/exists/docs/repl.txt +42 -0
- package/exists/docs/types/index.d.ts +112 -0
- package/exists/docs/types/test.ts +87 -0
- package/exists/docs/usage.txt +8 -0
- package/exists/etc/cli_opts.json +14 -0
- package/exists/lib/async.js +85 -0
- package/exists/lib/index.js +66 -0
- package/exists/lib/sync.js +62 -0
- package/exists/package.json +69 -0
- package/lib/index.js +186 -0
- package/open/README.md +204 -0
- package/open/docs/repl.txt +65 -0
- package/open/docs/types/index.d.ts +150 -0
- package/open/docs/types/test.ts +129 -0
- package/open/lib/defaults.json +4 -0
- package/open/lib/index.js +63 -0
- package/open/lib/main.js +64 -0
- package/open/lib/sync.js +68 -0
- package/open/package.json +64 -0
- package/package.json +67 -4
- package/read-dir/README.md +232 -0
- package/read-dir/bin/cli +83 -0
- package/read-dir/docs/repl.txt +44 -0
- package/read-dir/docs/types/index.d.ts +99 -0
- package/read-dir/docs/types/test.ts +85 -0
- package/read-dir/docs/usage.txt +8 -0
- package/read-dir/etc/cli_opts.json +14 -0
- package/read-dir/lib/async.js +57 -0
- package/read-dir/lib/index.js +62 -0
- package/read-dir/lib/sync.js +52 -0
- package/read-dir/package.json +69 -0
- package/read-file/README.md +246 -0
- package/read-file/bin/cli +91 -0
- package/read-file/docs/repl.txt +68 -0
- package/read-file/docs/types/index.d.ts +138 -0
- package/read-file/docs/types/test.ts +147 -0
- package/read-file/docs/usage.txt +10 -0
- package/read-file/etc/cli_opts.json +21 -0
- package/read-file/lib/index.js +61 -0
- package/read-file/lib/main.js +59 -0
- package/read-file/lib/sync.js +61 -0
- package/read-file/package.json +68 -0
- package/read-file-list/README.md +197 -0
- package/read-file-list/bin/cli +100 -0
- package/read-file-list/docs/repl.txt +81 -0
- package/read-file-list/docs/types/index.d.ts +166 -0
- package/read-file-list/docs/types/test.ts +166 -0
- package/read-file-list/docs/usage.txt +10 -0
- package/read-file-list/etc/cli_opts.json +21 -0
- package/read-file-list/lib/async.js +202 -0
- package/read-file-list/lib/clear_pending.js +47 -0
- package/read-file-list/lib/clear_timeout.js +23 -0
- package/read-file-list/lib/config.json +4 -0
- package/read-file-list/lib/delay.js +47 -0
- package/read-file-list/lib/index.js +66 -0
- package/read-file-list/lib/sync.js +94 -0
- package/read-file-list/package.json +72 -0
- package/read-json/README.md +176 -0
- package/read-json/docs/repl.txt +72 -0
- package/read-json/docs/types/index.d.ts +170 -0
- package/read-json/docs/types/test.ts +175 -0
- package/read-json/lib/async.js +114 -0
- package/read-json/lib/index.js +65 -0
- package/read-json/lib/sync.js +89 -0
- package/read-json/package.json +66 -0
- package/read-ndjson/README.md +166 -0
- package/read-ndjson/docs/repl.txt +72 -0
- package/read-ndjson/docs/types/index.d.ts +170 -0
- package/read-ndjson/docs/types/test.ts +179 -0
- package/read-ndjson/lib/async.js +114 -0
- package/read-ndjson/lib/index.js +65 -0
- package/read-ndjson/lib/sync.js +89 -0
- package/read-ndjson/package.json +68 -0
- package/read-wasm/README.md +158 -0
- package/read-wasm/docs/repl.txt +58 -0
- package/read-wasm/docs/types/index.d.ts +153 -0
- package/read-wasm/docs/types/test.ts +127 -0
- package/read-wasm/lib/async.js +106 -0
- package/read-wasm/lib/index.js +67 -0
- package/read-wasm/lib/sync.js +86 -0
- package/read-wasm/package.json +67 -0
- package/rename/README.md +259 -0
- package/rename/bin/cli +89 -0
- package/rename/docs/repl.txt +80 -0
- package/rename/docs/types/index.d.ts +109 -0
- package/rename/docs/types/test.ts +111 -0
- package/rename/docs/usage.txt +7 -0
- package/rename/etc/cli_opts.json +15 -0
- package/rename/lib/index.js +60 -0
- package/rename/lib/main.js +51 -0
- package/rename/lib/sync.js +53 -0
- package/rename/package.json +73 -0
- package/resolve-parent-path/README.md +204 -0
- package/resolve-parent-path/bin/cli +85 -0
- package/resolve-parent-path/docs/repl.txt +58 -0
- package/resolve-parent-path/docs/types/index.d.ts +128 -0
- package/resolve-parent-path/docs/types/test.ts +117 -0
- package/resolve-parent-path/docs/usage.txt +9 -0
- package/resolve-parent-path/etc/cli_opts.json +17 -0
- package/resolve-parent-path/lib/async.js +116 -0
- package/resolve-parent-path/lib/index.js +58 -0
- package/resolve-parent-path/lib/sync.js +83 -0
- package/resolve-parent-path/lib/validate.js +67 -0
- package/resolve-parent-path/package.json +78 -0
- package/resolve-parent-path-by/README.md +220 -0
- package/resolve-parent-path-by/docs/repl.txt +105 -0
- package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
- package/resolve-parent-path-by/docs/types/test.ts +225 -0
- package/resolve-parent-path-by/lib/async.js +156 -0
- package/resolve-parent-path-by/lib/index.js +67 -0
- package/resolve-parent-path-by/lib/sync.js +98 -0
- package/resolve-parent-path-by/lib/validate.js +67 -0
- package/resolve-parent-path-by/package.json +76 -0
- package/resolve-parent-paths/README.md +228 -0
- package/resolve-parent-paths/bin/cli +93 -0
- package/resolve-parent-paths/docs/repl.txt +84 -0
- package/resolve-parent-paths/docs/types/index.d.ts +145 -0
- package/resolve-parent-paths/docs/types/test.ts +196 -0
- package/resolve-parent-paths/docs/usage.txt +10 -0
- package/resolve-parent-paths/etc/cli_opts.json +18 -0
- package/resolve-parent-paths/lib/index.js +60 -0
- package/resolve-parent-paths/lib/main.js +415 -0
- package/resolve-parent-paths/lib/sync.js +231 -0
- package/resolve-parent-paths/lib/validate.js +81 -0
- package/resolve-parent-paths/package.json +78 -0
- package/unlink/README.md +233 -0
- package/unlink/bin/cli +81 -0
- package/unlink/docs/repl.txt +62 -0
- package/unlink/docs/types/index.d.ts +105 -0
- package/unlink/docs/types/test.ts +85 -0
- package/unlink/docs/usage.txt +7 -0
- package/unlink/etc/cli_opts.json +15 -0
- package/unlink/lib/index.js +60 -0
- package/unlink/lib/main.js +50 -0
- package/unlink/lib/sync.js +52 -0
- package/unlink/package.json +70 -0
- package/write-file/README.md +250 -0
- package/write-file/bin/cli +110 -0
- package/write-file/docs/repl.txt +74 -0
- package/write-file/docs/types/index.d.ts +133 -0
- package/write-file/docs/types/test.ts +193 -0
- package/write-file/docs/usage.txt +10 -0
- package/write-file/etc/cli_opts.json +22 -0
- package/write-file/lib/index.js +60 -0
- package/write-file/lib/main.js +58 -0
- package/write-file/lib/sync.js +58 -0
- package/write-file/package.json +68 -0
package/exists/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2018 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
# Exists
|
|
22
|
+
|
|
23
|
+
> Test whether a path exists on the filesystem.
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var exists = require( '@stdlib/fs/exists' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### exists( path, clbk )
|
|
34
|
+
|
|
35
|
+
Asynchronously tests whether a path exists on the filesystem.
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
exists( __dirname, done );
|
|
39
|
+
|
|
40
|
+
function done( bool ) {
|
|
41
|
+
if ( bool ) {
|
|
42
|
+
console.log( '...path exists.' );
|
|
43
|
+
} else {
|
|
44
|
+
console.log( '...path does not exist.' );
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The above callback signature matches the now **deprecated** [`fs.exists()`][node-fs-exists] API. The function also accepts the more conventional `error`-first style callback signature found in most asynchronous Node APIs.
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
exists( __dirname, done );
|
|
53
|
+
|
|
54
|
+
function done( error, bool ) {
|
|
55
|
+
if ( error ) {
|
|
56
|
+
console.error( error.message );
|
|
57
|
+
}
|
|
58
|
+
if ( bool ) {
|
|
59
|
+
console.log( '...path exists.' );
|
|
60
|
+
} else {
|
|
61
|
+
console.log( '...path does not exist.' );
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### exists.sync( path )
|
|
67
|
+
|
|
68
|
+
Synchronously tests whether a path exists on the filesystem.
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
var bool = exists.sync( __dirname );
|
|
72
|
+
// returns <boolean>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
</section>
|
|
76
|
+
|
|
77
|
+
<!-- /.usage -->
|
|
78
|
+
|
|
79
|
+
<section class="notes">
|
|
80
|
+
|
|
81
|
+
## Notes
|
|
82
|
+
|
|
83
|
+
- The following is considered an anti-pattern:
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
var path = require( 'path' );
|
|
87
|
+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
|
|
88
|
+
|
|
89
|
+
var file = path.join( __dirname, 'foo.js' );
|
|
90
|
+
if ( exists.sync( __dirname ) ) {
|
|
91
|
+
file = readFileSync( file );
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Because time elapses between checking for existence and performing IO, at the time IO is performed, the path is no longer guaranteed to exist. In other words, a race condition exists between the process attempting to read and another process attempting to delete.
|
|
96
|
+
|
|
97
|
+
Instead, the following pattern is preferred, where `errors` are handled explicitly:
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
var path = require( 'path' );
|
|
101
|
+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
|
|
102
|
+
|
|
103
|
+
var file = path.join( __dirname, 'foo.js' );
|
|
104
|
+
try {
|
|
105
|
+
file = readFileSync( file );
|
|
106
|
+
} catch ( error ) {
|
|
107
|
+
console.log( 'unable to read file.' );
|
|
108
|
+
console.error( error );
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- Nevertheless, use cases exist where one desires to check existence **without** performing IO. For example,
|
|
113
|
+
|
|
114
|
+
<!-- run-disable -->
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
var path = require( 'path' );
|
|
118
|
+
var writeFileSync = require( '@stdlib/fs/write-file' ).sync;
|
|
119
|
+
|
|
120
|
+
var file = path.join( __dirname, 'foo.js' );
|
|
121
|
+
if ( exists.sync( file ) ) {
|
|
122
|
+
console.log( 'Don\'t overwrite the file!' );
|
|
123
|
+
} else {
|
|
124
|
+
writeFileSync( file, 'beep', {
|
|
125
|
+
'encoding': 'utf8'
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
</section>
|
|
131
|
+
|
|
132
|
+
<!-- /.notes -->
|
|
133
|
+
|
|
134
|
+
<section class="examples">
|
|
135
|
+
|
|
136
|
+
## Examples
|
|
137
|
+
|
|
138
|
+
<!-- eslint no-undef: "error" -->
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
var exists = require( '@stdlib/fs/exists' );
|
|
142
|
+
|
|
143
|
+
/* Sync */
|
|
144
|
+
|
|
145
|
+
console.log( exists.sync( __dirname ) );
|
|
146
|
+
// => true
|
|
147
|
+
|
|
148
|
+
console.log( exists.sync( 'beepboop' ) );
|
|
149
|
+
// => false
|
|
150
|
+
|
|
151
|
+
/* Async */
|
|
152
|
+
|
|
153
|
+
exists( __dirname, done );
|
|
154
|
+
exists( 'beepboop', done );
|
|
155
|
+
|
|
156
|
+
function done( error, bool ) {
|
|
157
|
+
if ( error ) {
|
|
158
|
+
console.error( error.message );
|
|
159
|
+
} else {
|
|
160
|
+
console.log( bool );
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
</section>
|
|
166
|
+
|
|
167
|
+
<!-- /.examples -->
|
|
168
|
+
|
|
169
|
+
* * *
|
|
170
|
+
|
|
171
|
+
<section class="cli">
|
|
172
|
+
|
|
173
|
+
## CLI
|
|
174
|
+
|
|
175
|
+
<section class="usage">
|
|
176
|
+
|
|
177
|
+
### Usage
|
|
178
|
+
|
|
179
|
+
```text
|
|
180
|
+
Usage: exists [options] <path>
|
|
181
|
+
|
|
182
|
+
Options:
|
|
183
|
+
|
|
184
|
+
-h, --help Print this message.
|
|
185
|
+
-V, --version Print the package version.
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
</section>
|
|
189
|
+
|
|
190
|
+
<!-- /.usage -->
|
|
191
|
+
|
|
192
|
+
<section class="notes">
|
|
193
|
+
|
|
194
|
+
### Notes
|
|
195
|
+
|
|
196
|
+
- Relative paths are resolved relative to the current working directory.
|
|
197
|
+
- Errors are written to `stderr`.
|
|
198
|
+
- Results are written to `stdout`.
|
|
199
|
+
|
|
200
|
+
</section>
|
|
201
|
+
|
|
202
|
+
<!-- /.notes -->
|
|
203
|
+
|
|
204
|
+
<section class="examples">
|
|
205
|
+
|
|
206
|
+
### Examples
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
$ exists ./../
|
|
210
|
+
true || <error_message>
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
</section>
|
|
214
|
+
|
|
215
|
+
<!-- /.examples -->
|
|
216
|
+
|
|
217
|
+
</section>
|
|
218
|
+
|
|
219
|
+
<!-- /.cli -->
|
|
220
|
+
|
|
221
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
222
|
+
|
|
223
|
+
<section class="related">
|
|
224
|
+
|
|
225
|
+
* * *
|
|
226
|
+
|
|
227
|
+
## See Also
|
|
228
|
+
|
|
229
|
+
- <span class="package-name">[`@stdlib/fs/read-file`][@stdlib/fs/read-file]</span><span class="delimiter">: </span><span class="description">read the entire contents of a file.</span>
|
|
230
|
+
- <span class="package-name">[`@stdlib/fs/read-dir`][@stdlib/fs/read-dir]</span><span class="delimiter">: </span><span class="description">read the entire contents of a directory.</span>
|
|
231
|
+
|
|
232
|
+
</section>
|
|
233
|
+
|
|
234
|
+
<!-- /.related -->
|
|
235
|
+
|
|
236
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
237
|
+
|
|
238
|
+
<section class="links">
|
|
239
|
+
|
|
240
|
+
[node-fs-exists]: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
|
|
241
|
+
|
|
242
|
+
<!-- <related-links> -->
|
|
243
|
+
|
|
244
|
+
[@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
|
|
245
|
+
|
|
246
|
+
[@stdlib/fs/read-dir]: https://github.com/stdlib-js/fs/tree/main/read-dir
|
|
247
|
+
|
|
248
|
+
<!-- </related-links> -->
|
|
249
|
+
|
|
250
|
+
</section>
|
|
251
|
+
|
|
252
|
+
<!-- /.links -->
|
package/exists/bin/cli
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
'use strict';
|
|
22
|
+
|
|
23
|
+
// MODULES //
|
|
24
|
+
|
|
25
|
+
var resolve = require( 'path' ).resolve;
|
|
26
|
+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
|
|
27
|
+
var CLI = require( '@stdlib/cli/ctor' );
|
|
28
|
+
var cwd = require( '@stdlib/process/cwd' );
|
|
29
|
+
var exists = require( './../lib' );
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
// MAIN //
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Main execution sequence.
|
|
36
|
+
*
|
|
37
|
+
* @private
|
|
38
|
+
* @returns {void}
|
|
39
|
+
*/
|
|
40
|
+
function main() {
|
|
41
|
+
var flags;
|
|
42
|
+
var tpath;
|
|
43
|
+
var args;
|
|
44
|
+
var cli;
|
|
45
|
+
|
|
46
|
+
// Create a command-line interface:
|
|
47
|
+
cli = new CLI({
|
|
48
|
+
'pkg': require( './../package.json' ),
|
|
49
|
+
'options': require( './../etc/cli_opts.json' ),
|
|
50
|
+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
|
|
51
|
+
'encoding': 'utf8'
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Get any provided command-line options:
|
|
56
|
+
flags = cli.flags();
|
|
57
|
+
if ( flags.help || flags.version ) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Get any provided command-line arguments:
|
|
62
|
+
args = cli.args();
|
|
63
|
+
|
|
64
|
+
tpath = resolve( cwd(), args[ 0 ] );
|
|
65
|
+
exists( tpath, onTest );
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Callback invoked upon testing whether a path exists on the filesystem.
|
|
69
|
+
*
|
|
70
|
+
* @private
|
|
71
|
+
* @param {(Error|null)} error - error object
|
|
72
|
+
* @param {boolean} bool - boolean indicating if a path exists
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*/
|
|
75
|
+
function onTest( error, bool ) {
|
|
76
|
+
if ( error ) {
|
|
77
|
+
return cli.error( error );
|
|
78
|
+
}
|
|
79
|
+
console.log( bool.toString() ); // eslint-disable-line no-console
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
main();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( path, clbk )
|
|
3
|
+
Asynchronously tests whether a path exists on the filesystem.
|
|
4
|
+
|
|
5
|
+
Parameters
|
|
6
|
+
----------
|
|
7
|
+
path: string|Buffer
|
|
8
|
+
Path to test.
|
|
9
|
+
|
|
10
|
+
clbk: Function
|
|
11
|
+
Callback to invoke after testing for path existence. A callback may
|
|
12
|
+
accept a single argument, a boolean indicating whether a path exists, or
|
|
13
|
+
two arguments, an error argument and a boolean, matching the error-first
|
|
14
|
+
callback convention used in most asynchronous Node.js APIs.
|
|
15
|
+
|
|
16
|
+
Examples
|
|
17
|
+
--------
|
|
18
|
+
> function done( error, bool ) { console.log( bool ); };
|
|
19
|
+
> {{alias}}( './beep/boop', done );
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
{{alias}}.sync( path )
|
|
23
|
+
Synchronously tests whether a path exists on the filesystem.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
path: string|Buffer
|
|
28
|
+
Path to test.
|
|
29
|
+
|
|
30
|
+
Returns
|
|
31
|
+
-------
|
|
32
|
+
bool: boolean
|
|
33
|
+
Boolean indicating whether the path exists.
|
|
34
|
+
|
|
35
|
+
Examples
|
|
36
|
+
--------
|
|
37
|
+
> var bool = {{alias}}.sync( './beep/boop' )
|
|
38
|
+
<boolean>
|
|
39
|
+
|
|
40
|
+
See Also
|
|
41
|
+
--------
|
|
42
|
+
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2020 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Callback invoked after testing for path existence.
|
|
23
|
+
*
|
|
24
|
+
* @param bool - boolean indicating whether a path exists
|
|
25
|
+
*/
|
|
26
|
+
type Unary = ( bool: boolean ) => void;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Callback invoked after testing for path existence.
|
|
30
|
+
*
|
|
31
|
+
* @param err - error argument
|
|
32
|
+
* @param bool - boolean indicating whether a path exists
|
|
33
|
+
*/
|
|
34
|
+
type Binary = ( err: Error | null, bool: boolean ) => void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Callback invoked after testing for path existence.
|
|
38
|
+
*
|
|
39
|
+
* @param err - error argument
|
|
40
|
+
* @param bool - boolean indicating whether a path exists
|
|
41
|
+
*/
|
|
42
|
+
type Callback = Unary | Binary;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Interface for testing whether a path exists on the filesystem.
|
|
46
|
+
*/
|
|
47
|
+
interface Exists {
|
|
48
|
+
/**
|
|
49
|
+
* Asynchronously tests whether a path exists on the filesystem.
|
|
50
|
+
*
|
|
51
|
+
* @param path - path to test
|
|
52
|
+
* @param clbk - callback to invoke after testing path existence
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* exists( __dirname, done );
|
|
56
|
+
*
|
|
57
|
+
* function done( error, bool ) {
|
|
58
|
+
* if ( error ) {
|
|
59
|
+
* console.error( error );
|
|
60
|
+
* }
|
|
61
|
+
* if ( bool ) {
|
|
62
|
+
* console.log( '...path exists.' );
|
|
63
|
+
* } else {
|
|
64
|
+
* console.log( '...path does not exist.' );
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
*/
|
|
68
|
+
( path: string, clbk: Callback ): void;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Synchronously tests whether a path exists on the filesystem.
|
|
72
|
+
*
|
|
73
|
+
* @param path - path to test
|
|
74
|
+
* @returns boolean indicating whether the path exists
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* var bool = exists.sync( __dirname );
|
|
78
|
+
* // returns <boolean>
|
|
79
|
+
*/
|
|
80
|
+
sync( path: string ): boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Asynchronously tests whether a path exists on the filesystem.
|
|
85
|
+
*
|
|
86
|
+
* @param path - path to test
|
|
87
|
+
* @param clbk - callback to invoke after testing path existence
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* exists( __dirname, done );
|
|
91
|
+
*
|
|
92
|
+
* function done( error, bool ) {
|
|
93
|
+
* if ( error ) {
|
|
94
|
+
* console.error( error );
|
|
95
|
+
* }
|
|
96
|
+
* if ( bool ) {
|
|
97
|
+
* console.log( '...path exists.' );
|
|
98
|
+
* } else {
|
|
99
|
+
* console.log( '...path does not exist.' );
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* var bool = exists.sync( __dirname );
|
|
105
|
+
* // returns <boolean>
|
|
106
|
+
*/
|
|
107
|
+
declare var exists: Exists;
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
// EXPORTS //
|
|
111
|
+
|
|
112
|
+
export = exists;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2020 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import exists = require( './index' );
|
|
20
|
+
|
|
21
|
+
const done = ( error: Error | null, bool: boolean ) => {
|
|
22
|
+
if ( error || ( bool !== true && bool !== false ) ) {
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// TESTS //
|
|
29
|
+
|
|
30
|
+
// The function does not have a return value...
|
|
31
|
+
{
|
|
32
|
+
exists( 'beepboop', done ); // $ExpectType void
|
|
33
|
+
exists( '/var/www/html', done ); // $ExpectType void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The compiler throws an error if the function is provided a first argument which is not a string...
|
|
37
|
+
{
|
|
38
|
+
exists( 1, done ); // $ExpectError
|
|
39
|
+
exists( false, done ); // $ExpectError
|
|
40
|
+
exists( true, done ); // $ExpectError
|
|
41
|
+
exists( null, done ); // $ExpectError
|
|
42
|
+
exists( undefined, done ); // $ExpectError
|
|
43
|
+
exists( [], done ); // $ExpectError
|
|
44
|
+
exists( {}, done ); // $ExpectError
|
|
45
|
+
exists( ( x: number ): number => x, done ); // $ExpectError
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The compiler throws an error if the function is provided a second argument which is not a function with the expected signature...
|
|
49
|
+
{
|
|
50
|
+
exists( 'beepboop', 1 ); // $ExpectError
|
|
51
|
+
exists( 'beepboop', false ); // $ExpectError
|
|
52
|
+
exists( 'beepboop', true ); // $ExpectError
|
|
53
|
+
exists( 'beepboop', null ); // $ExpectError
|
|
54
|
+
exists( 'beepboop', undefined ); // $ExpectError
|
|
55
|
+
exists( 'beepboop', [] ); // $ExpectError
|
|
56
|
+
exists( 'beepboop', {} ); // $ExpectError
|
|
57
|
+
exists( 'beepboop', ( x: number ): number => x ); // $ExpectError
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The compiler throws an error if the function is provided an unsupported number of arguments...
|
|
61
|
+
{
|
|
62
|
+
exists(); // $ExpectError
|
|
63
|
+
exists( 'C:\\foo\\bar\\baz' ); // $ExpectError
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Attached to main export is a `sync` method which returns a boolean...
|
|
67
|
+
{
|
|
68
|
+
exists.sync( '/var/www/html' ); // $ExpectType boolean
|
|
69
|
+
exists.sync( '/foo/bar/baz' ); // $ExpectType boolean
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The compiler throws an error if the `sync` method is provided a first argument which is not a string...
|
|
73
|
+
{
|
|
74
|
+
exists.sync( 1 ); // $ExpectError
|
|
75
|
+
exists.sync( false ); // $ExpectError
|
|
76
|
+
exists.sync( true ); // $ExpectError
|
|
77
|
+
exists.sync( null ); // $ExpectError
|
|
78
|
+
exists.sync( undefined ); // $ExpectError
|
|
79
|
+
exists.sync( [] ); // $ExpectError
|
|
80
|
+
exists.sync( {} ); // $ExpectError
|
|
81
|
+
exists.sync( ( x: number ): number => x ); // $ExpectError
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
|
|
85
|
+
{
|
|
86
|
+
exists.sync(); // $ExpectError
|
|
87
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var fs = require( 'fs' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// FUNCTIONS //
|
|
27
|
+
|
|
28
|
+
var fcn;
|
|
29
|
+
if ( typeof fs.access === 'function' ) {
|
|
30
|
+
fcn = fs.access;
|
|
31
|
+
} else {
|
|
32
|
+
fcn = fs.stat;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// MAIN //
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Asynchronously tests whether a path exists on the filesystem.
|
|
40
|
+
*
|
|
41
|
+
* @param {(string|Buffer)} path - path to test
|
|
42
|
+
* @param {Function} clbk - callback to invoke after testing path existence
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* exists( __dirname, done );
|
|
46
|
+
*
|
|
47
|
+
* function done( error, bool ) {
|
|
48
|
+
* if ( error ) {
|
|
49
|
+
* console.error( error );
|
|
50
|
+
* }
|
|
51
|
+
* if ( bool ) {
|
|
52
|
+
* console.log( '...path exists.' );
|
|
53
|
+
* } else {
|
|
54
|
+
* console.log( '...path does not exist.' );
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
*/
|
|
58
|
+
function exists( path, clbk ) {
|
|
59
|
+
fcn( path, done );
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Callback invoked upon performing a filesystem call.
|
|
63
|
+
*
|
|
64
|
+
* @private
|
|
65
|
+
* @param {(Error|null)} error - error object
|
|
66
|
+
* @returns {void}
|
|
67
|
+
*/
|
|
68
|
+
function done( error ) {
|
|
69
|
+
if ( clbk.length === 2 ) {
|
|
70
|
+
if ( error ) {
|
|
71
|
+
return clbk( error, false );
|
|
72
|
+
}
|
|
73
|
+
return clbk( null, true );
|
|
74
|
+
}
|
|
75
|
+
if ( error ) {
|
|
76
|
+
return clbk( false );
|
|
77
|
+
}
|
|
78
|
+
return clbk( true );
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
// EXPORTS //
|
|
84
|
+
|
|
85
|
+
module.exports = exists;
|