README.md (3649B)
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 class="links"> 178 179 [@stdlib/fs/read-file]: https://www.npmjs.com/package/@stdlib/fs/tree/main/read-file 180 181 [node-buffer]: https://nodejs.org/api/buffer.html 182 183 [ndjson]: http://ndjson.org/ 184 185 </section> 186 187 <!-- /.links -->