import * as path from 'path' ; import * as protobuf from 'protobufjs' ; import * as walk from 'walkdir' ; let COMMON_PROTO_FILES ; export interface GoogleProtoFilesRootOptions { [ index ] : any ; } export class GoogleProtoFilesRoot extends protobuf . Root { constructor ( options ? ) { super ( options ) ; } static getCommonProtoFiles ( ) { const commonProtoDirs = [ 'api' , path . join ( 'logging' , 'type' ) , path . join ( 'cloud' , 'speech' , 'v1' ) , 'longrunning' , 'protobuf' , 'rpc' , 'type' , ] . map ( dir => path . join ( __dirname , '..' , '..' , 'google' , dir ) ) ; if ( ! COMMON_PROTO_FILES ) { COMMON_PROTO_FILES = commonProtoDirs . map ( dir => { return ( walk . sync ( dir ) as string [ ] ) . filter ( f => path . extname ( f ) === '.proto' ) . map ( path . normalize ) ; } ) . reduce ( ( a , c ) => a . concat ( c ) , [ ] ) ; } return COMMON_PROTO_FILES ; } resolvePath ( _ , includePath , alreadyNormalized ? ) { includePath = path . normalize ( includePath ) ; if ( path . isAbsolute ( includePath ) ) { return includePath ; } const fullIncludePath = path . join ( __dirname , '..' , '..' , includePath ) ; const commonProtoFiles = GoogleProtoFilesRoot . getCommonProtoFiles ( ) ; if ( commonProtoFiles . indexOf ( fullIncludePath ) > - 1 ) { return fullIncludePath ; } return protobuf . util . path . resolve ( _ , includePath , alreadyNormalized ) ; } } export function loadSync ( filename , options ? ) { const root = new GoogleProtoFilesRoot ( ) ; return root . loadSync ( filename , options ) ; } export function load ( filename , options ? ) { const root = new GoogleProtoFilesRoot ( ) ; return root . load ( filename , options ) ; }