import * as fs from 'fs' ; import * as glob from 'glob' ; import * as _ from 'lodash' ; import * as path from 'path' ; import { ComponentAst , getComponentAst } from './componentAst' ; export interface ComponentsConfig { readonly include ? : string [ ] ; readonly exclude ? : string [ ] ; readonly names ? : ComponentNameForFilePath [ ] ; } export interface ComponentTable { [ tag ] : ComponentAst ; } export interface ComponentNameForFilePath { for : string ; use : string ; } const DEFAULT = [ '**' ] ; const IGNORE = [ 'node_modules/**' , 'dist/**' , 'pkg/**' , '**/!(*.html)' , '**/index.html' ] ; export function getComponentTable ( projectName , rootDirectory , componentsConfig , usedConcepts ) { const fpToNameMap = _ . reduce ( _ . get ( componentsConfig , 'names' ) , ( fpToNameMapAccumulator , value ) => { fpToNameMapAccumulator [ value . for ] = value . use ; return fpToNameMapAccumulator ; } , { } ) ; return _ . reduce ( filesToParse ( rootDirectory , componentsConfig ) , ( componentTable , fp ) : ComponentTable => { const componentName = _ . get ( fpToNameMap , fp , getComponentNameFromFilePath ( fp , projectName ) ) ; componentTable [ componentName ] = getComponentAst ( projectName , componentName , usedConcepts , fs . readFileSync ( fp , { encoding : 'utf8' } ) ) ; return componentTable ; } , { } ) ; } returns function getComponentNameFromFilePath ( fp , projectName ) { return projectName + '-' + path . basename ( fp ) . split ( '.' ) [ 0 ] ; } function filesToParse ( rootDirectory , componentsConfig ) { const globs = < string [ ] > _ . get ( componentsConfig , 'include' , DEFAULT ) ; return htmlFilesFromGlobs ( globs , rootDirectory , componentsConfig ) ; } function htmlFilesFromGlobs ( globs , rootDirectory , componentsConfig ) { const globOptions = { cwd : rootDirectory , ignore : [ ... IGNORE , ... < string [ ] > _ . get ( componentsConfig , 'exclude' , [ ] ) ] , nodir : true } ; return _ . flatMap ( globs , ( pattern ) => glob . sync ( pattern , globOptions ) ) ; }