import * as exec from 'child_process' ; import * as fse from 'fs-extra' ; import * as os from 'os' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { LogManager } from '../log/logManager' ; import { Configuration } from './configuration' ; export class ScanUtils { public static readonly RESOURCES_DIR : string = ScanUtils . getResourcesDir ( ) ; public static readonly SPAWN_PROCESS_BUFFER_SIZE : number = 104857600 ; public static async scanWithProgress ( scanCbk , title ) { await vscode . window . withProgress ( < vscode . ProgressOptions > { location : vscode . ProgressLocation . Notification , title : title , cancellable : true } , async ( progress , token ) => { await scanCbk ( progress , ( ) => ScanUtils . checkCanceled ( token ) ) ; } ) ; } param workspaceFolders param logManager public static async locatePackageDescriptors ( workspaceFolders , logManager ) { let packageDescriptors = new Map ( ) ; for ( let workspace of workspaceFolders ) { logManager . logMessage ( 'Locating package descriptors in workspace "' + workspace . name + '".' , 'INFO' ) ; let wsPackageDescriptors = await vscode . workspace . findFiles ( { base : workspace . uri . fsPath , pattern : '**/{go.mod,pom.xml,package.json,*.sln,setup.py,requirements*.txt}' } , Configuration . getScanExcludePattern ( workspace ) ) ; for ( let wsPackageDescriptor of wsPackageDescriptors ) { let type = ScanUtils . extractDescriptorTypeFromPath ( wsPackageDescriptor . fsPath ) ; let uri = packageDescriptors . get ( type ) ; if ( ! uri ) { packageDescriptors . set ( type , [ wsPackageDescriptor ] ) ; } else { uri . push ( wsPackageDescriptor ) ; } } } return packageDescriptors ; } private static checkCanceled ( token ) { if ( token . isCancellationRequested ) { throw new ScanCancellationError ( ) ; } } public static getHomePath ( ) { return path . join ( os . homedir ( ) , '.jfrog-vscode-extension' ) ; } static readFileIfExists ( filePath ) { if ( fse . pathExistsSync ( filePath ) ) { return fse . readFileSync ( filePath ) . toString ( ) ; } return undefined ; } static async removeFolder ( folderPath ) { if ( fse . pathExistsSync ( folderPath ) ) { await fse . remove ( folderPath ) ; } } public static executeCmd ( command , cwd ? ) { return exec . execSync ( command , { cwd : cwd , maxBuffer : ScanUtils . SPAWN_PROCESS_BUFFER_SIZE } ) ; } public static setScanInProgress ( state ) { vscode . commands . executeCommand ( 'setContext' , 'scanInProgress' , state ) ; } private static getResourcesDir ( ) { let parent = path . dirname ( __dirname ) ; if ( parent . endsWith ( 'main' ) ) { return path . join ( parent , '..' , '..' , 'resources' ) ; } return path . join ( parent , 'resources' ) ; } param fsPath returns private static extractDescriptorTypeFromPath ( fsPath ) { if ( fsPath . endsWith ( 'go.mod' ) ) { return PackageDescriptorType . GO ; } if ( fsPath . endsWith ( 'pom.xml' ) ) { return PackageDescriptorType . MAVEN ; } if ( fsPath . endsWith ( 'package.json' ) ) { return PackageDescriptorType . NPM ; } if ( fsPath . endsWith ( '.sln' ) ) { return PackageDescriptorType . NUGET ; } return PackageDescriptorType . PYTHON ; } } export enum PackageDescriptorType { GO , MAVEN , NPM , NUGET , PYTHON } export class ScanCancellationError extends Error { message : string = 'Xray scan cancelled' ; }