time-to-botec/js/node_modules/@stdlib/fs/read-file-list
NunoSempere b6addc7f05 feat: add the node modules
Necessary in order to clearly see the squiggle hotwiring.
2022-12-03 12:44:49 +00:00
..
bin feat: add the node modules 2022-12-03 12:44:49 +00:00
docs feat: add the node modules 2022-12-03 12:44:49 +00:00
etc feat: add the node modules 2022-12-03 12:44:49 +00:00
lib feat: add the node modules 2022-12-03 12:44:49 +00:00
package.json feat: add the node modules 2022-12-03 12:44:49 +00:00
README.md feat: add the node modules 2022-12-03 12:44:49 +00:00

Read File List

Read the entire contents of each file in a file list.

Usage

var readFileList = require( '@stdlib/fs/read-file-list' );

readFileList( filepaths[, options], clbk )

Asynchronously reads the entire contents of each file in a file list.

readFileList( [ __filename ], onFiles );

function onFiles( error, files ) {
    if ( error ) {
        throw error;
    }
    console.dir( files );
    // => [{...}]
}

Each file is represented by an object with the following fields:

  • file: file path.
  • data: file contents as either a Buffer or string.

The function accepts the same options as readFile().

readFileList.sync( filepaths[, options] )

Synchronously reads the entire contents of each file in a file list.

var out = readFileList.sync( [ __filename ] );
if ( out instanceof Error ) {
    throw out;
}
console.dir( out );
// => [{...}]

The function accepts the same options as readFile.sync().

Examples

var readFileList = require( '@stdlib/fs/read-file-list' );

/* Sync */

var files = readFileList.sync( [ __filename ], 'utf8' );
// returns <ObjectArray>

console.log( files instanceof Error );
// => false

files = readFileList.sync( [ 'beepboop' ], {
    'encoding': 'utf8'
});
// returns <Error>

console.log( files instanceof Error );
// => true

/* Async */

readFileList( [ __filename ], onFiles );
readFileList( [ 'beepboop' ], onFiles );

function onFiles( error, files ) {
    if ( error ) {
        if ( error.code === 'ENOENT' ) {
            console.error( 'A file does not exist.' );
        } else {
            throw error;
        }
    } else {
        console.dir( files );
    }
}

CLI

Usage

Usage: read-file-list [options] <filepath1> <filepath2> ...

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.
  --enc, --encoding encoding   Encoding.
         --flag flag           Flag. Default: 'r'.

Notes

  • Relative file paths are resolved relative to the current working directory.
  • Errors are written to stderr.
  • File contents are written to stdout as newline-delimited JSON (NDJSON).

Examples

$ read-file-list ./README.md ./package.json
{"file":"...","data":"..."}
{"file":"...","data":"..."}