@@ -12,7 +12,6 @@ import { DetectedServiceData, DetectedStrategy, detectImportStrategies } from '.
1212import { ComponentTypesView } from '../../registriesView' ;
1313import { ComponentTypeDescription } from '../../odo/componentType' ;
1414import { Response } from '../../git-import/types' ;
15- import { Uri } from 'vscode' ;
1615import { Component } from '../../openshift/component' ;
1716import OpenShiftItem from '../../openshift/openshiftItem' ;
1817import { selectWorkspaceFolder } from '../../util/workspace' ;
@@ -21,6 +20,9 @@ import GitUrlParse = require('git-url-parse');
2120import treeKill = require( 'tree-kill' )
2221import cp = require( 'child_process' ) ;
2322import { vsCommand } from '../../vscommand' ;
23+ import * as odo3 from '../../odo3' ;
24+ import { ComponentDescription } from '../../odo/componentTypeDescription' ;
25+ import { ComponentWorkspaceFolder } from '../../odo/workspace' ;
2426
2527let panel : vscode . WebviewPanel ;
2628let childProcess : cp . ChildProcess ;
@@ -35,9 +37,28 @@ interface CloneProcess {
3537export class Command {
3638 @vsCommand ( 'openshift.component.importFromGit' )
3739 static async createComponent ( event : any ) {
38- const workspacePath = await selectWorkspaceFolder ( ) ;
39- appendedUri = Uri . joinPath ( workspacePath , event . projectName ) ;
40- panel ?. webview . postMessage ( {
40+ let alreadyExist : boolean ;
41+ let workspacePath : vscode . Uri , appendedUri : vscode . Uri ;
42+ let workspaceFolder : vscode . WorkspaceFolder ;
43+ do {
44+ alreadyExist = false ;
45+ workspacePath = await selectWorkspaceFolder ( ) ;
46+ if ( ! workspacePath ) {
47+ return null ;
48+ }
49+ appendedUri = vscode . Uri . joinPath ( workspacePath , event . projectName ) ;
50+ workspaceFolder = vscode . workspace . getWorkspaceFolder ( workspacePath ) ;
51+ const isExistingComponent = await existingComponent ( workspacePath ) ;
52+ if ( isExistingComponent ) {
53+ vscode . window . showErrorMessage ( `Unable to create Component inside an existing Component: ${ workspacePath } , Please select another folder` ) ;
54+ alreadyExist = true ;
55+ } else if ( fs . existsSync ( appendedUri . fsPath ) && fs . readdirSync ( appendedUri . fsPath ) . length > 0 ) {
56+ vscode . window . showErrorMessage ( `Folder ${ appendedUri . fsPath . substring ( appendedUri . fsPath . lastIndexOf ( '\\' ) + 1 ) } already exist
57+ at the selected location: ${ appendedUri . fsPath . substring ( 0 , appendedUri . fsPath . lastIndexOf ( '\\' ) ) } ` ) ;
58+ alreadyExist = true ;
59+ }
60+ } while ( alreadyExist ) ;
61+ panel . webview . postMessage ( {
4162 action : 'cloneStarted'
4263 } ) ;
4364 const cloneProcess : CloneProcess = await clone ( event . gitURL , appendedUri . fsPath ) ;
@@ -49,7 +70,7 @@ export class Command {
4970 if ( ! event . isDevFile ) {
5071 panel . webview . postMessage ( {
5172 action : 'start_create_component'
52- } )
73+ } ) ;
5374 try {
5475 await Component . createFromRootWorkspaceFolder ( appendedUri , undefined ,
5576 {
@@ -82,6 +103,9 @@ export class Command {
82103 } ) ;
83104 vscode . window . showInformationMessage ( 'Selected Component added to the workspace.' ) ;
84105 }
106+ if ( ! workspaceFolder ) {
107+ vscode . workspace . updateWorkspaceFolders ( vscode . workspace . workspaceFolders ? vscode . workspace . workspaceFolders . length : 0 , null , { uri : appendedUri } ) ;
108+ }
85109 }
86110}
87111
@@ -288,7 +312,7 @@ function validateDevFilePath(event: any) {
288312 let validationMessage = OpenShiftItem . emptyName ( `Required ${ event . param } ` , event . param . trim ( ) ) ;
289313 if ( ! validationMessage ) validationMessage = OpenShiftItem . validateFilePath ( `Not matches ^[a-z]:((\/|\\\\)[a-zA-Z0-9_ \\-]+)+\\.yaml$` , event . param ) ;
290314 if ( ! validationMessage && event . param !== 'devfile.yaml' && event . param !== 'devfile.yml' ) {
291- const uri = Uri . parse ( event . param ) ;
315+ const uri = vscode . Uri . parse ( event . param ) ;
292316 const devFileLocation = path . join ( uri . fsPath ) ;
293317 validationMessage = fs . existsSync ( devFileLocation ) ? null : 'devfile not available on the given path' ;
294318 }
@@ -318,6 +342,38 @@ function isGitURL(host: string): boolean {
318342 return [ 'github.com' , 'bitbucket.org' , 'gitlab.com' ] . includes ( host ) ;
319343}
320344
345+ async function existingComponent ( uri : vscode . Uri ) : Promise < boolean > {
346+ let worksapceFolder = vscode . workspace . getWorkspaceFolder ( uri ) ;
347+ if ( worksapceFolder ?. uri ) {
348+ const workspaceSubDirs = fs . readdirSync ( worksapceFolder . uri . fsPath , { withFileTypes : true } )
349+ . filter ( dirent => dirent . isDirectory ( ) )
350+ . map ( dirent => dirent . name ) . map ( ( subDir ) => vscode . Uri . joinPath ( worksapceFolder . uri , subDir ) ) ;
351+ const components = await getComponents ( workspaceSubDirs ) ;
352+ if ( components ?. length > 0 ) {
353+ const sameFolder = components . filter ( component => component . contextPath === uri . fsPath || uri . fsPath . indexOf ( component . contextPath ) !== - 1 ) ;
354+ if ( sameFolder && sameFolder . length > 0 ) {
355+ return true ;
356+ } else {
357+ return false ;
358+ }
359+ }
360+ }
361+ return false ;
362+ }
363+
364+ async function getComponents ( folders : vscode . Uri [ ] ) : Promise < ComponentWorkspaceFolder [ ] > {
365+ const descriptions = folders . map (
366+ ( folder ) => odo3 . newInstance ( ) . describeComponent ( folder . fsPath )
367+ . then ( ( component : ComponentDescription ) => {
368+ return {
369+ contextPath : folder . fsPath ,
370+ component
371+ }
372+ } )
373+ ) ;
374+ const results = await Promise . all ( descriptions ) ;
375+ return results . filter ( ( compFolder ) => ! ! compFolder . component ) ;
376+ }
321377function deleteDirectory ( dir : string ) {
322378 return new Promise < void > ( function ( _resolve , reject ) {
323379 fs . rmdir ( dir , { recursive : true } , err => {
0 commit comments