@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,202 @@
|
|
|
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 logger = require( 'debug' );
|
|
24
|
+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
|
|
25
|
+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
|
|
26
|
+
var isFunction = require( '@stdlib/assert/is-function' );
|
|
27
|
+
var assign = require( '@stdlib/object/assign' );
|
|
28
|
+
var readFile = require( './../../read-file' );
|
|
29
|
+
var format = require( '@stdlib/string/format' );
|
|
30
|
+
var config = require( './config.json' );
|
|
31
|
+
var delay = require( './delay.js' );
|
|
32
|
+
var clearPending = require( './clear_pending.js' );
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// VARIABLES //
|
|
36
|
+
|
|
37
|
+
var debug = logger( 'read-file-list:async' );
|
|
38
|
+
var MAX_RETRIES = config.max_retries;
|
|
39
|
+
var MAX_DELAY = config.max_delay;
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
// MAIN //
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Asynchronously reads the entire contents of each file in a file list.
|
|
46
|
+
*
|
|
47
|
+
* @param {StringArray} list - list of file paths
|
|
48
|
+
* @param {(Object|string)} [options] - options
|
|
49
|
+
* @param {(string|null)} [options.encoding] - file encoding
|
|
50
|
+
* @param {string} [options.flag] - file status flag
|
|
51
|
+
* @param {Function} clbk - callback to invoke after reading file contents
|
|
52
|
+
* @throws {TypeError} first argument must be an array of strings
|
|
53
|
+
* @throws {TypeError} callback argument must be a function
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* var list = [ __filename ];
|
|
57
|
+
*
|
|
58
|
+
* readFileList( list, onFiles );
|
|
59
|
+
*
|
|
60
|
+
* function onFiles( error, files ) {
|
|
61
|
+
* if ( error ) {
|
|
62
|
+
* throw error;
|
|
63
|
+
* }
|
|
64
|
+
* console.dir( files );
|
|
65
|
+
* }
|
|
66
|
+
*/
|
|
67
|
+
function readFileList( list, options, clbk ) {
|
|
68
|
+
var pending;
|
|
69
|
+
var results;
|
|
70
|
+
var errFLG;
|
|
71
|
+
var count;
|
|
72
|
+
var opts;
|
|
73
|
+
var len;
|
|
74
|
+
var cb;
|
|
75
|
+
var i;
|
|
76
|
+
|
|
77
|
+
if ( !isStringArray( list ) ) {
|
|
78
|
+
throw new TypeError( format( 'invalid argument. First argument must be an array of strings. Value: `%s`.', list ) );
|
|
79
|
+
}
|
|
80
|
+
if ( arguments.length < 3 ) {
|
|
81
|
+
opts = {};
|
|
82
|
+
cb = options;
|
|
83
|
+
} else {
|
|
84
|
+
if ( isString( options ) ) {
|
|
85
|
+
opts = options;
|
|
86
|
+
} else {
|
|
87
|
+
opts = assign( {}, options );
|
|
88
|
+
}
|
|
89
|
+
cb = clbk;
|
|
90
|
+
}
|
|
91
|
+
if ( !isFunction( cb ) ) {
|
|
92
|
+
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) );
|
|
93
|
+
}
|
|
94
|
+
len = list.length;
|
|
95
|
+
|
|
96
|
+
results = new Array( len );
|
|
97
|
+
pending = {};
|
|
98
|
+
count = 0;
|
|
99
|
+
|
|
100
|
+
debug( 'Reading %d files...', len );
|
|
101
|
+
for ( i = 0; i < len; i++ ) {
|
|
102
|
+
debug( 'Reading file: %s (%d of %d).', list[ i ], i+1, len );
|
|
103
|
+
readFile( list[ i ], opts, getCallback( i ) );
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Returns a callback to be invoked upon reading a file.
|
|
108
|
+
*
|
|
109
|
+
* @private
|
|
110
|
+
* @param {NonNegativeInteger} idx - index
|
|
111
|
+
* @returns {Callback} callback
|
|
112
|
+
*/
|
|
113
|
+
function getCallback( idx ) {
|
|
114
|
+
var retries;
|
|
115
|
+
var file;
|
|
116
|
+
var k;
|
|
117
|
+
|
|
118
|
+
file = list[ idx ];
|
|
119
|
+
k = idx + 1;
|
|
120
|
+
retries = 0;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Retries reading a file.
|
|
124
|
+
*
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
function retry() {
|
|
128
|
+
delete pending[ idx ];
|
|
129
|
+
debug( 'Reading file: %s (%d of %d). Retry: %d of %d.', file, k, len, retries, MAX_RETRIES );
|
|
130
|
+
readFile( file, opts, onRead );
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Callback to be invoked upon reading a file.
|
|
135
|
+
*
|
|
136
|
+
* @private
|
|
137
|
+
* @param {(Error|null)} error - error object
|
|
138
|
+
* @param {(Buffer|string)} data - file data
|
|
139
|
+
* @returns {void}
|
|
140
|
+
*/
|
|
141
|
+
function onRead( error, data ) {
|
|
142
|
+
var d;
|
|
143
|
+
if ( errFLG ) {
|
|
144
|
+
debug( 'An error has already been returned. Discarding data for file: %s (%d of %d).', file, k, len );
|
|
145
|
+
return; // prevents `done()` from being called more than once
|
|
146
|
+
}
|
|
147
|
+
if ( error ) {
|
|
148
|
+
debug( 'Encountered an error when reading %s (%d of %d). Error: %s', file, k, len, error.message );
|
|
149
|
+
if (
|
|
150
|
+
error.code === 'EMFILE' || // current process
|
|
151
|
+
error.code === 'ENFILE' // across entire system
|
|
152
|
+
) {
|
|
153
|
+
retries += 1;
|
|
154
|
+
if ( retries > MAX_RETRIES ) {
|
|
155
|
+
debug( 'Maximum number of retries exceeded. Too many open files.' );
|
|
156
|
+
error = new Error( 'unexpected error. Max retries exceeded. Too many open files.' );
|
|
157
|
+
return done( error );
|
|
158
|
+
}
|
|
159
|
+
d = delay( retries, MAX_DELAY );
|
|
160
|
+
debug( 'Too many open files. Will retry reading file %d of %d in %s seconds.', k, len, d/1000 );
|
|
161
|
+
pending[ idx ] = setTimeout( retry, d );
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
return done( error );
|
|
165
|
+
}
|
|
166
|
+
debug( 'Successfully read file: %s (%d of %d).', file, k, len );
|
|
167
|
+
results[ idx ] = {
|
|
168
|
+
'file': file,
|
|
169
|
+
'data': data
|
|
170
|
+
};
|
|
171
|
+
count += 1;
|
|
172
|
+
debug( 'Read %d of %d files.', count, len );
|
|
173
|
+
if ( count === len ) {
|
|
174
|
+
return done();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return onRead;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Callback invoked upon completion.
|
|
183
|
+
*
|
|
184
|
+
* @private
|
|
185
|
+
* @param {Error} [error] - error object
|
|
186
|
+
* @returns {void}
|
|
187
|
+
*/
|
|
188
|
+
function done( error ) {
|
|
189
|
+
clearPending( pending );
|
|
190
|
+
if ( error ) {
|
|
191
|
+
errFLG = true;
|
|
192
|
+
return cb( error );
|
|
193
|
+
}
|
|
194
|
+
debug( 'Successfully read all files.' );
|
|
195
|
+
cb( null, results );
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
// EXPORTS //
|
|
201
|
+
|
|
202
|
+
module.exports = readFileList;
|
|
@@ -0,0 +1,47 @@
|
|
|
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 objectKeys = require( '@ohos-ports/stdlib-utils/keys' );
|
|
24
|
+
var cancelTimeout = require( './clear_timeout.js' );
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// MAIN //
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Clears pending timeouts.
|
|
31
|
+
*
|
|
32
|
+
* @private
|
|
33
|
+
* @param {Object} pending - hash containing pending timeout ids
|
|
34
|
+
*/
|
|
35
|
+
function clearPending( pending ) {
|
|
36
|
+
var keys;
|
|
37
|
+
var i;
|
|
38
|
+
keys = objectKeys( pending );
|
|
39
|
+
for ( i = 0; i < keys.length; i++ ) {
|
|
40
|
+
cancelTimeout( pending[ keys[i] ] );
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// EXPORTS //
|
|
46
|
+
|
|
47
|
+
module.exports = clearPending;
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
// EXPORTS //
|
|
22
|
+
|
|
23
|
+
module.exports = clearTimeout;
|
|
@@ -0,0 +1,47 @@
|
|
|
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 pow = require( '@stdlib/math/base/special/pow' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Computes an exponential delay.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {NonNegativeInteger} retries - number of retries
|
|
33
|
+
* @param {number} max - maximum delay
|
|
34
|
+
* @returns {NonNegativeInteger} exponential delay
|
|
35
|
+
*/
|
|
36
|
+
function delay( retries, max ) {
|
|
37
|
+
var d = pow( 2, retries ) - 1;
|
|
38
|
+
if ( d > max ) {
|
|
39
|
+
d = max;
|
|
40
|
+
}
|
|
41
|
+
return d;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// EXPORTS //
|
|
46
|
+
|
|
47
|
+
module.exports = delay;
|
|
@@ -0,0 +1,66 @@
|
|
|
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 entire contents of each file in a file list.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/fs/read-file-list
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var readFileList = require( '@stdlib/fs/read-file-list' );
|
|
28
|
+
*
|
|
29
|
+
* var list = [ __filename ];
|
|
30
|
+
*
|
|
31
|
+
* readFileList( list, onFiles );
|
|
32
|
+
*
|
|
33
|
+
* function onFiles( error, files ) {
|
|
34
|
+
* if ( error ) {
|
|
35
|
+
* throw error;
|
|
36
|
+
* }
|
|
37
|
+
* console.dir( files );
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var readFileList = require( '@stdlib/fs/read-file-list' ).sync;
|
|
42
|
+
*
|
|
43
|
+
* var list = [ __filename ];
|
|
44
|
+
* var files = readFileList( list );
|
|
45
|
+
*
|
|
46
|
+
* if ( files instanceof Error ) {
|
|
47
|
+
* throw files;
|
|
48
|
+
* }
|
|
49
|
+
* console.dir( files );
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
// MODULES //
|
|
53
|
+
|
|
54
|
+
var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
|
|
55
|
+
var readFileList = require( './async.js' );
|
|
56
|
+
var sync = require( './sync.js' );
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
// MAIN //
|
|
60
|
+
|
|
61
|
+
setReadOnly( readFileList, 'sync', sync );
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
// EXPORTS //
|
|
65
|
+
|
|
66
|
+
module.exports = readFileList;
|
|
@@ -0,0 +1,94 @@
|
|
|
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 logger = require( 'debug' );
|
|
24
|
+
var readFile = require( './../../read-file' ).sync;
|
|
25
|
+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
|
|
26
|
+
var format = require( '@stdlib/string/format' );
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// VARIABLES //
|
|
30
|
+
|
|
31
|
+
var debug = logger( 'read-file-list:sync' );
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// MAIN //
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Synchronously reads the entire contents of each file in a file list.
|
|
38
|
+
*
|
|
39
|
+
* @param {StringArray} list - list of file paths
|
|
40
|
+
* @param {(Object|string)} [options] - options
|
|
41
|
+
* @param {(string|null)} [options.encoding] - file encoding
|
|
42
|
+
* @param {string} [options.flag] - file status flag
|
|
43
|
+
* @throws {TypeError} must provide an array of strings
|
|
44
|
+
* @returns {ObjectArray} file contents
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* var list = [ __filename ];
|
|
48
|
+
* var files = readFileListSync( list );
|
|
49
|
+
*
|
|
50
|
+
* if ( files instanceof Error ) {
|
|
51
|
+
* throw files;
|
|
52
|
+
* }
|
|
53
|
+
* console.dir( files );
|
|
54
|
+
*/
|
|
55
|
+
function readFileListSync( list, options ) {
|
|
56
|
+
var results;
|
|
57
|
+
var opts;
|
|
58
|
+
var file;
|
|
59
|
+
var len;
|
|
60
|
+
var i;
|
|
61
|
+
|
|
62
|
+
if ( !isStringArray( list ) ) {
|
|
63
|
+
throw new TypeError( format( 'invalid argument. First argument must be an array of strings. Value: `%s`.', list ) );
|
|
64
|
+
}
|
|
65
|
+
if ( arguments.length > 1 ) {
|
|
66
|
+
opts = options;
|
|
67
|
+
} else {
|
|
68
|
+
opts = {};
|
|
69
|
+
}
|
|
70
|
+
len = list.length;
|
|
71
|
+
results = new Array( len );
|
|
72
|
+
|
|
73
|
+
debug( 'Reading %d files...', len );
|
|
74
|
+
for ( i = 0; i < len; i++ ) {
|
|
75
|
+
debug( 'Reading file: %s (%d of %d).', list[ i ], i+1, len );
|
|
76
|
+
file = readFile( list[ i ], opts );
|
|
77
|
+
if ( file instanceof Error ) {
|
|
78
|
+
debug( 'Encountered an error when reading file: %s (%d of %d). Error: %s', list[ i ], i, len, file.message );
|
|
79
|
+
return file;
|
|
80
|
+
}
|
|
81
|
+
debug( 'Successfully read file: %s (%d of %d).', list[ i ], i, len );
|
|
82
|
+
results[ i ] = {
|
|
83
|
+
'file': list[ i ],
|
|
84
|
+
'data': file
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
debug( 'Finished reading files.' );
|
|
88
|
+
return results;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
// EXPORTS //
|
|
93
|
+
|
|
94
|
+
module.exports = readFileListSync;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/fs/read-file-list",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Read the entire contents of each file in a file list.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"bin": {
|
|
17
|
+
"read-file-list": "./bin/cli"
|
|
18
|
+
},
|
|
19
|
+
"main": "./lib",
|
|
20
|
+
"directories": {
|
|
21
|
+
"benchmark": "./benchmark",
|
|
22
|
+
"doc": "./docs",
|
|
23
|
+
"example": "./examples",
|
|
24
|
+
"lib": "./lib",
|
|
25
|
+
"test": "./test"
|
|
26
|
+
},
|
|
27
|
+
"types": "./docs/types",
|
|
28
|
+
"scripts": {},
|
|
29
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {},
|
|
38
|
+
"devDependencies": {},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=0.10.0",
|
|
41
|
+
"npm": ">2.7.0"
|
|
42
|
+
},
|
|
43
|
+
"os": [
|
|
44
|
+
"aix",
|
|
45
|
+
"darwin",
|
|
46
|
+
"freebsd",
|
|
47
|
+
"linux",
|
|
48
|
+
"macos",
|
|
49
|
+
"openbsd",
|
|
50
|
+
"sunos",
|
|
51
|
+
"win32",
|
|
52
|
+
"windows"
|
|
53
|
+
],
|
|
54
|
+
"keywords": [
|
|
55
|
+
"stdlib",
|
|
56
|
+
"stdfs",
|
|
57
|
+
"fs",
|
|
58
|
+
"readfile",
|
|
59
|
+
"readfilesync",
|
|
60
|
+
"path",
|
|
61
|
+
"async",
|
|
62
|
+
"sync",
|
|
63
|
+
"file",
|
|
64
|
+
"read",
|
|
65
|
+
"open",
|
|
66
|
+
"filesystem",
|
|
67
|
+
"list",
|
|
68
|
+
"multiple",
|
|
69
|
+
"concurrent",
|
|
70
|
+
"parallel"
|
|
71
|
+
]
|
|
72
|
+
}
|