\href{https://nodei.co/npm/readdirp/}{\texttt{ }}

Recursive version of \href{http://nodejs.org/docs/latest/api/fs.html\#fs_fs_readdir_path_callback}{\texttt{ fs.\+readdir}}. Exposes a {\bfseries{stream api}}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var readdirp = require('readdirp')}
\DoxyCodeLine{  , path = require('path')}
\DoxyCodeLine{  , es = require('event-\/stream');}
\DoxyCodeLine{}
\DoxyCodeLine{// print out all JavaScript files along with their size}
\DoxyCodeLine{}
\DoxyCodeLine{var stream = readdirp(\{ root: path.join(\_\_dirname), fileFilter: '*.js' \});}
\DoxyCodeLine{stream}
\DoxyCodeLine{  .on('warn', function (err) \{}
\DoxyCodeLine{    console.error('non-\/fatal error', err);}
\DoxyCodeLine{    // optionally call stream.destroy() here in order to abort and cause 'close' to be emitted}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  .on('error', function (err) \{ console.error('fatal error', err); \})}
\DoxyCodeLine{  .pipe(es.mapSync(function (entry) \{}
\DoxyCodeLine{    return \{ path: entry.path, size: entry.stat.size \};}
\DoxyCodeLine{  \}))}
\DoxyCodeLine{  .pipe(es.stringify())}
\DoxyCodeLine{  .pipe(process.stdout); }

\end{DoxyCode}


Meant to be one of the recursive versions of \href{http://nodejs.org/docs/latest/api/fs.html}{\texttt{ fs}} functions, e.\+g., like \href{https://github.com/substack/node-mkdirp}{\texttt{ mkdirp}}.

{\bfseries{Table of Contents}} {\itshape generated with \href{http://doctoc.herokuapp.com/}{\texttt{ Doc\+Toc}}}


\begin{DoxyItemize}
\item \href{\#installation}{\texttt{ Installation}}
\item \href{\#api}{\texttt{ API}}
\begin{DoxyItemize}
\item \href{\#entry-stream}{\texttt{ entry stream}}
\item \href{\#options}{\texttt{ options}}
\item \href{\#entry-info}{\texttt{ entry info}}
\item \href{\#filters}{\texttt{ Filters}}
\item \href{\#callback-api}{\texttt{ Callback API}}
\begin{DoxyItemize}
\item \href{\#allprocessed}{\texttt{ all\+Processed}}
\item \href{\#fileprocessed}{\texttt{ file\+Processed}}
\end{DoxyItemize}
\end{DoxyItemize}
\item \href{\#more-examples}{\texttt{ More Examples}}
\begin{DoxyItemize}
\item \href{\#stream-api}{\texttt{ stream api}}
\item \href{\#stream-api-pipe}{\texttt{ stream api pipe}}
\item \href{\#grep}{\texttt{ grep}}
\item \href{\#using-callback-api}{\texttt{ using callback api}}
\item \href{\#tests}{\texttt{ tests}}
\end{DoxyItemize}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32126}{}\doxysection{Installation}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32126}
\begin{DoxyVerb}npm install readdirp
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32127}{}\doxysection{API}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32127}
{\itshape {\bfseries{var entry\+Stream = readdirp (options)}}}

Reads given root recursively and returns a {\ttfamily stream} of \href{\#entry-info}{\texttt{ entry info}}s.\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32128}{}\doxysubsection{entry stream}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32128}
Behaves as follows\+:


\begin{DoxyItemize}
\item `emit(\textquotesingle{}data'){\ttfamily passes an \mbox{[}entry info\mbox{]}(\#entry-\/info) whenever one is found -\/}emit(\textquotesingle{}warn\textquotesingle{}){\ttfamily passes a non-\/fatal}Error\`{} that prevents a file/directory from being processed (i.\+e., if it is inaccessible to the user)
\item `emit(\textquotesingle{}error'){\ttfamily passes a fatal}Error{\ttfamily which also ends the stream (i.\+e., when illegal options where passed) -\/}emit(\textquotesingle{}end\textquotesingle{}){\ttfamily called when all entries were found and no more will be emitted (i.\+e., we are done) -\/}emit(\textquotesingle{}close\textquotesingle{}){\ttfamily called when the stream is destroyed via}stream.\+destroy(){\ttfamily (which could be useful if you want to manually abort even on a non fatal error) -\/ at that point the stream is no longer}readable\`{} and no more entries, warning or errors are emitted
\item to learn more about streams, consult the very detailed \href{http://nodejs.org/api/stream.html}{\texttt{ nodejs streams documentation}} or the \href{https://github.com/substack/stream-handbook}{\texttt{ stream-\/handbook}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32129}{}\doxysubsection{options}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32129}

\begin{DoxyItemize}
\item {\bfseries{root}}\+: path in which to start reading and recursing into subdirectories
\item {\bfseries{file\+Filter}}\+: filter to include/exclude files found (see \href{\#filters}{\texttt{ Filters}} for more)
\item {\bfseries{directory\+Filter}}\+: filter to include/exclude directories found and to recurse into (see \href{\#filters}{\texttt{ Filters}} for more)
\item {\bfseries{depth}}\+: depth at which to stop recursing even if more subdirectories are found
\item {\bfseries{entry\+Type}}\+: determines if data events on the stream should be emitted for `\textquotesingle{}files'{\ttfamily ,}\textquotesingle{}directories\textquotesingle{}{\ttfamily ,}\textquotesingle{}both\textquotesingle{}{\ttfamily , or}\textquotesingle{}all\textquotesingle{}{\ttfamily . Setting to}\textquotesingle{}all\textquotesingle{}{\ttfamily will also include entries for other types of file descriptors like character devices, unix sockets and named pipes. Defaults to}\textquotesingle{}files\textquotesingle{}\`{}.
\item {\bfseries{lstat}}\+: if {\ttfamily true}, readdirp uses {\ttfamily fs.\+lstat} instead of {\ttfamily fs.\+stat} in order to stat files and includes symlink entries in the stream along with files.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32130}{}\doxysubsection{entry info}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32130}
Has the following properties\+:


\begin{DoxyItemize}
\item {\bfseries{parent\+Dir}} \+: directory in which entry was found (relative to given root)
\item {\bfseries{full\+Parent\+Dir}} \+: full path to parent directory
\item {\bfseries{name}} \+: name of the file/directory
\item {\bfseries{path}} \+: path to the file/directory (relative to given root)
\item {\bfseries{full\+Path}} \+: full path to the file/directory found
\item {\bfseries{stat}} \+: built in \href{http://nodejs.org/docs/v0.4.9/api/fs.html\#fs.Stats}{\texttt{ stat object}}
\item {\bfseries{Example}}\+: (assuming root was {\ttfamily /\+User/dev/readdirp}) \begin{DoxyVerb}  parentDir     :  'test/bed/root_dir1',
  fullParentDir :  '/User/dev/readdirp/test/bed/root_dir1',
  name          :  'root_dir1_subdir1',
  path          :  'test/bed/root_dir1/root_dir1_subdir1',
  fullPath      :  '/User/dev/readdirp/test/bed/root_dir1/root_dir1_subdir1',
  stat          :  [ ... ]
\end{DoxyVerb}
 
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32131}{}\doxysubsection{Filters}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32131}
There are three different ways to specify filters for files and directories respectively.


\begin{DoxyItemize}
\item {\bfseries{function}}\+: a function that takes an entry info as a parameter and returns true to include or false to exclude the entry
\item {\bfseries{glob string}}\+: a string (e.\+g., {\ttfamily $\ast$.js}) which is matched using \href{https://github.com/isaacs/minimatch}{\texttt{ minimatch}}, so go there for more information.

Globstars ({\ttfamily $\ast$$\ast$}) are not supported since specifying a recursive pattern for an already recursive function doesn\textquotesingle{}t make sense.

Negated globs (as explained in the minimatch documentation) are allowed, e.\+g., {\ttfamily !$\ast$.txt} matches everything but text files.
\item {\bfseries{array of glob strings}}\+: either need to be all inclusive or all exclusive (negated) patterns otherwise an error is thrown.

`\mbox{[} '$\ast$.json\textquotesingle{}, \textquotesingle{}$\ast$.js\textquotesingle{} \mbox{]}\`{} includes all Java\+Script and Json files.
\end{DoxyItemize}

\begin{DoxyVerb}`[ '!.git', '!node_modules' ]` includes all directories except the '.git' and 'node_modules'.
\end{DoxyVerb}
 Directories that do not pass a filter will not be recursed into.\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32132}{}\doxysubsection{Callback API}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32132}
Although the stream api is recommended, readdirp also exposes a callback based api.

{\itshape {\bfseries{readdirp (options, callback1 \mbox{[}, callback2\mbox{]})}}}

If callback2 is given, callback1 functions as the {\bfseries{file\+Processed}} callback, and callback2 as the {\bfseries{all\+Processed}} callback.

If only callback1 is given, it functions as the {\bfseries{all\+Processed}} callback.\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32133}{}\doxysubsubsection{all\+Processed}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32133}

\begin{DoxyItemize}
\item function with err and res parameters, e.\+g., {\ttfamily function (err, res) \{ ... \}}
\item {\bfseries{err}}\+: array of errors that occurred during the operation, {\bfseries{res may still be present, even if errors occurred}}
\item {\bfseries{res}}\+: collection of file/directory \href{\#entry-info}{\texttt{ entry infos}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32134}{}\doxysubsubsection{file\+Processed}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32134}

\begin{DoxyItemize}
\item function with \href{\#entry-info}{\texttt{ entry info}} parameter e.\+g., {\ttfamily function (entry\+Info) \{ ... \}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32135}{}\doxysection{More Examples}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32135}
`on(\textquotesingle{}error', ..){\ttfamily ,}on(\textquotesingle{}warn\textquotesingle{}, ..){\ttfamily and}on(\textquotesingle{}end\textquotesingle{}, ..)\`{} handling omitted for brevity


\begin{DoxyCode}{0}
\DoxyCodeLine{var readdirp = require('readdirp');}
\DoxyCodeLine{}
\DoxyCodeLine{// Glob file filter}
\DoxyCodeLine{readdirp(\{ root: './test/bed', fileFilter: '*.js' \})}
\DoxyCodeLine{  .on('data', function (entry) \{}
\DoxyCodeLine{    // do something with each JavaScript file entry}
\DoxyCodeLine{  \});}
\DoxyCodeLine{}
\DoxyCodeLine{// Combined glob file filters}
\DoxyCodeLine{readdirp(\{ root: './test/bed', fileFilter: [ '*.js', '*.json' ] \})}
\DoxyCodeLine{  .on('data', function (entry) \{}
\DoxyCodeLine{    // do something with each JavaScript and Json file entry}
\DoxyCodeLine{  \});}
\DoxyCodeLine{}
\DoxyCodeLine{// Combined negated directory filters}
\DoxyCodeLine{readdirp(\{ root: './test/bed', directoryFilter: [ '!.git', '!*modules' ] \})}
\DoxyCodeLine{  .on('data', function (entry) \{}
\DoxyCodeLine{    // do something with each file entry found outside '.git' or any modules directory}
\DoxyCodeLine{  \});}
\DoxyCodeLine{}
\DoxyCodeLine{// Function directory filter}
\DoxyCodeLine{readdirp(\{ root: './test/bed', directoryFilter: function (di) \{ return di.name.length === 9; \} \})}
\DoxyCodeLine{  .on('data', function (entry) \{}
\DoxyCodeLine{    // do something with each file entry found inside directories whose name has length 9}
\DoxyCodeLine{  \});}
\DoxyCodeLine{}
\DoxyCodeLine{// Limiting depth}
\DoxyCodeLine{readdirp(\{ root: './test/bed', depth: 1 \})}
\DoxyCodeLine{  .on('data', function (entry) \{}
\DoxyCodeLine{    // do something with each file entry found up to 1 subdirectory deep}
\DoxyCodeLine{  \});}
\DoxyCodeLine{}
\DoxyCodeLine{// callback api}
\DoxyCodeLine{readdirp(\{ root: '.' \}, function(fileInfo) \{}
\DoxyCodeLine{   // do something with file entry here}
\DoxyCodeLine{  \}, function (err, res) \{}
\DoxyCodeLine{    // all done, move on or do final step for all file entries here}
\DoxyCodeLine{\}); }

\end{DoxyCode}


Try more examples by following \href{https://github.com/paulmillr/readdirp/blob/master/examples/Readme.md}{\texttt{ instructions}} on how to get going.\hypertarget{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32136}{}\doxysubsection{tests}\label{md_sfcode_node_modules_watchpack_chokidar2_node_modules_readdirp_README_autotoc_md32136}
The \href{https://github.com/paulmillr/readdirp/blob/master/test/readdirp.js}{\texttt{ readdirp tests}} also will give you a good idea on how things work. 