@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.
Files changed (173) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +230 -2
  4. package/SECURITY.md +5 -0
  5. package/append-file/README.md +239 -0
  6. package/append-file/bin/cli +110 -0
  7. package/append-file/docs/repl.txt +72 -0
  8. package/append-file/docs/types/index.d.ts +133 -0
  9. package/append-file/docs/types/test.ts +193 -0
  10. package/append-file/docs/usage.txt +10 -0
  11. package/append-file/etc/cli_opts.json +22 -0
  12. package/append-file/lib/async.js +58 -0
  13. package/append-file/lib/index.js +61 -0
  14. package/append-file/lib/sync.js +58 -0
  15. package/append-file/package.json +69 -0
  16. package/close/README.md +172 -0
  17. package/close/docs/repl.txt +45 -0
  18. package/close/docs/types/index.d.ts +135 -0
  19. package/close/docs/types/test.ts +89 -0
  20. package/close/lib/index.js +73 -0
  21. package/close/lib/main.js +68 -0
  22. package/close/lib/sync.js +65 -0
  23. package/close/package.json +64 -0
  24. package/docs/types/index.d.ts +502 -0
  25. package/exists/README.md +252 -0
  26. package/exists/bin/cli +83 -0
  27. package/exists/docs/repl.txt +42 -0
  28. package/exists/docs/types/index.d.ts +112 -0
  29. package/exists/docs/types/test.ts +87 -0
  30. package/exists/docs/usage.txt +8 -0
  31. package/exists/etc/cli_opts.json +14 -0
  32. package/exists/lib/async.js +85 -0
  33. package/exists/lib/index.js +66 -0
  34. package/exists/lib/sync.js +62 -0
  35. package/exists/package.json +69 -0
  36. package/lib/index.js +186 -0
  37. package/open/README.md +204 -0
  38. package/open/docs/repl.txt +65 -0
  39. package/open/docs/types/index.d.ts +150 -0
  40. package/open/docs/types/test.ts +129 -0
  41. package/open/lib/defaults.json +4 -0
  42. package/open/lib/index.js +63 -0
  43. package/open/lib/main.js +64 -0
  44. package/open/lib/sync.js +68 -0
  45. package/open/package.json +64 -0
  46. package/package.json +67 -4
  47. package/read-dir/README.md +232 -0
  48. package/read-dir/bin/cli +83 -0
  49. package/read-dir/docs/repl.txt +44 -0
  50. package/read-dir/docs/types/index.d.ts +99 -0
  51. package/read-dir/docs/types/test.ts +85 -0
  52. package/read-dir/docs/usage.txt +8 -0
  53. package/read-dir/etc/cli_opts.json +14 -0
  54. package/read-dir/lib/async.js +57 -0
  55. package/read-dir/lib/index.js +62 -0
  56. package/read-dir/lib/sync.js +52 -0
  57. package/read-dir/package.json +69 -0
  58. package/read-file/README.md +246 -0
  59. package/read-file/bin/cli +91 -0
  60. package/read-file/docs/repl.txt +68 -0
  61. package/read-file/docs/types/index.d.ts +138 -0
  62. package/read-file/docs/types/test.ts +147 -0
  63. package/read-file/docs/usage.txt +10 -0
  64. package/read-file/etc/cli_opts.json +21 -0
  65. package/read-file/lib/index.js +61 -0
  66. package/read-file/lib/main.js +59 -0
  67. package/read-file/lib/sync.js +61 -0
  68. package/read-file/package.json +68 -0
  69. package/read-file-list/README.md +197 -0
  70. package/read-file-list/bin/cli +100 -0
  71. package/read-file-list/docs/repl.txt +81 -0
  72. package/read-file-list/docs/types/index.d.ts +166 -0
  73. package/read-file-list/docs/types/test.ts +166 -0
  74. package/read-file-list/docs/usage.txt +10 -0
  75. package/read-file-list/etc/cli_opts.json +21 -0
  76. package/read-file-list/lib/async.js +202 -0
  77. package/read-file-list/lib/clear_pending.js +47 -0
  78. package/read-file-list/lib/clear_timeout.js +23 -0
  79. package/read-file-list/lib/config.json +4 -0
  80. package/read-file-list/lib/delay.js +47 -0
  81. package/read-file-list/lib/index.js +66 -0
  82. package/read-file-list/lib/sync.js +94 -0
  83. package/read-file-list/package.json +72 -0
  84. package/read-json/README.md +176 -0
  85. package/read-json/docs/repl.txt +72 -0
  86. package/read-json/docs/types/index.d.ts +170 -0
  87. package/read-json/docs/types/test.ts +175 -0
  88. package/read-json/lib/async.js +114 -0
  89. package/read-json/lib/index.js +65 -0
  90. package/read-json/lib/sync.js +89 -0
  91. package/read-json/package.json +66 -0
  92. package/read-ndjson/README.md +166 -0
  93. package/read-ndjson/docs/repl.txt +72 -0
  94. package/read-ndjson/docs/types/index.d.ts +170 -0
  95. package/read-ndjson/docs/types/test.ts +179 -0
  96. package/read-ndjson/lib/async.js +114 -0
  97. package/read-ndjson/lib/index.js +65 -0
  98. package/read-ndjson/lib/sync.js +89 -0
  99. package/read-ndjson/package.json +68 -0
  100. package/read-wasm/README.md +158 -0
  101. package/read-wasm/docs/repl.txt +58 -0
  102. package/read-wasm/docs/types/index.d.ts +153 -0
  103. package/read-wasm/docs/types/test.ts +127 -0
  104. package/read-wasm/lib/async.js +106 -0
  105. package/read-wasm/lib/index.js +67 -0
  106. package/read-wasm/lib/sync.js +86 -0
  107. package/read-wasm/package.json +67 -0
  108. package/rename/README.md +259 -0
  109. package/rename/bin/cli +89 -0
  110. package/rename/docs/repl.txt +80 -0
  111. package/rename/docs/types/index.d.ts +109 -0
  112. package/rename/docs/types/test.ts +111 -0
  113. package/rename/docs/usage.txt +7 -0
  114. package/rename/etc/cli_opts.json +15 -0
  115. package/rename/lib/index.js +60 -0
  116. package/rename/lib/main.js +51 -0
  117. package/rename/lib/sync.js +53 -0
  118. package/rename/package.json +73 -0
  119. package/resolve-parent-path/README.md +204 -0
  120. package/resolve-parent-path/bin/cli +85 -0
  121. package/resolve-parent-path/docs/repl.txt +58 -0
  122. package/resolve-parent-path/docs/types/index.d.ts +128 -0
  123. package/resolve-parent-path/docs/types/test.ts +117 -0
  124. package/resolve-parent-path/docs/usage.txt +9 -0
  125. package/resolve-parent-path/etc/cli_opts.json +17 -0
  126. package/resolve-parent-path/lib/async.js +116 -0
  127. package/resolve-parent-path/lib/index.js +58 -0
  128. package/resolve-parent-path/lib/sync.js +83 -0
  129. package/resolve-parent-path/lib/validate.js +67 -0
  130. package/resolve-parent-path/package.json +78 -0
  131. package/resolve-parent-path-by/README.md +220 -0
  132. package/resolve-parent-path-by/docs/repl.txt +105 -0
  133. package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
  134. package/resolve-parent-path-by/docs/types/test.ts +225 -0
  135. package/resolve-parent-path-by/lib/async.js +156 -0
  136. package/resolve-parent-path-by/lib/index.js +67 -0
  137. package/resolve-parent-path-by/lib/sync.js +98 -0
  138. package/resolve-parent-path-by/lib/validate.js +67 -0
  139. package/resolve-parent-path-by/package.json +76 -0
  140. package/resolve-parent-paths/README.md +228 -0
  141. package/resolve-parent-paths/bin/cli +93 -0
  142. package/resolve-parent-paths/docs/repl.txt +84 -0
  143. package/resolve-parent-paths/docs/types/index.d.ts +145 -0
  144. package/resolve-parent-paths/docs/types/test.ts +196 -0
  145. package/resolve-parent-paths/docs/usage.txt +10 -0
  146. package/resolve-parent-paths/etc/cli_opts.json +18 -0
  147. package/resolve-parent-paths/lib/index.js +60 -0
  148. package/resolve-parent-paths/lib/main.js +415 -0
  149. package/resolve-parent-paths/lib/sync.js +231 -0
  150. package/resolve-parent-paths/lib/validate.js +81 -0
  151. package/resolve-parent-paths/package.json +78 -0
  152. package/unlink/README.md +233 -0
  153. package/unlink/bin/cli +81 -0
  154. package/unlink/docs/repl.txt +62 -0
  155. package/unlink/docs/types/index.d.ts +105 -0
  156. package/unlink/docs/types/test.ts +85 -0
  157. package/unlink/docs/usage.txt +7 -0
  158. package/unlink/etc/cli_opts.json +15 -0
  159. package/unlink/lib/index.js +60 -0
  160. package/unlink/lib/main.js +50 -0
  161. package/unlink/lib/sync.js +52 -0
  162. package/unlink/package.json +70 -0
  163. package/write-file/README.md +250 -0
  164. package/write-file/bin/cli +110 -0
  165. package/write-file/docs/repl.txt +74 -0
  166. package/write-file/docs/types/index.d.ts +133 -0
  167. package/write-file/docs/types/test.ts +193 -0
  168. package/write-file/docs/usage.txt +10 -0
  169. package/write-file/etc/cli_opts.json +22 -0
  170. package/write-file/lib/index.js +60 -0
  171. package/write-file/lib/main.js +58 -0
  172. package/write-file/lib/sync.js +58 -0
  173. package/write-file/package.json +68 -0
@@ -0,0 +1,52 @@
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 readdirSync = require( 'fs' ).readdirSync;
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Synchronously reads the contents of a directory.
30
+ *
31
+ * @param {(string|Buffer)} path - directory path
32
+ * @returns {(StringArray|Error)} directory contents or an error
33
+ *
34
+ * @example
35
+ * var out = readDirSync( __dirname );
36
+ * if ( out instanceof Error ) {
37
+ * throw out;
38
+ * }
39
+ * console.log( out );
40
+ */
41
+ function readDirSync( path ) {
42
+ try {
43
+ return readdirSync( path );
44
+ } catch ( err ) {
45
+ return err;
46
+ }
47
+ }
48
+
49
+
50
+ // EXPORTS //
51
+
52
+ module.exports = readDirSync;
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@stdlib/fs/read-dir",
3
+ "version": "0.0.0",
4
+ "description": "Read the entire contents of a directory.",
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-dir": "./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
+ "readdir",
59
+ "readdirsync",
60
+ "path",
61
+ "async",
62
+ "sync",
63
+ "dir",
64
+ "read",
65
+ "open",
66
+ "filesystem",
67
+ "directory"
68
+ ]
69
+ }
@@ -0,0 +1,246 @@
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
22
+
23
+ > Read the entire contents of a file.
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ ```javascript
30
+ var readFile = require( '@stdlib/fs/read-file' );
31
+ ```
32
+
33
+ #### readFile( file\[, options], clbk )
34
+
35
+ Asynchronously reads the entire contents of a file.
36
+
37
+ ```javascript
38
+ readFile( __filename, onFile );
39
+
40
+ function onFile( error, data ) {
41
+ if ( error ) {
42
+ throw error;
43
+ }
44
+ console.log( data );
45
+ }
46
+ ```
47
+
48
+ The function accepts the same `options` and has the same defaults as [`fs.readFile()`][node-fs].
49
+
50
+ #### readFile.sync( file\[, options] )
51
+
52
+ Synchronously reads the entire contents of a `file`.
53
+
54
+ ```javascript
55
+ var out = readFile.sync( __filename );
56
+ if ( out instanceof Error ) {
57
+ throw out;
58
+ }
59
+ console.log( out );
60
+ ```
61
+
62
+ The function accepts the same `options` and has the same defaults as [`fs.readFileSync()`][node-fs].
63
+
64
+ </section>
65
+
66
+ <!-- /.usage -->
67
+
68
+ <section class="notes">
69
+
70
+ ## Notes
71
+
72
+ - The difference between this API and [`fs.readFileSync()`][node-fs] is that [`fs.readFileSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent `path`) and this API will return an `error`. Hence, the following anti-pattern
73
+
74
+
75
+ ```javascript
76
+ var fs = require( 'fs' );
77
+
78
+ var file = '/path/to/file.js';
79
+
80
+ // Check for existence to prevent an error being thrown...
81
+ if ( fs.existsSync( file ) ) {
82
+ file = fs.readFileSync( file );
83
+ }
84
+ ```
85
+
86
+ can be replaced by an approach which addresses existence via `error` handling.
87
+
88
+ ```javascript
89
+ var readFile = require( '@stdlib/fs/read-file' );
90
+
91
+ var file = '/path/to/file.js';
92
+
93
+ // Explicitly handle the error...
94
+ file = readFile.sync( file );
95
+ if ( file instanceof Error ) {
96
+ // You choose what to do...
97
+ console.error( file.message );
98
+ }
99
+ ```
100
+
101
+ </section>
102
+
103
+ <!-- /.notes -->
104
+
105
+ <section class="examples">
106
+
107
+ ## Examples
108
+
109
+ <!-- eslint no-undef: "error" -->
110
+
111
+ ```javascript
112
+ var readFile = require( '@stdlib/fs/read-file' );
113
+
114
+ /* Sync */
115
+
116
+ var file = readFile.sync( __filename, 'utf8' );
117
+ // returns <string>
118
+
119
+ console.log( file instanceof Error );
120
+ // => false
121
+
122
+ file = readFile.sync( 'beepboop', {
123
+ 'encoding': 'utf8'
124
+ });
125
+ // returns <Error>
126
+
127
+ console.log( file instanceof Error );
128
+ // => true
129
+
130
+ /* Async */
131
+
132
+ readFile( __filename, onFile );
133
+ readFile( 'beepboop', onFile );
134
+
135
+ function onFile( error, data ) {
136
+ if ( error ) {
137
+ if ( error.code === 'ENOENT' ) {
138
+ console.error( 'File does not exist.' );
139
+ } else {
140
+ throw error;
141
+ }
142
+ } else {
143
+ console.log( data );
144
+ }
145
+ }
146
+ ```
147
+
148
+ </section>
149
+
150
+ <!-- /.examples -->
151
+
152
+ * * *
153
+
154
+ <section class="cli">
155
+
156
+ ## CLI
157
+
158
+ <section class="usage">
159
+
160
+ ### Usage
161
+
162
+ ```text
163
+ Usage: read-file [options] <filepath>
164
+
165
+ Options:
166
+
167
+ -h, --help Print this message.
168
+ -V, --version Print the package version.
169
+ --enc, --encoding encoding Encoding.
170
+ --flag flag Flag. Default: 'r'.
171
+ ```
172
+
173
+ </section>
174
+
175
+ <!-- /.usage -->
176
+
177
+ <section class="notes">
178
+
179
+ ### Notes
180
+
181
+ - Relative file paths are resolved relative to the current working directory.
182
+ - Errors are written to `stderr`.
183
+ - File contents are written to `stdout`.
184
+
185
+ </section>
186
+
187
+ <!-- /.notes -->
188
+
189
+ <section class="examples">
190
+
191
+ ### Examples
192
+
193
+ ```bash
194
+ $ read-file ./README.md
195
+ <file_contents>
196
+ ```
197
+
198
+ </section>
199
+
200
+ <!-- /.examples -->
201
+
202
+ </section>
203
+
204
+ <!-- /.cli -->
205
+
206
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
207
+
208
+ <section class="related">
209
+
210
+ * * *
211
+
212
+ ## See Also
213
+
214
+ - <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>
215
+ - <span class="package-name">[`@stdlib/fs/open`][@stdlib/fs/open]</span><span class="delimiter">: </span><span class="description">open a file.</span>
216
+ - <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>
217
+ - <span class="package-name">[`@stdlib/fs/read-json`][@stdlib/fs/read-json]</span><span class="delimiter">: </span><span class="description">read a file as JSON.</span>
218
+ - <span class="package-name">[`@stdlib/fs/write-file`][@stdlib/fs/write-file]</span><span class="delimiter">: </span><span class="description">write data to a file.</span>
219
+
220
+ </section>
221
+
222
+ <!-- /.related -->
223
+
224
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225
+
226
+ <section class="links">
227
+
228
+ [node-fs]: https://nodejs.org/api/fs.html
229
+
230
+ <!-- <related-links> -->
231
+
232
+ [@stdlib/fs/exists]: https://github.com/stdlib-js/fs/tree/main/exists
233
+
234
+ [@stdlib/fs/open]: https://github.com/stdlib-js/fs/tree/main/open
235
+
236
+ [@stdlib/fs/read-dir]: https://github.com/stdlib-js/fs/tree/main/read-dir
237
+
238
+ [@stdlib/fs/read-json]: https://github.com/stdlib-js/fs/tree/main/read-json
239
+
240
+ [@stdlib/fs/write-file]: https://github.com/stdlib-js/fs/tree/main/write-file
241
+
242
+ <!-- </related-links> -->
243
+
244
+ </section>
245
+
246
+ <!-- /.links -->
@@ -0,0 +1,91 @@
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 cwd = require( 'process' ).cwd; // Note: avoiding `@stdlib/process/cwd` is intentional in order to avoid a circular dependency at the package level
27
+ var CLI = require( '@stdlib/cli/ctor' );
28
+ var readFile = require( './../lib' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Main execution sequence.
35
+ *
36
+ * @private
37
+ * @returns {void}
38
+ */
39
+ function main() {
40
+ var fpath;
41
+ var flags;
42
+ var args;
43
+ var opts;
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': readFile.sync( 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
+ opts = {};
65
+ if ( flags.encoding ) {
66
+ opts.encoding = flags.encoding;
67
+ }
68
+ if ( flags.flag ) {
69
+ opts.flag = flags.flag;
70
+ }
71
+
72
+ fpath = resolve( cwd(), args[ 0 ] );
73
+ readFile( fpath, opts, onFile );
74
+
75
+ /**
76
+ * Callback invoked upon reading the contents of a file.
77
+ *
78
+ * @private
79
+ * @param {(Error|null)} error - error object
80
+ * @param {(Buffer|string)} file - file contents
81
+ * @returns {void}
82
+ */
83
+ function onFile( error, file ) {
84
+ if ( error ) {
85
+ return cli.error( error );
86
+ }
87
+ console.log( file.toString() ); // eslint-disable-line no-console
88
+ }
89
+ }
90
+
91
+ main();
@@ -0,0 +1,68 @@
1
+
2
+ {{alias}}( file[, options], clbk )
3
+ Asynchronously reads the entire contents of a file.
4
+
5
+ If provided an encoding, the function returns a string. Otherwise, the
6
+ function returns a Buffer object.
7
+
8
+ Parameters
9
+ ----------
10
+ file: string|Buffer|integer
11
+ Filename or file descriptor.
12
+
13
+ options: Object|string (optional)
14
+ Options. If a string, the value is the encoding.
15
+
16
+ options.encoding: string|null (optional)
17
+ Encoding. Default: null.
18
+
19
+ options.flag: string (optional)
20
+ Flag. Default: 'r'.
21
+
22
+ clbk: Function
23
+ Callback to invoke upon reading file contents.
24
+
25
+ Examples
26
+ --------
27
+ > function onRead( error, data ) {
28
+ ... if ( error ) {
29
+ ... console.error( error.message );
30
+ ... } else {
31
+ ... console.log( data );
32
+ ... }
33
+ ... };
34
+ > {{alias}}( './beep/boop.js', onRead );
35
+
36
+
37
+ {{alias}}.sync( file[, options] )
38
+ Synchronously reads the entire contents of a file.
39
+
40
+ If provided an encoding, the function returns a string. Otherwise, the
41
+ function returns a Buffer object.
42
+
43
+ Parameters
44
+ ----------
45
+ file: string|Buffer|integer
46
+ Filename or file descriptor.
47
+
48
+ options: Object|string (optional)
49
+ Options. If a string, the value is the encoding.
50
+
51
+ options.encoding: string|null (optional)
52
+ Encoding. Default: null.
53
+
54
+ options.flag: string (optional)
55
+ Flag. Default: 'r'.
56
+
57
+ Returns
58
+ -------
59
+ out: Error|Buffer|string
60
+ File contents.
61
+
62
+ Examples
63
+ --------
64
+ > var out = {{alias}}.sync( './beep/boop.js' );
65
+
66
+ See Also
67
+ --------
68
+
@@ -0,0 +1,138 @@
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
+ * Interface defining function options.
27
+ */
28
+ interface Options {
29
+ /**
30
+ * Encoding (default: null).
31
+ */
32
+ encoding?: string | null;
33
+
34
+ /**
35
+ * Flag (default: 'r').
36
+ */
37
+ flag?: string;
38
+ }
39
+
40
+ /**
41
+ * Callback invoked upon reading a file.
42
+ *
43
+ * @param err - error object
44
+ * @param file - file contents
45
+ */
46
+ type Callback = ( err: Error | null, file: Buffer | string ) => void;
47
+
48
+ /**
49
+ * Interface for reading a file.
50
+ */
51
+ interface ReadFile {
52
+ /**
53
+ * Asynchronously reads the entire contents of a file.
54
+ *
55
+ * @param file - file path or file descriptor
56
+ * @param options - options
57
+ * @param options.encoding - file encoding
58
+ * @param options.flag - file status flag
59
+ * @param clbk - callback to invoke after reading file contents
60
+ *
61
+ * @example
62
+ * function onFile( error, data ) {
63
+ * if ( error ) {
64
+ * throw error;
65
+ * }
66
+ * console.log( data );
67
+ * }
68
+ * readFile( __filename, onFile );
69
+ */
70
+ ( file: string | Buffer | number, options: Options | string, clbk: Callback ): void;
71
+
72
+ /**
73
+ * Asynchronously reads the entire contents of a file.
74
+ *
75
+ * @param file - file path or file descriptor
76
+ * @param clbk - callback to invoke after reading file contents
77
+ *
78
+ * @example
79
+ * function onFile( error, data ) {
80
+ * if ( error ) {
81
+ * throw error;
82
+ * }
83
+ * console.log( data );
84
+ * }
85
+ * readFile( __filename, onFile );
86
+ */
87
+ ( file: string | Buffer | number, clbk: Callback ): void;
88
+
89
+ /**
90
+ * Synchronously reads the entire contents of a file.
91
+ *
92
+ * @param file - file path or file descriptor
93
+ * @param option - options
94
+ * @param options.encoding - file encoding
95
+ * @param options.flag - file status flag
96
+ * @returns file contents or an error
97
+ *
98
+ * @example
99
+ * var out = readFile.sync( __filename );
100
+ * if ( out instanceof Error ) {
101
+ * throw out;
102
+ * }
103
+ * console.log( out );
104
+ */
105
+ sync( file: string | Buffer | number, options?: Options | string ): Buffer | string | Error;
106
+ }
107
+
108
+ /**
109
+ * Asynchronously reads the entire contents of a file.
110
+ *
111
+ * @param file - file path or file descriptor
112
+ * @param options - options
113
+ * @param options.encoding - file encoding
114
+ * @param options.flag - file status flag
115
+ * @param clbk - callback to invoke after reading file contents
116
+ *
117
+ * @example
118
+ * function onFile( error, data ) {
119
+ * if ( error ) {
120
+ * throw error;
121
+ * }
122
+ * console.log( data );
123
+ * }
124
+ * readFile( __filename, onFile );
125
+ *
126
+ * @example
127
+ * var out = readFile.sync( __filename );
128
+ * if ( out instanceof Error ) {
129
+ * throw out;
130
+ * }
131
+ * console.log( out );
132
+ */
133
+ declare var readFile: ReadFile;
134
+
135
+
136
+ // EXPORTS //
137
+
138
+ export = readFile;