@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
|
@@ -0,0 +1,232 @@
|
|
|
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
|
+
# Read Directory
|
|
22
|
+
|
|
23
|
+
> Read the contents of a directory.
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var readDir = require( '@stdlib/fs/read-dir' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### readDir( path, clbk )
|
|
34
|
+
|
|
35
|
+
Asynchronously reads the contents of a directory.
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
readDir( __dirname, onRead );
|
|
39
|
+
|
|
40
|
+
function onRead( error, data ) {
|
|
41
|
+
if ( error ) {
|
|
42
|
+
console.error( error );
|
|
43
|
+
} else {
|
|
44
|
+
console.log( data );
|
|
45
|
+
// => [...]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### readDir.sync( path )
|
|
51
|
+
|
|
52
|
+
Synchronously reads the contents of a directory.
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
var out = readDir.sync( __dirname );
|
|
56
|
+
if ( out instanceof Error ) {
|
|
57
|
+
throw out;
|
|
58
|
+
}
|
|
59
|
+
console.log( out );
|
|
60
|
+
// => [...]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
</section>
|
|
64
|
+
|
|
65
|
+
<!-- /.usage -->
|
|
66
|
+
|
|
67
|
+
<section class="notes">
|
|
68
|
+
|
|
69
|
+
## Notes
|
|
70
|
+
|
|
71
|
+
- The difference between this module and [`fs.readdirSync()`][fs] is that [`fs.readdirSync()`][fs] will throw if an `error` is encountered (e.g., if given a non-existent `path`) and this module will return an `error`. Hence, the following anti-pattern
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
var fs = require( 'fs' );
|
|
75
|
+
|
|
76
|
+
var dir = '/path/to/dir';
|
|
77
|
+
|
|
78
|
+
// Check for existence to prevent an error being thrown...
|
|
79
|
+
if ( fs.existsSync( dir ) ) {
|
|
80
|
+
dir = fs.readdirSync( dir );
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
can be replaced by an approach which addresses existence via `error` handling.
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
var readDir = require( '@stdlib/fs/read-dir' );
|
|
88
|
+
|
|
89
|
+
var dir = '/path/to/dir';
|
|
90
|
+
|
|
91
|
+
// Explicitly handle the error...
|
|
92
|
+
dir = readDir.sync( dir );
|
|
93
|
+
if ( dir instanceof Error ) {
|
|
94
|
+
// You choose what to do...
|
|
95
|
+
throw dir;
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
</section>
|
|
100
|
+
|
|
101
|
+
<!-- /.notes -->
|
|
102
|
+
|
|
103
|
+
<section class="examples">
|
|
104
|
+
|
|
105
|
+
## Examples
|
|
106
|
+
|
|
107
|
+
<!-- eslint no-undef: "error" -->
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
var readDir = require( '@stdlib/fs/read-dir' );
|
|
111
|
+
|
|
112
|
+
/* Sync */
|
|
113
|
+
|
|
114
|
+
var out = readDir.sync( __dirname );
|
|
115
|
+
// returns <Array>
|
|
116
|
+
|
|
117
|
+
console.log( out instanceof Error );
|
|
118
|
+
// => false
|
|
119
|
+
|
|
120
|
+
out = readDir.sync( 'beepboop' );
|
|
121
|
+
// returns <Error>
|
|
122
|
+
|
|
123
|
+
console.log( out instanceof Error );
|
|
124
|
+
// => true
|
|
125
|
+
|
|
126
|
+
/* Async */
|
|
127
|
+
|
|
128
|
+
readDir( __dirname, onRead );
|
|
129
|
+
readDir( 'beepboop', onRead );
|
|
130
|
+
|
|
131
|
+
function onRead( error, data ) {
|
|
132
|
+
if ( error ) {
|
|
133
|
+
if ( error.code === 'ENOENT' ) {
|
|
134
|
+
console.error( 'Directory does not exist.' );
|
|
135
|
+
} else {
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
console.log( data );
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
</section>
|
|
145
|
+
|
|
146
|
+
<!-- /.examples -->
|
|
147
|
+
|
|
148
|
+
* * *
|
|
149
|
+
|
|
150
|
+
<section class="cli">
|
|
151
|
+
|
|
152
|
+
## CLI
|
|
153
|
+
|
|
154
|
+
<section class="usage">
|
|
155
|
+
|
|
156
|
+
### Usage
|
|
157
|
+
|
|
158
|
+
```text
|
|
159
|
+
Usage: read-dir [options] <dirpath>
|
|
160
|
+
|
|
161
|
+
Options:
|
|
162
|
+
|
|
163
|
+
-h, --help Print this message.
|
|
164
|
+
-V, --version Print the package version.
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
</section>
|
|
168
|
+
|
|
169
|
+
<!-- /.usage -->
|
|
170
|
+
|
|
171
|
+
<section class="notes">
|
|
172
|
+
|
|
173
|
+
### Notes
|
|
174
|
+
|
|
175
|
+
- Relative paths are resolved relative to the current working directory.
|
|
176
|
+
- Errors are written to `stderr`.
|
|
177
|
+
- Directory contents are written to `stdout`.
|
|
178
|
+
|
|
179
|
+
</section>
|
|
180
|
+
|
|
181
|
+
<!-- /.notes -->
|
|
182
|
+
|
|
183
|
+
<section class="examples">
|
|
184
|
+
|
|
185
|
+
### Examples
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
$ read-dir ./../
|
|
189
|
+
...
|
|
190
|
+
...
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
</section>
|
|
194
|
+
|
|
195
|
+
<!-- /.examples -->
|
|
196
|
+
|
|
197
|
+
</section>
|
|
198
|
+
|
|
199
|
+
<!-- /.cli -->
|
|
200
|
+
|
|
201
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
202
|
+
|
|
203
|
+
<section class="related">
|
|
204
|
+
|
|
205
|
+
* * *
|
|
206
|
+
|
|
207
|
+
## See Also
|
|
208
|
+
|
|
209
|
+
- <span class="package-name">[`@stdlib/fs/exists`][@stdlib/fs/exists]</span><span class="delimiter">: </span><span class="description">test whether a path exists on the filesystem.</span>
|
|
210
|
+
- <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>
|
|
211
|
+
|
|
212
|
+
</section>
|
|
213
|
+
|
|
214
|
+
<!-- /.related -->
|
|
215
|
+
|
|
216
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
217
|
+
|
|
218
|
+
<section class="links">
|
|
219
|
+
|
|
220
|
+
[fs]: https://nodejs.org/api/fs.html
|
|
221
|
+
|
|
222
|
+
<!-- <related-links> -->
|
|
223
|
+
|
|
224
|
+
[@stdlib/fs/exists]: https://github.com/stdlib-js/fs/tree/main/exists
|
|
225
|
+
|
|
226
|
+
[@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
|
|
227
|
+
|
|
228
|
+
<!-- </related-links> -->
|
|
229
|
+
|
|
230
|
+
</section>
|
|
231
|
+
|
|
232
|
+
<!-- /.links -->
|
package/read-dir/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 readDir = 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 dpath;
|
|
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
|
+
dpath = resolve( cwd(), args[ 0 ] );
|
|
65
|
+
readDir( dpath, onRead );
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Callback invoked upon reading the contents of a directory.
|
|
69
|
+
*
|
|
70
|
+
* @private
|
|
71
|
+
* @param {(Error|null)} error - error object
|
|
72
|
+
* @param {string[]} data - directory contents
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*/
|
|
75
|
+
function onRead( error, data ) {
|
|
76
|
+
if ( error ) {
|
|
77
|
+
return cli.error( error );
|
|
78
|
+
}
|
|
79
|
+
console.log( data.join( '\n' ) ); // eslint-disable-line no-console
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
main();
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( path, clbk )
|
|
3
|
+
Asynchronously reads the contents of a directory.
|
|
4
|
+
|
|
5
|
+
Parameters
|
|
6
|
+
----------
|
|
7
|
+
path: string|Buffer
|
|
8
|
+
Directory path.
|
|
9
|
+
|
|
10
|
+
clbk: Function
|
|
11
|
+
Callback to invoke after reading directory contents.
|
|
12
|
+
|
|
13
|
+
Examples
|
|
14
|
+
--------
|
|
15
|
+
> function onRead( error, data ) {
|
|
16
|
+
... if ( error ) {
|
|
17
|
+
... console.error( error.message );
|
|
18
|
+
... } else {
|
|
19
|
+
... console.log( data );
|
|
20
|
+
... }
|
|
21
|
+
... };
|
|
22
|
+
> {{alias}}( './beep/boop', onRead );
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
{{alias}}.sync( path )
|
|
26
|
+
Synchronously reads the contents of a directory.
|
|
27
|
+
|
|
28
|
+
Parameters
|
|
29
|
+
----------
|
|
30
|
+
path: string|Buffer
|
|
31
|
+
Directory path.
|
|
32
|
+
|
|
33
|
+
Returns
|
|
34
|
+
-------
|
|
35
|
+
out: Error|Array|Array<string>
|
|
36
|
+
Directory contents.
|
|
37
|
+
|
|
38
|
+
Examples
|
|
39
|
+
--------
|
|
40
|
+
> var out = {{alias}}.sync( './beep/boop' );
|
|
41
|
+
|
|
42
|
+
See Also
|
|
43
|
+
--------
|
|
44
|
+
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
/// <reference types="node"/>
|
|
22
|
+
|
|
23
|
+
import { Buffer } from 'buffer';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Callback invoked after reading the contents of a directory.
|
|
27
|
+
*
|
|
28
|
+
* @param err - error argument
|
|
29
|
+
* @param data - directory contents
|
|
30
|
+
*/
|
|
31
|
+
type Callback = ( err: Error | null, data: Array<string> ) => void;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Interface for reading the contents of a directory.
|
|
35
|
+
*/
|
|
36
|
+
interface ReadDir {
|
|
37
|
+
/**
|
|
38
|
+
* Asynchronously reads the contents of a directory.
|
|
39
|
+
*
|
|
40
|
+
* @param path - directory path
|
|
41
|
+
* @param clbk - callback to invoke after reading directory contents
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* function onRead( error, data ) {
|
|
45
|
+
* if ( error ) {
|
|
46
|
+
* console.error( error );
|
|
47
|
+
* } else {
|
|
48
|
+
* console.log( data );
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* readDir( __dirname, onRead );
|
|
52
|
+
*/
|
|
53
|
+
( path: string | Buffer, clbk: Callback ): void;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Synchronously reads the contents of a directory.
|
|
57
|
+
*
|
|
58
|
+
* @param path - directory path
|
|
59
|
+
* @returns directory contents or an error
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* var out = readDir.sync( __dirname );
|
|
63
|
+
* if ( out instanceof Error ) {
|
|
64
|
+
* throw out;
|
|
65
|
+
* }
|
|
66
|
+
* console.log( out );
|
|
67
|
+
*/
|
|
68
|
+
sync( path: string | Buffer ): Array<string> | Error;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Asynchronously reads the contents of a directory.
|
|
73
|
+
*
|
|
74
|
+
* @param path - directory path
|
|
75
|
+
* @param clbk - callback to invoke after reading directory contents
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* function onRead( error, data ) {
|
|
79
|
+
* if ( error ) {
|
|
80
|
+
* console.error( error );
|
|
81
|
+
* } else {
|
|
82
|
+
* console.log( data );
|
|
83
|
+
* }
|
|
84
|
+
* }
|
|
85
|
+
* readDir( __dirname, onRead );
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* var out = readDir.sync( __dirname );
|
|
89
|
+
* if ( out instanceof Error ) {
|
|
90
|
+
* throw out;
|
|
91
|
+
* }
|
|
92
|
+
* console.log( out );
|
|
93
|
+
*/
|
|
94
|
+
declare var readDir: ReadDir;
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
// EXPORTS //
|
|
98
|
+
|
|
99
|
+
export = readDir;
|
|
@@ -0,0 +1,85 @@
|
|
|
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 readDir = require( './index' );
|
|
20
|
+
|
|
21
|
+
const done = ( error: Error | null, data: Array<string> ) => {
|
|
22
|
+
if ( error || !data.length ) {
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// TESTS //
|
|
29
|
+
|
|
30
|
+
// The function does not have a return value...
|
|
31
|
+
{
|
|
32
|
+
readDir( 'beepboop', done ); // $ExpectType void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The compiler throws an error if the function is provided a first argument which is not a string...
|
|
36
|
+
{
|
|
37
|
+
readDir( 123, done ); // $ExpectError
|
|
38
|
+
readDir( false, done ); // $ExpectError
|
|
39
|
+
readDir( true, done ); // $ExpectError
|
|
40
|
+
readDir( null, done ); // $ExpectError
|
|
41
|
+
readDir( undefined, done ); // $ExpectError
|
|
42
|
+
readDir( [], done ); // $ExpectError
|
|
43
|
+
readDir( {}, done ); // $ExpectError
|
|
44
|
+
readDir( ( x: number ): number => x, done ); // $ExpectError
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The compiler throws an error if the function is provided a second argument which is not a function with the expected signature...
|
|
48
|
+
{
|
|
49
|
+
readDir( '/var/log/', 1 ); // $ExpectError
|
|
50
|
+
readDir( '/var/log/', false ); // $ExpectError
|
|
51
|
+
readDir( '/var/log/', true ); // $ExpectError
|
|
52
|
+
readDir( '/var/log/', null ); // $ExpectError
|
|
53
|
+
readDir( '/var/log/', undefined ); // $ExpectError
|
|
54
|
+
readDir( '/var/log/', [] ); // $ExpectError
|
|
55
|
+
readDir( '/var/log/', {} ); // $ExpectError
|
|
56
|
+
readDir( '/var/log/', ( x: number ): number => x ); // $ExpectError
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The compiler throws an error if the function is provided an unsupported number of arguments...
|
|
60
|
+
{
|
|
61
|
+
readDir(); // $ExpectError
|
|
62
|
+
readDir( 'C:\\foo\\bar\\baz' ); // $ExpectError
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Attached to main export is a `sync` method which returns a string array...
|
|
66
|
+
{
|
|
67
|
+
readDir.sync( 'beepboop' ); // $ExpectType Error | string[]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The compiler throws an error if the `sync` method is provided a first argument which is not a string...
|
|
71
|
+
{
|
|
72
|
+
readDir.sync( 123 ); // $ExpectError
|
|
73
|
+
readDir.sync( false ); // $ExpectError
|
|
74
|
+
readDir.sync( true ); // $ExpectError
|
|
75
|
+
readDir.sync( null ); // $ExpectError
|
|
76
|
+
readDir.sync( undefined ); // $ExpectError
|
|
77
|
+
readDir.sync( [] ); // $ExpectError
|
|
78
|
+
readDir.sync( {} ); // $ExpectError
|
|
79
|
+
readDir.sync( ( x: number ): number => x ); // $ExpectError
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
|
|
83
|
+
{
|
|
84
|
+
readDir.sync(); // $ExpectError
|
|
85
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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 readdir = require( 'fs' ).readdir;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Asynchronously reads the contents of a directory.
|
|
30
|
+
*
|
|
31
|
+
* @param {(string|Buffer)} path - directory path
|
|
32
|
+
* @param {Function} clbk - callback to invoke after reading directory contents
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* function onRead( error, data ) {
|
|
36
|
+
* if ( error ) {
|
|
37
|
+
* console.error( error );
|
|
38
|
+
* } else {
|
|
39
|
+
* console.log( data );
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
* readDir( __dirname, onRead );
|
|
43
|
+
*/
|
|
44
|
+
function readDir() {
|
|
45
|
+
var args;
|
|
46
|
+
var i;
|
|
47
|
+
args = [];
|
|
48
|
+
for ( i = 0; i < arguments.length; i++ ) {
|
|
49
|
+
args.push( arguments[ i ] );
|
|
50
|
+
}
|
|
51
|
+
readdir.apply( null, args );
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
// EXPORTS //
|
|
56
|
+
|
|
57
|
+
module.exports = readDir;
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
/**
|
|
22
|
+
* Read the contents of a directory.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/fs/read-dir
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var readDir = require( '@stdlib/fs/read-dir' );
|
|
28
|
+
*
|
|
29
|
+
* function onRead( error, data ) {
|
|
30
|
+
* if ( error ) {
|
|
31
|
+
* console.error( error );
|
|
32
|
+
* } else {
|
|
33
|
+
* console.log( data );
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* readDir( __dirname, onRead );
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* var readDirSync = require( '@stdlib/fs/read-dir' ).sync;
|
|
40
|
+
*
|
|
41
|
+
* var out = readDirSync( __dirname );
|
|
42
|
+
* if ( out instanceof Error ) {
|
|
43
|
+
* throw out;
|
|
44
|
+
* }
|
|
45
|
+
* console.log( out );
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
// MODULES //
|
|
49
|
+
|
|
50
|
+
var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
|
|
51
|
+
var readDir = require( './async.js' );
|
|
52
|
+
var sync = require( './sync.js' );
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
// MAIN //
|
|
56
|
+
|
|
57
|
+
setReadOnly( readDir, 'sync', sync );
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
// EXPORTS //
|
|
61
|
+
|
|
62
|
+
module.exports = readDir;
|