import * as globby from 'globby' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { IActionContext , IAzureQuickPickItem } from 'vscode-azureextensionui' ; import { localize } from '../localize' ; import * as fsUtils from './fs' ; export function isMultiRootWorkspace ( ) { return ! ! vscode . workspace . workspaceFolders && vscode . workspace . workspaceFolders . length > 0 && vscode . workspace . name !== vscode . workspace . workspaceFolders [ 0 ] . name ; } /**
 * Alternative to `vscode.workspace.findFiles` which always returns an empty array if no workspace is open
 */ export async function findFiles ( base , pattern ) { const posixBase = path . posix . normalize ( typeof base === 'string' ? base : base . uri . fsPath ) . replace ( /\\/g , '/' ) ; const escapedBase = escapeCharacters ( posixBase ) ; const fullPattern = path . posix . join ( escapedBase , pattern ) ; return ( await globby ( fullPattern ) ) . map ( s => vscode . Uri . file ( s ) ) ; } function escapeCharacters ( nonPattern ) { return nonPattern . replace ( /[$^*+?()[\]]/g , '\\$&' ) ; } export async function selectWorkspaceFolder ( context , placeHolder , getSubPath ? ) { return await selectWorkspaceItem ( context , placeHolder , { canSelectFiles : false , canSelectFolders : true , canSelectMany : false , defaultUri : vscode . workspace . workspaceFolders && vscode . workspace . workspaceFolders . length > 0 ? vscode . workspace . workspaceFolders [ 0 ] . uri : undefined , openLabel : localize ( 'select' , 'Select' ) } , getSubPath ) ; } export async function selectWorkspaceFile ( context , placeHolder , getSubPath ? ) { let defaultUri ; if ( vscode . workspace . workspaceFolders && vscode . workspace . workspaceFolders . length > 0 && getSubPath ) { const firstFolder = vscode . workspace . workspaceFolders [ 0 ] ; const subPath = await getSubPath ( firstFolder ) ; if ( subPath ) { defaultUri = vscode . Uri . file ( path . join ( firstFolder . uri . fsPath , subPath ) ) ; } } return await selectWorkspaceItem ( context , placeHolder , { canSelectFiles : true , canSelectFolders : false , canSelectMany : false , defaultUri : defaultUri , openLabel : localize ( 'select' , 'Select' ) } , getSubPath ) ; } export async function selectWorkspaceItem ( context , placeHolder , options , getSubPath ? ) { let folder ; if ( vscode . workspace . workspaceFolders ) { const folderPicks = await Promise . all ( vscode . workspace . workspaceFolders . map ( async ( f ) => { let subpath ; if ( getSubPath ) { subpath = await getSubPath ( f ) ; } const fsPath = subpath ? path . join ( f . uri . fsPath , subpath ) : f . uri . fsPath ; return { label : path . basename ( fsPath ) , description : fsPath , data : fsPath } ; } ) ) ; folderPicks . push ( { label : localize ( 'browse' , '$(file-directory) Browse...' ) , description : '' , data : undefined } ) ; folder = await context . ui . showQuickPick ( folderPicks , { placeHolder } ) ; } return folder && folder . data ? folder . data : ( await context . ui . showOpenDialog ( options ) ) [ 0 ] . fsPath ; } export function getContainingWorkspace ( fsPath ) { const openFolders = vscode . workspace . workspaceFolders || [ ] ; return openFolders . find ( ( f ) : boolean => { return fsUtils . isPathEqual ( f . uri . fsPath , fsPath ) || fsUtils . isSubpath ( f . uri . fsPath , fsPath ) ; } ) ; }