import * as fs from 'fs' ; import { Reader } from './reader' ; export class FileSystemReader implements Reader { constructor ( private readonly directory ) { } public async list ( ) { return new Promise < string [ ] > ( ( resolve , reject ) => { fs . readdir ( this . directory , ( error , filenames ) => { if ( error ) { reject ( error ) ; } else { resolve ( filenames ) ; } } ) ; } ) ; } public async read ( name ) { return new Promise < string > ( ( resolve , reject ) => { fs . readFile ( `${ this . directory }/${ name }` , ( error , data ) => { if ( error ) { reject ( error ) ; } else { resolve ( data . toString ( ) ) ; } } ) ; } ) ; } public async readAnyOf ( filenames ) { try { for ( const file of filenames ) { return await this . read ( file ) ; } } catch ( err ) { return filenames . length > 0 ? await this . readAnyOf ( filenames . slice ( 1 , filenames . length ) ) : undefined ; } } }