import Promise from "../utils/promise" ; import error from "../utils/error" ; import lookupStructureFile from "./lookupStructureFile" ; param { FS } fs param { ParsableFile } file param { string } type return { Promise < Array < String , List | Map > > } function parseFile ( fs , file , type ) { const filepath = file . getPath ( ) ; const parser = file . getParser ( ) ; if ( ! parser ) { return Promise . reject ( error . FileNotParsableError ( { filename : filepath , } ) ) ; } return fs . readAsString ( filepath ) . then ( ( content ) => { if ( type === "readme" ) { return parser . parseReadme ( content ) ; } else if ( type === "glossary" ) { return parser . parseGlossary ( content ) ; } else if ( type === "summary" ) { return parser . parseSummary ( content ) ; } else if ( type === "langs" ) { return parser . parseLanguages ( content ) ; } else { throw new Error ( `Parsing invalid type "${ type }"` ) ; } } ) . then ( ( result ) => { return [ file , result ] ; } ) ; } param { Book } book param { string } type return { Promise < List | Map > } function parseStructureFile ( book , type ) { const fs = book . getContentFS ( ) ; return lookupStructureFile ( book , type ) . then ( ( file ) => { if ( ! file ) return [ undefined , undefined ] ; return parseFile ( fs , file , type ) ; } ) ; } export default parseStructureFile ;