import { execSync } from 'child_process' ; import { ComponentDetails } from 'jfrog-client-js' ; import * as path from 'path' ; import Set from 'typescript-collections/dist/lib/Set' ; import * as vscode from 'vscode' ; import { FocusType } from '../focus/abstractFocus' ; import { NpmTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesRoot/npmTree' ; import { DependenciesTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesTreeNode' ; import { TreesManager } from '../treeDataProviders/treesManager' ; export class NpmUtils { public static readonly DOCUMENT_SELECTOR : vscode . DocumentSelector = { scheme : 'file' , pattern : '**/package.json' } ; public static readonly PKG_TYPE : string = 'npm' ; param document public static getDependenciesPos ( document ) { let res = [ ] ; let packageJsonContent = document . getText ( ) ; let dependenciesMatch = packageJsonContent . match ( '"((devD)|(d))ependencies"s*:s*' ) ; if ( ! dependenciesMatch ) { return res ; } res . push ( document . positionAt ( < number > dependenciesMatch . index ) ) ; res . push ( new vscode . Position ( res [ 0 ] . line , res [ 0 ] . character + dependenciesMatch [ 0 ] . length ) ) ; return res ; } param document param dependenciesTreeNode public static getDependencyPos ( document , dependenciesTreeNode , focusType ) { let res = [ ] ; let packageJsonContent = document . getText ( ) ; let dependencyMatch = packageJsonContent . match ( '("' + dependenciesTreeNode . generalInfo . artifactId + '"\\s*:\\s*).*"' ) ; if ( ! dependencyMatch ) { return res ; } switch ( focusType ) { case FocusType . Dependency : res . push ( document . positionAt ( < number > dependencyMatch . index ) ) ; break ; case FocusType . DependencyVersion : res . push ( document . positionAt ( < number > dependencyMatch . index + dependencyMatch [ 1 ] . length ) ) ; break ; } res . push ( new vscode . Position ( res [ 0 ] . line , res [ 0 ] . character + dependencyMatch [ 0 ] . length ) ) ; return res ; } param packageJsons param componentsToScan param treesManager param parent param quickScan public static async createDependenciesTrees ( packageJsons , componentsToScan , treesManager , parent , quickScan ) { if ( ! packageJsons ) { treesManager . logManager . logMessage ( 'No package.json files found in workspaces.' , 'DEBUG' ) ; return ; } if ( ! NpmUtils . verifyNpmInstalled ( ) ) { vscode . window . showErrorMessage ( 'Could not scan npm project dependencies, because npm CLI is not in the PATH.' ) ; return ; } treesManager . logManager . logMessage ( 'package.json files to scan: [' + packageJsons . toString ( ) + ']' , 'DEBUG' ) ; for ( let packageJson of packageJsons ) { let dependenciesTreeNode = new NpmTreeNode ( path . dirname ( packageJson . fsPath ) , componentsToScan , treesManager , parent ) ; dependenciesTreeNode . refreshDependencies ( quickScan ) ; } } public static verifyNpmInstalled ( ) { try { execSync ( 'npm --version' ) ; } catch ( error ) { return false ; } return true ; } public static getDependencyScope ( dep ) { if ( dep !== '' && dep [ 0 ] === '@' ) { return dep . substring ( 1 , dep . indexOf ( '/' ) ) ; } return '' ; } } export class ScopedNpmProject { private _projectName : string = '' ; private _projectVersion : string = '' ; private _dependencies : any ; private _scope : NpmGlobalScopes ; constructor ( scope ) { this . _scope = scope ; } public get projectName ( ) : string { return this . _projectName ; } public set projectName ( projectName ) { this . _projectName = projectName ; } public get projectVersion ( ) : string { return this . _projectVersion ; } public set projectVersion ( projectVersion ) { this . _projectVersion = projectVersion ; } public get dependencies ( ) { return this . _dependencies ; } public loadProjectDetails ( lsOutput ) { this . _projectName = lsOutput . name ; this . _projectVersion = lsOutput . version ; this . _dependencies = lsOutput . dependencies ; } public get scope ( ) : NpmGlobalScopes { return this . _scope ; } } export enum NpmGlobalScopes { PRODUCTION = 'prod' , DEVELOPMENT = 'dev' }