import fsExtra from 'fs-extra' ; import nodePath from 'path' ; import { WriteStream , ReadStream } from 'fs' ; import { matchGlob } from './utils' ; import glob from 'glob' ; export function throwErrorWhenNotExists ( absPath ) { if ( ! fsExtra . existsSync ( absPath ) ) { throw new Error ( `${ absPath } has not exists` ) ; } } export function isDirectory ( dir ) { return fsExtra . statSync ( dir ) . isDirectory ( ) ; } export interface GetDirsOrFilesOptions { /** Include .dot files in normal matches */ dot ? : boolean ; /** Exclude files in normal matches */ exclude ? : string | string [ ] ; } const DEFAULT_OPTIONS = { dot : false } ; export async function getDirsAndFiles ( path , options ? ) { options = { ... DEFAULT_OPTIONS , ... options } ; const dirs = await fsExtra . readdir ( path ) ; return dirs . filter ( dir => { if ( options . exclude && matchGlob ( dir , options . exclude ) ) { return false ; } if ( options . dot ) { return true ; } else { return ! dir . startsWith ( '.' ) ; } } ) ; } export async function getDirs ( path , options ? ) { const dirs = await getDirsAndFiles ( path , options ) ; return dirs . filter ( dir => { return isDirectory ( nodePath . resolve ( path , dir ) ) ; } ) ; } export function globSync ( pattern , options ? ) { options = { ... DEFAULT_OPTIONS , ... options } ; const result = glob . sync ( pattern , { dot : options . dot , root : options . root } ) ; return result . filter ( dir => { if ( options . exclude && matchGlob ( dir , options . exclude ) ) { return false ; } return true ; } ) ; } export async function readFileContent ( filePath , encoding = 'UTF-8' ) { const result = await fsExtra . readFile ( filePath , encoding ) ; return result ; } export * from 'fs-extra' ;