import * as vscode from "vscode" ; import * as path from "path" ; import * as fs from "fs" ; import * as mkdirp from "mkdirp" ; import * as rimraf from "rimraf" ; import * as utils from "./utils" ; import { FlrFileUtil } from "./util/FlrFileUtil" ; import * as glob from "glob" ; export class FolderManager { static async getAllPubspecFiles ( ) { let flutterMainProjectRootDir = FlrFileUtil . getFlutterMainProjectRootDir ( ) ; if ( flutterMainProjectRootDir ) { let fileRegx = `${ flutterMainProjectRootDir }/**/pubspec.yaml` ; let pubspecFiles = glob . sync ( fileRegx ) ; var result = [ ] ; pubspecFiles . forEach ( ( file ) => { result . push ( [ file , vscode . FileType . File ] ) ; } ) ; return Promise . resolve ( result ) ; } else { return Promise . reject ( ) ; } } static readDirectory ( uri ) { return this . _readDirectory ( uri ) ; } static async _readDirectory ( uri ) { const children = await _ . readdir ( uri . fsPath ) ; const result = [ ] ; for ( let i = 0 ; i < children . length ; i ++ ) { const child = children [ i ] ; const stat = await this . _stat ( path . join ( uri . fsPath , child ) ) ; result . push ( [ child , stat . type ] ) ; } return Promise . resolve ( result ) ; } static stat ( uri ) { return this . _stat ( uri . fsPath ) ; } static async _stat ( path ) { return new FileStat ( await _ . stat ( path ) ) ; } } namespace _ { function handleResult < T > ( resolve , reject , error , result ) { if ( error ) { reject ( massageError ( error ) ) ; } else { resolve ( result ) ; } } function massageError ( error ) { if ( error . code === "ENOENT" ) { return vscode . FileSystemError . FileNotFound ( ) ; } if ( error . code === "EISDIR" ) { return vscode . FileSystemError . FileIsADirectory ( ) ; } if ( error . code === "EEXIST" ) { return vscode . FileSystemError . FileExists ( ) ; } if ( error . code === "EPERM" || error . code === "EACCESS" ) { return vscode . FileSystemError . NoPermissions ( ) ; } return error ; } export function checkCancellation ( token ) { if ( token . isCancellationRequested ) { throw new Error ( "Operation cancelled" ) ; } } export function normalizeNFC ( items ) ; export function normalizeNFC ( items ) ; export function normalizeNFC ( items ) { if ( process . platform !== "darwin" ) { return items ; } if ( Array . isArray ( items ) ) { return items . map ( ( item ) => item . normalize ( "NFC" ) ) ; } return items . normalize ( "NFC" ) ; } export function readdir ( path ) { return new Promise < string [ ] > ( ( resolve , reject ) => { fs . readdir ( path , ( error , children ) => handleResult ( resolve , reject , error , normalizeNFC ( children ) ) ) ; } ) ; } export function stat ( path ) { return new Promise < fs . Stats > ( ( resolve , reject ) => { fs . stat ( path , ( error , stat ) => handleResult ( resolve , reject , error , stat ) ) ; } ) ; } export function readfile ( path ) { return new Promise < Buffer > ( ( resolve , reject ) => { fs . readFile ( path , ( error , buffer ) => handleResult ( resolve , reject , error , buffer ) ) ; } ) ; } export function writefile ( path , content ) { return new Promise < void > ( ( resolve , reject ) => { fs . writeFile ( path , content , ( error ) => handleResult ( resolve , reject , error , void 0 ) ) ; } ) ; } export function exists ( path ) { return new Promise < boolean > ( ( resolve , reject ) => { fs . exists ( path , ( exists ) => handleResult ( resolve , reject , null , exists ) ) ; } ) ; } export function rmrf ( path ) { return new Promise < void > ( ( resolve , reject ) => { rimraf ( path , ( error ) => handleResult ( resolve , reject , error , void 0 ) ) ; } ) ; } export function mkdir ( path ) { return new Promise < void > ( ( resolve , reject ) => { mkdirp ( path , ( error ) => handleResult ( resolve , reject , error , void 0 ) ) ; } ) ; } export function rename ( oldPath , newPath ) { return new Promise < void > ( ( resolve , reject ) => { fs . rename ( oldPath , newPath , ( error ) => handleResult ( resolve , reject , error , void 0 ) ) ; } ) ; } export function unlink ( path ) { return new Promise < void > ( ( resolve , reject ) => { fs . unlink ( path , ( error ) => handleResult ( resolve , reject , error , void 0 ) ) ; } ) ; } } export class FileStat implements vscode . FileStat { constructor ( private fsStat ) { } get type ( ) : vscode . FileType { return this . fsStat . isFile ( ) ? vscode . FileType . File : this . fsStat . isDirectory ( ) ? vscode . FileType . Directory : this . fsStat . isSymbolicLink ( ) ? vscode . FileType . SymbolicLink : vscode . FileType . Unknown ; } get isFile ( ) : boolean | undefined { return this . fsStat . isFile ( ) ; } get isDirectory ( ) : boolean | undefined { return this . fsStat . isDirectory ( ) ; } get isSymbolicLink ( ) : boolean | undefined { return this . fsStat . isSymbolicLink ( ) ; } get size ( ) : number { return this . fsStat . size ; } get ctime ( ) : number { return this . fsStat . ctime . getTime ( ) ; } get mtime ( ) : number { return this . fsStat . mtime . getTime ( ) ; } } export interface Entry { uri : vscode . Uri ; label : string ; type : vscode . FileType ; }