import * as rrd from 'recursive-readdir' ; import * as fs from 'fs' ; import { TextDocument } from 'vscode-languageserver' ; import { Global } from './server' ; export class export class File { static param { ( string | RegExp ) } pattern param { string } startdir returns { Promise < string > } memberOf static async findAsync ( pattern , startdir ? ) { startdir = startdir ? startdir : Global . workspaceRoot ; let matcher = typeof pattern === "string" ? new RegExp ( ( pattern as string ) ) : pattern as RegExp ; return new Promise < string [ ] > ( ( resolve , reject ) => { rrd ( startdir , ( err , files ) => { if ( err ) { reject ( err ) ; return ; } let result = files . filter ( x => x . match ( matcher ) != null ) ; if ( result ) resolve ( result ) ; else reject ( ) ; } ) ; } ) ; } static param { ( RegExp | string ) } pattern param { string } startdir returns { string [ ] } memberOf static find ( pattern , startdir ? ) { startdir = startdir ? startdir : Global . workspaceRoot ; let regex = typeof pattern === "string" ? new RegExp ( pattern as string ) : pattern as RegExp ; return findFilesSync ( regex , startdir ) ; } static getFileName ( path ) { return path . split ( "\\" ) . pop ( ) ; } static open ( path , encoding ? ) { encoding = encoding ? encoding : "utf-8" ; return fs . readFileSync ( path , encoding ) ; } } function findFilesSync ( pattern , startdir ) { let results = [ ] ; let list = fs . readdirSync ( startdir ) ; for ( let file of list ) { file = startdir + '\\' + file ; var stat = fs . statSync ( file ) ; if ( stat && stat . isDirectory ( ) ) results = results . concat ( findFilesSync ( pattern , file ) ) ; else if ( file . match ( pattern ) ) results . push ( file ) ; } return results ; }