import * as vscode from "vscode" ; import * as path from "path" ; import * as glob from "glob" ; import * as fs from "fs" ; import * as yaml from "js-yaml" ; import * as FlrConstant from "../FlrConstant" ; export class FlrFileUtil { public static getFlutterMainProjectRootDir ( ) { let folders = vscode . workspace . workspaceFolders ; if ( folders ) { let root = folders [ 0 ] . uri . fsPath ; return root ; } return undefined ; } public static getFlutterSubProjectRootDirs ( flutterMainProjectRootDir ) { var flutterSubProjectRootDirArray = new Array ( ) ; let fileRegx = `${ flutterMainProjectRootDir }/*/pubspec.yaml` ; glob . sync ( fileRegx ) . forEach ( ( file ) => { let fileDir = path . dirname ( file ) ; flutterSubProjectRootDirArray . push ( fileDir ) ; } ) ; return flutterSubProjectRootDirArray ; } public static getPubspecFilePath ( flutterProjectDir ) { let filePath = flutterProjectDir + "/pubspec.yaml" ; return filePath ; } public static getFlrResourceDirs ( flutterProjectRootDir ) { var assetsResourceDirs = new Array ( ) ; var fontsResourceDirs = new Array ( ) ; try { let pubspecFilePath = this . getPubspecFilePath ( flutterProjectRootDir ) ; let fileContents = fs . readFileSync ( pubspecFilePath , "utf8" ) ; let data = yaml . safeLoad ( fileContents ) ; let flr = data [ "flr" ] ; if ( flr !== null && flr !== undefined ) { let assets = flr [ "assets" ] ; let fonts = flr [ "fonts" ] ; if ( assets !== null && assets !== undefined ) { let assetsRelativeResourceDirs = Object . values < string > ( assets ) ; assetsRelativeResourceDirs . forEach ( ( relativeResourceDir ) => { let resourceDir = flutterProjectRootDir + "/" + relativeResourceDir ; assetsResourceDirs . push ( resourceDir ) ; } ) ; } if ( fonts !== null && fonts !== undefined ) { let fontsRelativeResourceDirs = Object . values < string > ( fonts ) ; fontsRelativeResourceDirs . forEach ( ( relativeResourceDir ) => { let resourceDir = flutterProjectRootDir + "/" + relativeResourceDir ; fontsResourceDirs . push ( resourceDir ) ; } ) ; } } } catch ( e ) { let flutterMainProjectRootDir = this . getFlutterMainProjectRootDir ( ) ; var pubspecFile = "pubspec.yaml" ; if ( flutterProjectRootDir !== flutterMainProjectRootDir ) { let subProjectRootDirname = path . basename ( flutterProjectRootDir ) ; pubspecFile = `${ subProjectRootDirname }/${ pubspecFile }` ; } if ( e instanceof yaml . YAMLException ) { let msg = `${ pubspecFile } is damaged with syntax error: \n ${ e . message }` ; vscode . window . showErrorMessage ( msg ) ; } else { let msg = `${ pubspecFile } is damaged with some error: \n ${ e }` ; console . log ( msg ) ; } } let resourceDirResultTuple = [ assetsResourceDirs , fontsResourceDirs ] ; return resourceDirResultTuple ; } public static getFlrRelativeResourceDirs ( flutterProjectRootDir ) { var assetsRelativeResourceDirs = new Array ( ) ; var fontsRelativeResourceDirs = new Array ( ) ; try { let pubspecFilePath = this . getPubspecFilePath ( flutterProjectRootDir ) ; let fileContents = fs . readFileSync ( pubspecFilePath , "utf8" ) ; let data = yaml . safeLoad ( fileContents ) ; let flr = data [ "flr" ] ; if ( flr !== null && flr !== undefined ) { let assets = flr [ "assets" ] ; let fonts = flr [ "fonts" ] ; if ( assets !== null && assets !== undefined ) { assetsRelativeResourceDirs = Object . values < string > ( assets ) ; } if ( fonts !== null && fonts !== undefined ) { fontsRelativeResourceDirs = Object . values < string > ( fonts ) ; } } } catch ( e ) { let flutterMainProjectRootDir = this . getFlutterMainProjectRootDir ( ) ; var pubspecFile = "pubspec.yaml" ; if ( flutterProjectRootDir !== flutterMainProjectRootDir ) { let subProjectRootDirname = path . basename ( flutterProjectRootDir ) ; pubspecFile = `${ subProjectRootDirname }/${ pubspecFile }` ; } if ( e instanceof yaml . YAMLException ) { let msg = `${ pubspecFile } is damaged with syntax error: \n ${ e . message }` ; vscode . window . showErrorMessage ( msg ) ; } else { let msg = `${ pubspecFile } is damaged with some error: \n ${ e }` ; console . log ( msg ) ; } } let resourceDirResultTuple = [ assetsRelativeResourceDirs , fontsRelativeResourceDirs , ] ; return resourceDirResultTuple ; } public static loadPubspecConfigFromFile ( pubspecFile ) { let fileContents = fs . readFileSync ( pubspecFile , "utf8" ) ; let pubspecConfig = yaml . safeLoad ( fileContents ) ; return pubspecConfig ; } public static dumpPubspecConfigToFile ( pubspecConfig , pubspecFile ) { try { fs . writeFileSync ( pubspecFile , yaml . dump ( pubspecConfig , { indent : 2 , noArrayIndent : true , lineWidth : Infinity , } ) , "utf8" ) ; } catch ( e ) { console . log ( e ) ; } } public static formatPubspecFile ( pubspecFile ) { try { let pubspecConfig = this . loadPubspecConfigFromFile ( pubspecFile ) ; this . dumpPubspecConfigToFile ( pubspecConfig , pubspecFile ) ; } catch ( _ ) { } } public static isPackageProjectType ( flutterProjectDir ) { let metadataFilePath = flutterProjectDir + "/.metadata" ; if ( fs . existsSync ( metadataFilePath ) ) { let fileContents = fs . readFileSync ( metadataFilePath , "utf8" ) ; let metadataConfig = yaml . safeLoad ( fileContents ) ; let projectType = metadataConfig [ "project_type" ] ; if ( projectType === "package" || projectType === "plugin" ) { return true ; } } else { let pubspecFile = this . getPubspecFilePath ( flutterProjectDir ) ; let pubspecConfig = this . loadPubspecConfigFromFile ( pubspecFile ) ; if ( pubspecConfig . hasOwnProperty ( "author" ) ) { return true ; } } return false ; } public static isLegalResourceFile ( file ) { let fileBasenameWithoutExtension = path . parse ( file ) . name ; let regex = /^[a-zA-Z0-9_\+\-\.·!@&$￥]+$/ ; return regex . test ( fileBasenameWithoutExtension ) ; } public static isNonSvgImageResourceFile ( file ) { let fileExtName = path . extname ( file ) . toLowerCase ( ) ; return FlrConstant . NON_SVG_IMAGE_FILE_TYPES . includes ( fileExtName ) ; } public static isSvgImageResourceFile ( file ) { let fileExtName = path . extname ( file ) . toLowerCase ( ) ; return FlrConstant . SVG_IMAGE_FILE_TYPES . includes ( fileExtName ) ; } public static isImageResourceFile ( file ) { let fileExtName = path . extname ( file ) . toLowerCase ( ) ; return FlrConstant . IMAGE_FILE_TYPES . includes ( fileExtName ) ; } public static isTextResourceFile ( file ) { let fileExtName = path . extname ( file ) . toLowerCase ( ) ; return FlrConstant . TEXT_FILE_TYPES . includes ( fileExtName ) ; } public static isFontResourceFile ( file ) { let fileExtName = path . extname ( file ) . toLowerCase ( ) ; return FlrConstant . FONT_FILE_TYPES . includes ( fileExtName ) ; } public static findImageFiles ( resourceDir ) { var legalImageFileArray = new Array ( ) ; var illegalImageFileArray = new Array ( ) ; let patternFileTypes = FlrConstant . IMAGE_FILE_TYPES . join ( "," ) ; let regx = `${ resourceDir }/**/*{${ patternFileTypes }}` ; let files = glob . sync ( regx ) ; files . forEach ( ( file ) => { if ( this . isLegalResourceFile ( file ) ) { legalImageFileArray . push ( file ) ; } else { illegalImageFileArray . push ( file ) ; } } ) ; return [ legalImageFileArray , illegalImageFileArray ] ; } public static findTextFiles ( resourceDir ) { var legalTextFileArray = new Array ( ) ; var illegalTextFileArray = new Array ( ) ; let patternFileTypes = FlrConstant . TEXT_FILE_TYPES . join ( "," ) ; let regx = `${ resourceDir }/**/*{${ patternFileTypes }}` ; let files = glob . sync ( regx ) ; files . forEach ( ( file ) => { if ( this . isLegalResourceFile ( file ) ) { legalTextFileArray . push ( file ) ; } else { illegalTextFileArray . push ( file ) ; } } ) ; return [ legalTextFileArray , illegalTextFileArray ] ; } public static findTopChildDirs ( resourceDir ) { var resourceDirFileArray = new Array ( ) ; let patternFileTypes = FlrConstant . TEXT_FILE_TYPES . join ( "," ) ; let regx = `${ resourceDir }/*` ; let files = glob . sync ( regx ) ; files . forEach ( ( file ) => { if ( fs . statSync ( file ) . isDirectory ( ) ) { resourceDirFileArray . push ( file ) ; } } ) ; return resourceDirFileArray ; } public static findFontFilesInFontFamilyDir ( fontFamilyDir ) { var legalFontFileArray = new Array ( ) ; var illegalFontFileArray = new Array ( ) ; let patternFileTypes = FlrConstant . FONT_FILE_TYPES . join ( "," ) ; let regx = `${ fontFamilyDir }/**/*{${ patternFileTypes }}` ; let files = glob . sync ( regx ) ; files . forEach ( ( file ) => { if ( this . isLegalResourceFile ( file ) ) { legalFontFileArray . push ( file ) ; } else { illegalFontFileArray . push ( file ) ; } } ) ; return [ legalFontFileArray , illegalFontFileArray ] ; } }