@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,147 @@
|
|
|
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 readFile = require( './index' );
|
|
20
|
+
|
|
21
|
+
const onLoad = ( error: Error | null, file: string | Buffer ) => {
|
|
22
|
+
if ( error || !file ) {
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// TESTS //
|
|
29
|
+
|
|
30
|
+
// The function does not have a return value...
|
|
31
|
+
{
|
|
32
|
+
readFile( 'beepboop', onLoad ); // $ExpectType void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor...
|
|
36
|
+
{
|
|
37
|
+
readFile( false, onLoad ); // $ExpectError
|
|
38
|
+
readFile( true, onLoad ); // $ExpectError
|
|
39
|
+
readFile( null, onLoad ); // $ExpectError
|
|
40
|
+
readFile( undefined, onLoad ); // $ExpectError
|
|
41
|
+
readFile( [], onLoad ); // $ExpectError
|
|
42
|
+
readFile( {}, onLoad ); // $ExpectError
|
|
43
|
+
readFile( ( x: number ): number => x, onLoad ); // $ExpectError
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature...
|
|
47
|
+
{
|
|
48
|
+
readFile( '/path/to/beepboop', 'abc' ); // $ExpectError
|
|
49
|
+
readFile( '/path/to/beepboop', 1 ); // $ExpectError
|
|
50
|
+
readFile( '/path/to/beepboop', false ); // $ExpectError
|
|
51
|
+
readFile( '/path/to/beepboop', true ); // $ExpectError
|
|
52
|
+
readFile( '/path/to/beepboop', null ); // $ExpectError
|
|
53
|
+
readFile( '/path/to/beepboop', undefined ); // $ExpectError
|
|
54
|
+
readFile( '/path/to/beepboop', [] ); // $ExpectError
|
|
55
|
+
readFile( '/path/to/beepboop', {} ); // $ExpectError
|
|
56
|
+
readFile( '/path/to/beepboop', ( x: number ): number => x ); // $ExpectError
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The compiler throws an error if the function is provided an options argument which is not an object or string...
|
|
60
|
+
{
|
|
61
|
+
readFile( 'beepboop', false, onLoad ); // $ExpectError
|
|
62
|
+
readFile( 'beepboop', true, onLoad ); // $ExpectError
|
|
63
|
+
readFile( 'beepboop', null, onLoad ); // $ExpectError
|
|
64
|
+
readFile( 'beepboop', undefined, onLoad ); // $ExpectError
|
|
65
|
+
readFile( 'beepboop', 123, onLoad ); // $ExpectError
|
|
66
|
+
readFile( 'beepboop', [], onLoad ); // $ExpectError
|
|
67
|
+
readFile( 'beepboop', ( x: number ): number => x, onLoad ); // $ExpectError
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The compiler throws an error if the function is provided an `encoding` option which is not a string or null...
|
|
71
|
+
{
|
|
72
|
+
readFile( 'beepboop', { 'encoding': 123 }, onLoad ); // $ExpectError
|
|
73
|
+
readFile( 'beepboop', { 'encoding': true }, onLoad ); // $ExpectError
|
|
74
|
+
readFile( 'beepboop', { 'encoding': false }, onLoad ); // $ExpectError
|
|
75
|
+
readFile( 'beepboop', { 'encoding': [] }, onLoad ); // $ExpectError
|
|
76
|
+
readFile( 'beepboop', { 'encoding': {} }, onLoad ); // $ExpectError
|
|
77
|
+
readFile( 'beepboop', { 'encoding': ( x: number ): number => x }, onLoad ); // $ExpectError
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// The compiler throws an error if the function is provided a `flag` option which is not a string...
|
|
81
|
+
{
|
|
82
|
+
readFile( 'beepboop', { 'flag': 123 }, onLoad ); // $ExpectError
|
|
83
|
+
readFile( 'beepboop', { 'flag': true }, onLoad ); // $ExpectError
|
|
84
|
+
readFile( 'beepboop', { 'flag': false }, onLoad ); // $ExpectError
|
|
85
|
+
readFile( 'beepboop', { 'flag': null }, onLoad ); // $ExpectError
|
|
86
|
+
readFile( 'beepboop', { 'flag': [] }, onLoad ); // $ExpectError
|
|
87
|
+
readFile( 'beepboop', { 'flag': {} }, onLoad ); // $ExpectError
|
|
88
|
+
readFile( 'beepboop', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The compiler throws an error if the function is provided an unsupported number of arguments...
|
|
92
|
+
{
|
|
93
|
+
readFile(); // $ExpectError
|
|
94
|
+
readFile( 'C:\\foo\\bar\\baz\\beepboop' ); // $ExpectError
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Attached to main export is a `sync` method which returns a string or an error...
|
|
98
|
+
{
|
|
99
|
+
readFile.sync( 'beepboop' ); // $ExpectType string | Buffer | Error
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor...
|
|
103
|
+
{
|
|
104
|
+
readFile.sync( false ); // $ExpectError
|
|
105
|
+
readFile.sync( true ); // $ExpectError
|
|
106
|
+
readFile.sync( null ); // $ExpectError
|
|
107
|
+
readFile.sync( undefined ); // $ExpectError
|
|
108
|
+
readFile.sync( [] ); // $ExpectError
|
|
109
|
+
readFile.sync( {} ); // $ExpectError
|
|
110
|
+
readFile.sync( ( x: number ): number => x ); // $ExpectError
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The compiler throws an error if the `sync` method is provided an options argument which is not an object or string...
|
|
114
|
+
{
|
|
115
|
+
readFile.sync( 'beepboop', null ); // $ExpectError
|
|
116
|
+
readFile.sync( 'beepboop', true ); // $ExpectError
|
|
117
|
+
readFile.sync( 'beepboop', false ); // $ExpectError
|
|
118
|
+
readFile.sync( 'beepboop', 123 ); // $ExpectError
|
|
119
|
+
readFile.sync( 'beepboop', [] ); // $ExpectError
|
|
120
|
+
readFile.sync( 'beepboop', ( x: number ): number => x ); // $ExpectError
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null...
|
|
124
|
+
{
|
|
125
|
+
readFile.sync( 'beepboop', { 'encoding': 123 } ); // $ExpectError
|
|
126
|
+
readFile.sync( 'beepboop', { 'encoding': true } ); // $ExpectError
|
|
127
|
+
readFile.sync( 'beepboop', { 'encoding': false } ); // $ExpectError
|
|
128
|
+
readFile.sync( 'beepboop', { 'encoding': [] } ); // $ExpectError
|
|
129
|
+
readFile.sync( 'beepboop', { 'encoding': {} } ); // $ExpectError
|
|
130
|
+
readFile.sync( 'beepboop', { 'encoding': ( x: number ): number => x } ); // $ExpectError
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string...
|
|
134
|
+
{
|
|
135
|
+
readFile.sync( 'beepboop', { 'flag': 123 } ); // $ExpectError
|
|
136
|
+
readFile.sync( 'beepboop', { 'flag': true } ); // $ExpectError
|
|
137
|
+
readFile.sync( 'beepboop', { 'flag': false } ); // $ExpectError
|
|
138
|
+
readFile.sync( 'beepboop', { 'flag': null } ); // $ExpectError
|
|
139
|
+
readFile.sync( 'beepboop', { 'flag': [] } ); // $ExpectError
|
|
140
|
+
readFile.sync( 'beepboop', { 'flag': {} } ); // $ExpectError
|
|
141
|
+
readFile.sync( 'beepboop', { 'flag': ( x: number ): number => x } ); // $ExpectError
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
|
|
145
|
+
{
|
|
146
|
+
readFile.sync(); // $ExpectError
|
|
147
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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 a file.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/fs/read-file
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var readFile = require( '@stdlib/fs/read-file' );
|
|
28
|
+
*
|
|
29
|
+
* function onFile( error, data ) {
|
|
30
|
+
* if ( error ) {
|
|
31
|
+
* throw error;
|
|
32
|
+
* }
|
|
33
|
+
* console.log( data );
|
|
34
|
+
* }
|
|
35
|
+
* readFile( __filename, onFile );
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* var readFileSync = require( '@stdlib/fs/read-file' ).sync;
|
|
39
|
+
*
|
|
40
|
+
* var out = readFileSync( __filename );
|
|
41
|
+
* if ( out instanceof Error ) {
|
|
42
|
+
* throw out;
|
|
43
|
+
* }
|
|
44
|
+
* console.log( out );
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
// MODULES //
|
|
48
|
+
|
|
49
|
+
var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
|
|
50
|
+
var main = require( './main.js' );
|
|
51
|
+
var sync = require( './sync.js' );
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// MAIN //
|
|
55
|
+
|
|
56
|
+
setReadOnly( main, 'sync', sync );
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
// EXPORTS //
|
|
60
|
+
|
|
61
|
+
module.exports = main;
|
|
@@ -0,0 +1,59 @@
|
|
|
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 readfile = require( 'fs' ).readFile;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Asynchronously reads the entire contents of a file.
|
|
30
|
+
*
|
|
31
|
+
* @param {(string|Buffer|integer)} file - file path or file descriptor
|
|
32
|
+
* @param {(Object|string)} [options] - options
|
|
33
|
+
* @param {(string|null)} [options.encoding] - file encoding
|
|
34
|
+
* @param {string} [options.flag] - file status flag
|
|
35
|
+
* @param {Function} clbk - callback to invoke after reading file contents
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* function onFile( error, data ) {
|
|
39
|
+
* if ( error ) {
|
|
40
|
+
* throw error;
|
|
41
|
+
* }
|
|
42
|
+
* console.log( data );
|
|
43
|
+
* }
|
|
44
|
+
* readFile( __filename, onFile );
|
|
45
|
+
*/
|
|
46
|
+
function readFile() {
|
|
47
|
+
var args;
|
|
48
|
+
var i;
|
|
49
|
+
args = [];
|
|
50
|
+
for ( i = 0; i < arguments.length; i++ ) {
|
|
51
|
+
args.push( arguments[ i ] );
|
|
52
|
+
}
|
|
53
|
+
readfile.apply( null, args );
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// EXPORTS //
|
|
58
|
+
|
|
59
|
+
module.exports = readFile;
|
|
@@ -0,0 +1,61 @@
|
|
|
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 readfileSync = require( 'fs' ).readFileSync;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Synchronously reads the entire contents of a file.
|
|
30
|
+
*
|
|
31
|
+
* @param {(string|Buffer|integer)} file - file path or file descriptor
|
|
32
|
+
* @param {(Object|string)} [options] - options
|
|
33
|
+
* @param {(string|null)} [options.encoding] - file encoding
|
|
34
|
+
* @param {string} [options.flag] - file status flag
|
|
35
|
+
* @returns {(Buffer|string|Error)} file contents or an error
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* var out = readFileSync( __filename );
|
|
39
|
+
* if ( out instanceof Error ) {
|
|
40
|
+
* throw out;
|
|
41
|
+
* }
|
|
42
|
+
* console.log( out );
|
|
43
|
+
*/
|
|
44
|
+
function readFileSync( file, options ) {
|
|
45
|
+
var f;
|
|
46
|
+
try {
|
|
47
|
+
if ( arguments.length > 1 ) {
|
|
48
|
+
f = readfileSync( file, options );
|
|
49
|
+
} else {
|
|
50
|
+
f = readfileSync( file );
|
|
51
|
+
}
|
|
52
|
+
} catch ( err ) {
|
|
53
|
+
return err;
|
|
54
|
+
}
|
|
55
|
+
return f;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
// EXPORTS //
|
|
60
|
+
|
|
61
|
+
module.exports = readFileSync;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/fs/read-file",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Read the entire contents of a file.",
|
|
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": "./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
|
+
]
|
|
68
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
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 File List
|
|
22
|
+
|
|
23
|
+
> Read the entire contents of each file in a file list.
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var readFileList = require( '@stdlib/fs/read-file-list' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### readFileList( filepaths\[, options], clbk )
|
|
34
|
+
|
|
35
|
+
Asynchronously reads the entire contents of each file in a file list.
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
readFileList( [ __filename ], onFiles );
|
|
39
|
+
|
|
40
|
+
function onFiles( error, files ) {
|
|
41
|
+
if ( error ) {
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
console.dir( files );
|
|
45
|
+
// => [{...}]
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Each file is represented by an `object` with the following fields:
|
|
50
|
+
|
|
51
|
+
- **file**: file path.
|
|
52
|
+
- **data**: file contents as either a [`Buffer`][node-buffer] or `string`.
|
|
53
|
+
|
|
54
|
+
The function accepts the same options as [`readFile()`][@stdlib/fs/read-file].
|
|
55
|
+
|
|
56
|
+
#### readFileList.sync( filepaths\[, options] )
|
|
57
|
+
|
|
58
|
+
Synchronously reads the entire contents of each file in a file list.
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
var out = readFileList.sync( [ __filename ] );
|
|
62
|
+
if ( out instanceof Error ) {
|
|
63
|
+
throw out;
|
|
64
|
+
}
|
|
65
|
+
console.dir( out );
|
|
66
|
+
// => [{...}]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The function accepts the same options as [`readFile.sync()`][@stdlib/fs/read-file].
|
|
70
|
+
|
|
71
|
+
</section>
|
|
72
|
+
|
|
73
|
+
<!-- /.usage -->
|
|
74
|
+
|
|
75
|
+
<section class="examples">
|
|
76
|
+
|
|
77
|
+
## Examples
|
|
78
|
+
|
|
79
|
+
<!-- eslint no-undef: "error" -->
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
var readFileList = require( '@stdlib/fs/read-file-list' );
|
|
83
|
+
|
|
84
|
+
/* Sync */
|
|
85
|
+
|
|
86
|
+
var files = readFileList.sync( [ __filename ], 'utf8' );
|
|
87
|
+
// returns <ObjectArray>
|
|
88
|
+
|
|
89
|
+
console.log( files instanceof Error );
|
|
90
|
+
// => false
|
|
91
|
+
|
|
92
|
+
files = readFileList.sync( [ 'beepboop' ], {
|
|
93
|
+
'encoding': 'utf8'
|
|
94
|
+
});
|
|
95
|
+
// returns <Error>
|
|
96
|
+
|
|
97
|
+
console.log( files instanceof Error );
|
|
98
|
+
// => true
|
|
99
|
+
|
|
100
|
+
/* Async */
|
|
101
|
+
|
|
102
|
+
readFileList( [ __filename ], onFiles );
|
|
103
|
+
readFileList( [ 'beepboop' ], onFiles );
|
|
104
|
+
|
|
105
|
+
function onFiles( error, files ) {
|
|
106
|
+
if ( error ) {
|
|
107
|
+
if ( error.code === 'ENOENT' ) {
|
|
108
|
+
console.error( 'A file does not exist.' );
|
|
109
|
+
} else {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
console.dir( files );
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
</section>
|
|
119
|
+
|
|
120
|
+
<!-- /.examples -->
|
|
121
|
+
|
|
122
|
+
* * *
|
|
123
|
+
|
|
124
|
+
<section class="cli">
|
|
125
|
+
|
|
126
|
+
## CLI
|
|
127
|
+
|
|
128
|
+
<section class="usage">
|
|
129
|
+
|
|
130
|
+
### Usage
|
|
131
|
+
|
|
132
|
+
```text
|
|
133
|
+
Usage: read-file-list [options] <filepath1> <filepath2> ...
|
|
134
|
+
|
|
135
|
+
Options:
|
|
136
|
+
|
|
137
|
+
-h, --help Print this message.
|
|
138
|
+
-V, --version Print the package version.
|
|
139
|
+
--enc, --encoding encoding Encoding.
|
|
140
|
+
--flag flag Flag. Default: 'r'.
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
</section>
|
|
144
|
+
|
|
145
|
+
<!-- /.usage -->
|
|
146
|
+
|
|
147
|
+
<section class="notes">
|
|
148
|
+
|
|
149
|
+
### Notes
|
|
150
|
+
|
|
151
|
+
- Relative file paths are resolved relative to the current working directory.
|
|
152
|
+
- Errors are written to `stderr`.
|
|
153
|
+
- File contents are written to `stdout` as newline-delimited JSON ([NDJSON][ndjson]).
|
|
154
|
+
|
|
155
|
+
</section>
|
|
156
|
+
|
|
157
|
+
<!-- /.notes -->
|
|
158
|
+
|
|
159
|
+
<section class="examples">
|
|
160
|
+
|
|
161
|
+
### Examples
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
$ read-file-list ./README.md ./package.json
|
|
165
|
+
{"file":"...","data":"..."}
|
|
166
|
+
{"file":"...","data":"..."}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
</section>
|
|
170
|
+
|
|
171
|
+
<!-- /.examples -->
|
|
172
|
+
|
|
173
|
+
</section>
|
|
174
|
+
|
|
175
|
+
<!-- /.cli -->
|
|
176
|
+
|
|
177
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
178
|
+
|
|
179
|
+
<section class="related">
|
|
180
|
+
|
|
181
|
+
</section>
|
|
182
|
+
|
|
183
|
+
<!-- /.related -->
|
|
184
|
+
|
|
185
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
186
|
+
|
|
187
|
+
<section class="links">
|
|
188
|
+
|
|
189
|
+
[@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
|
|
190
|
+
|
|
191
|
+
[node-buffer]: https://nodejs.org/api/buffer.html
|
|
192
|
+
|
|
193
|
+
[ndjson]: http://ndjson.org/
|
|
194
|
+
|
|
195
|
+
</section>
|
|
196
|
+
|
|
197
|
+
<!-- /.links -->
|