1 | # read-multiple-files
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/read-multiple-files.svg)](https://www.npmjs.com/package/read-multiple-files)
|
4 | [![Build Status](https://travis-ci.org/shinnn/read-multiple-files.svg?branch=master)](https://travis-ci.org/shinnn/read-multiple-files)
|
5 | [![Build status](https://ci.appveyor.com/api/projects/status/ia3h5bcsy84vgfpc?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/read-multiple-files)
|
6 | [![Coverage Status](https://img.shields.io/coveralls/shinnn/read-multiple-files.svg)](https://coveralls.io/r/shinnn/read-multiple-files)
|
7 |
|
8 | A [Node.js](https://nodejs.org/) module to read multiple files asynchronously
|
9 |
|
10 | ```javascript
|
11 | const readMultipleFiles = require('read-multiple-files');
|
12 |
|
13 | readMultipleFiles(new Set(['one.txt', 'another.txt']), (err, bufs) => {
|
14 | if (err) {
|
15 | throw err;
|
16 | }
|
17 |
|
18 | bufs; //=> [<Buffer ... >, <Buffer ... >]
|
19 | });
|
20 | ```
|
21 |
|
22 | ## Installation
|
23 |
|
24 | [Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
|
25 |
|
26 | ```
|
27 | npm install read-multiple-files
|
28 | ```
|
29 |
|
30 | ## API
|
31 |
|
32 | ```javascript
|
33 | const readMultipleFiles = require('read-multiple-files');
|
34 | ```
|
35 |
|
36 | ### readMultipleFiles(*paths* [, *options*], *callback*)
|
37 |
|
38 | *paths*: `<Array|Set<string|Buffer|URL|integer>>` (file paths)
|
39 | *options*: `Object` ([fs.readFile] options) or `string` (encoding)
|
40 | *callback*: `Function`
|
41 |
|
42 | #### callback(*error*, *contents*)
|
43 |
|
44 | *error*: `Error` if it fails to read at least one of the files, otherwise `null`
|
45 | *contents*: `Array<Buffer>` or `Array<string>` (according to `encoding` option)
|
46 |
|
47 | The second argument will be an array of file contents. The order of contents follows the order of file paths.
|
48 |
|
49 | It automatically strips [UTF-8 byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) from results.
|
50 |
|
51 | ```javascript
|
52 | const readMultipleFiles = require('read-multiple-files');
|
53 |
|
54 | // foo.txt: Hello
|
55 | // bar.txt: World
|
56 |
|
57 | readMultipleFiles(['foo.txt', 'bar.txt'], 'utf8', (err, contents) => {
|
58 | if (err) {
|
59 | throw err;
|
60 | }
|
61 |
|
62 | contents; //=> ['Hello', 'World']
|
63 | });
|
64 | ```
|
65 |
|
66 | If it fails to read at least one of the files, it passes an error to the first argument and doesn't pass any values to the second argument.
|
67 |
|
68 | ```javascript
|
69 | const readMultipleFiles = require('read-multiple-files');
|
70 |
|
71 | // foo.txt: exists
|
72 | // bar.txt: doesn't exist
|
73 | // baz.txt: exists
|
74 |
|
75 | readMultipleFiles(['foo.txt', 'bar.txt', 'baz.txt'], (err, contents) => {
|
76 | err.code; //=> 'ENOENT'
|
77 | contents; //=> undefined
|
78 | arguments.length; //=> 1
|
79 | });
|
80 | ```
|
81 |
|
82 | ## Related project
|
83 |
|
84 | * [read-files-promise](https://github.com/shinnn/read-files-promise) ([Promises](https://promisesaplus.com/) version)
|
85 |
|
86 | ## License
|
87 |
|
88 | [ISC License](./LICENSE) © 2017 Shinnosuke Watanabe
|