@@ -19,22 +19,29 @@ import jsYaml = require('js-yaml');
1919import GitUrlParse = require( 'git-url-parse' ) ;
2020import cp = require( 'child_process' ) ;
2121import { vsCommand } from '../../vscommand' ;
22+ import * as odo3 from '../../odo3' ;
23+ import { ComponentDescription } from '../../odo/componentTypeDescription' ;
24+ import { ComponentWorkspaceFolder } from '../../odo/workspace' ;
2225
2326let panel : vscode . WebviewPanel ;
2427
2528export class Command {
2629 @vsCommand ( 'openshift.component.importFromGit' )
2730 static async createComponent ( event : any ) {
2831 let alreadyExist : boolean ;
29- let workspacePath , appendedUri : vscode . Uri ;
32+ let workspacePath : vscode . Uri , appendedUri : vscode . Uri ;
3033 let workspaceFolder : vscode . WorkspaceFolder ;
3134 do {
3235 alreadyExist = false ;
3336 workspacePath = await selectWorkspaceFolder ( ) ;
37+ if ( ! workspacePath ) {
38+ return null ;
39+ }
3440 appendedUri = vscode . Uri . joinPath ( workspacePath , event . projectName ) ;
3541 workspaceFolder = vscode . workspace . getWorkspaceFolder ( workspacePath ) ;
36- if ( workspaceFolder && fs . readdirSync ( workspacePath . fsPath ) . length > 0 ) {
37- vscode . window . showErrorMessage ( `Unable to create Component on Workspace Folder: ${ workspacePath } , Please select another folder` ) ;
42+ const isExistingComponent = await existingComponent ( workspacePath ) ;
43+ if ( isExistingComponent ) {
44+ vscode . window . showErrorMessage ( `Unable to create Component inside an existing Component: ${ workspacePath } , Please select another folder` ) ;
3845 alreadyExist = true ;
3946 } else if ( fs . existsSync ( appendedUri . fsPath ) && fs . readdirSync ( appendedUri . fsPath ) . length > 0 ) {
4047 vscode . window . showErrorMessage ( `Folder ${ appendedUri . fsPath . substring ( appendedUri . fsPath . lastIndexOf ( '\\' ) + 1 ) } already exist
@@ -86,7 +93,6 @@ export class Command {
8693 }
8794}
8895
89-
9096async function gitImportMessageListener ( event : any ) : Promise < any > {
9197 switch ( event ?. action ) {
9298 case 'validateGitURL' :
@@ -210,8 +216,8 @@ function validateGitURL(event: any) {
210216 try {
211217 const parse = GitUrlParse ( event . param ) ;
212218 const isGitRepo = isGitURL ( parse . host ) ;
213- if ( ! isGitRepo ) {
214- throw 'Invalid Git URL' ;
219+ if ( ! isGitRepo ) {
220+ throw 'Invalid Git URL' ;
215221 }
216222 if ( parse . organization !== '' && parse . name !== '' ) {
217223 panel . webview . postMessage ( {
@@ -299,6 +305,38 @@ function showError(event: any): void {
299305}
300306
301307function isGitURL ( host : string ) : boolean {
302- return [ 'github.com' , 'bitbucket.org' , 'gitlab.com' ] . includes ( host ) ;
308+ return [ 'github.com' , 'bitbucket.org' , 'gitlab.com' ] . includes ( host ) ;
309+ }
310+
311+ async function existingComponent ( uri : vscode . Uri ) : Promise < boolean > {
312+ let worksapceFolder = vscode . workspace . getWorkspaceFolder ( uri ) ;
313+ if ( worksapceFolder ?. uri ) {
314+ const workspaceSubDirs = fs . readdirSync ( worksapceFolder . uri . fsPath , { withFileTypes : true } )
315+ . filter ( dirent => dirent . isDirectory ( ) )
316+ . map ( dirent => dirent . name ) . map ( ( subDir ) => vscode . Uri . joinPath ( worksapceFolder . uri , subDir ) ) ;
317+ const components = await getComponents ( workspaceSubDirs ) ;
318+ if ( components ?. length > 0 ) {
319+ const sameFolder = components . filter ( component => component . contextPath === uri . fsPath || uri . fsPath . indexOf ( component . contextPath ) !== - 1 ) ;
320+ if ( sameFolder && sameFolder . length > 0 ) {
321+ return true ;
322+ } else {
323+ return false ;
324+ }
325+ }
326+ }
327+ return false ;
303328}
304329
330+ async function getComponents ( folders : vscode . Uri [ ] ) : Promise < ComponentWorkspaceFolder [ ] > {
331+ const descriptions = folders . map (
332+ ( folder ) => odo3 . newInstance ( ) . describeComponent ( folder . fsPath )
333+ . then ( ( component : ComponentDescription ) => {
334+ return {
335+ contextPath : folder . fsPath ,
336+ component
337+ }
338+ } )
339+ ) ;
340+ const results = await Promise . all ( descriptions ) ;
341+ return results . filter ( ( compFolder ) => ! ! compFolder . component ) ;
342+ }
0 commit comments