import * as fs from 'fs' ; import { _ } from 'streamline-runtime' ; import * as node from './node' ; import * as generic from './generic' ; import { Reader } from '../reader' ; import { Writer } from '../writer' ; export const text = { reader ( path , encoding ? ) { return node . reader ( fs . createReadStream ( path , { encoding : encoding || 'utf8' } ) ) ; } , writer ( path , encoding ? ) { return node . writer ( fs . createWriteStream ( path , { encoding : encoding || 'utf8' } ) ) ; } } ; export const binary = { reader ( path ) { return node . reader ( fs . createReadStream ( path ) ) ; } , writer ( path ) { return node . writer ( fs . createWriteStream ( path ) ) ; } } ; export interface ListOptions { recurse ? : boolean | 'preorder' | 'postorder' ; accept ? : ( _ : _ , entry : ListEntry ) => boolean ; } export interface ListEntry { path : string ; name : string ; depth : number ; stat : fs . Stats ; } export function list ( path , options ? ) { var recurse , accept ; if ( options && typeof options === 'object' ) { recurse = options . recurse || false ; accept = options . accept ; } else { recurse = arguments [ 1 ] ; accept = arguments [ 2 ] ; } const postorder = recurse === 'postorder' ; return generic . empty . reader . transform ( ( _ , reader , writer ) => { function process ( _ , p , name , depth ) { const stat = fs . stat ( p , _ ) ; const entry = { path : p , name : name , depth : depth , stat : stat , } ; if ( accept && ! accept ( _ , entry ) ) return ; if ( ( recurse || depth === 1 ) && ! postorder ) writer . write ( _ , entry ) ; if ( ( recurse || depth === 0 ) && stat . isDirectory ( ) ) fs . readdir ( p , _ ) . forEach_ ( _ , ( _ , pp ) => { process ( _ , p + '/' + pp , pp , depth + 1 ) ; } ) ; if ( ( recurse || depth === 1 ) && postorder ) writer . write ( _ , entry ) ; } process ( _ , path , path . substring ( path . lastIndexOf ( '/' ) + 1 ) , 0 ) ; } ) ; }