@@ -3,6 +3,7 @@ import semver from "semver";
33import {
44 appendPath ,
55 createFsResource ,
6+ getAlternateName ,
67 getIdFromPath ,
78 getNameOfResource ,
89 getNewNameIfQuickCreate ,
@@ -78,21 +79,22 @@ import { isEmpty } from "lodash";
7879import { HandleError } from "./decorators/handle-error.decorator" ;
7980import { FsIgnoreManager } from "./fsIgnore-manager" ;
8081import { fileIndex } from "./file-index" ;
81-
8282export class ResourceNotFound extends Error {
8383 constructor ( message : string ) {
8484 super ( message ) ;
8585 }
8686}
87-
8887export class FsManager {
8988 private rootPath : string ;
9089
9190 private config : Static < typeof Config > ;
9291
9392 private fsIgnoreManager : FsIgnoreManager ;
9493
95- constructor ( rootPath : string , readonly exposedWorkspacePaths : Map < string , unknown > ) {
94+ constructor (
95+ rootPath : string ,
96+ readonly exposedWorkspacePaths : Map < string , unknown >
97+ ) {
9698 this . rootPath = getNormalizedPath ( rootPath ) ;
9799 this . config = { } as Static < typeof Config > ;
98100 this . fsIgnoreManager = new FsIgnoreManager ( this . rootPath , this . config ) ;
@@ -111,10 +113,10 @@ export class FsManager {
111113 // Calling these methods to initialise file index
112114 await this . getAllRecords ( ) ;
113115 await this . getAllEnvironments ( ) ;
114-
115116 } catch ( error ) {
116117 throw new Error (
117- `Failed to initialize FsManager: ${ error instanceof Error ? error . message : String ( error )
118+ `Failed to initialize FsManager: ${
119+ error instanceof Error ? error . message : String ( error )
118120 } `
119121 ) ;
120122 }
@@ -474,7 +476,9 @@ export class FsManager {
474476 } ) {
475477 const path = fileIndex . getPath ( params . id ) ;
476478 if ( ! path ) {
477- throw new ResourceNotFound ( `Resource ${ params . id } of type ${ params . type } not found!` ) ;
479+ throw new ResourceNotFound (
480+ `Resource ${ params . id } of type ${ params . type } not found!`
481+ ) ;
478482 }
479483 return createFsResource ( {
480484 path,
@@ -562,19 +566,18 @@ export class FsManager {
562566 return envFolderPath ;
563567 }
564568
565-
566569 private async getFileResourceWithNameVerification ( params : {
567- name ?: string ,
568- id : string ,
570+ name ?: string ;
571+ id : string ;
569572 } ) : Promise < FileSystemResult < FileResource > > {
570573 const existingFile = this . createResource ( {
571574 id : params . id ,
572- type : ' file' ,
575+ type : " file" ,
573576 } ) ;
574577
575578 if ( ! params . name ) {
576579 return {
577- type : ' success' ,
580+ type : " success" ,
578581 content : existingFile ,
579582 } ;
580583 }
@@ -583,7 +586,7 @@ export class FsManager {
583586 const sanitizedName = this . generateFileName ( params . name ) ;
584587 if ( existingFileName === sanitizedName ) {
585588 return {
586- type : ' success' ,
589+ type : " success" ,
587590 content : existingFile ,
588591 } ;
589592 }
@@ -593,31 +596,33 @@ export class FsManager {
593596
594597 const newFileResource = this . createRawResource ( {
595598 path : newFilePath ,
596- type : ' file' ,
599+ type : " file" ,
597600 } ) ;
598601
599- const isSamePath = getNormalizedPath ( existingFile . path ) . toLowerCase ( ) === getNormalizedPath ( newFileResource . path ) . toLowerCase ( ) ;
602+ const isSamePath =
603+ getNormalizedPath ( existingFile . path ) . toLowerCase ( ) ===
604+ getNormalizedPath ( newFileResource . path ) . toLowerCase ( ) ;
600605 const alreadyExists = await getIfFileExists ( newFileResource ) ;
601606 if ( ! isSamePath && alreadyExists ) {
602607 return {
603- " type" : "error" ,
604- " error" : {
608+ type : "error" ,
609+ error : {
605610 message : `Resource with name '${ sanitizedName } ' already exists!` ,
606611 fileType : FileTypeEnum . UNKNOWN ,
607612 path : newFilePath ,
608613 code : ErrorCode . EntityAlreadyExists ,
609- }
610- }
614+ } ,
615+ } ;
611616 }
612617
613618 const result = await rename ( existingFile , newFileResource ) ;
614619
615- if ( result . type === ' error' ) {
620+ if ( result . type === " error" ) {
616621 return result ;
617622 }
618623
619624 return {
620- type : ' success' ,
625+ type : " success" ,
621626 content : newFileResource ,
622627 } ;
623628 }
@@ -776,7 +781,7 @@ export class FsManager {
776781 async createRecord (
777782 content : Static < typeof ApiRecord > ,
778783 collectionId ?: string ,
779- idToUse ?: string ,
784+ idToUse ?: string
780785 ) : Promise < FileSystemResult < API > > {
781786 const parentFolderResource = this . createResource ( {
782787 id : collectionId || getIdFromPath ( this . rootPath ) ,
@@ -785,13 +790,16 @@ export class FsManager {
785790
786791 const newName = getNewNameIfQuickCreate ( {
787792 name : sanitizeFsResourceName ( content . name ) ,
788- baseName : ' Untitled request' ,
793+ baseName : " Untitled request" ,
789794 parentPath : getNormalizedPath ( parentFolderResource . path ) ,
790795 } ) ;
791796
792797 content . name = newName ;
793798
794- const path = appendPath ( parentFolderResource . path , this . generateFileName ( content . name ) ) ;
799+ const path = appendPath (
800+ parentFolderResource . path ,
801+ this . generateFileName ( content . name )
802+ ) ;
795803 const resource = this . createRawResource ( {
796804 path,
797805 type : "file" ,
@@ -822,7 +830,7 @@ export class FsManager {
822830 async createRecordWithId (
823831 content : Static < typeof ApiRecord > ,
824832 id : string ,
825- collectionId ?: string ,
833+ collectionId ?: string
826834 ) : Promise < FileSystemResult < API > > {
827835 return this . createRecord ( content , collectionId , id ) ;
828836 }
@@ -842,12 +850,11 @@ export class FsManager {
842850 return {
843851 type : "success" ,
844852 } ;
845- }
846- catch ( e ) {
853+ } catch ( e ) {
847854 if ( e instanceof ResourceNotFound ) {
848855 return {
849856 type : "success" ,
850- }
857+ } ;
851858 }
852859
853860 throw e ;
@@ -873,15 +880,15 @@ export class FsManager {
873880 async createCollection (
874881 name : string ,
875882 collectionId ?: string ,
876- idToUse ?: string ,
883+ idToUse ?: string
877884 ) : Promise < FileSystemResult < Collection > > {
878885 const folderResource = this . createResource ( {
879886 id : collectionId || getIdFromPath ( this . rootPath ) ,
880887 type : "folder" ,
881888 } ) ;
882889 const newName = getNewNameIfQuickCreate ( {
883890 name : sanitizeFsResourceName ( name ) ,
884- baseName : ' New collection' ,
891+ baseName : " New collection" ,
885892 parentPath : getNormalizedPath ( folderResource . path ) ,
886893 } ) ;
887894
@@ -918,7 +925,7 @@ export class FsManager {
918925 async createCollectionWithId (
919926 name : string ,
920927 id : string ,
921- collectionId ?: string ,
928+ collectionId ?: string
922929 ) : Promise < FileSystemResult < Collection > > {
923930 return this . createCollection ( name , collectionId , id ) ;
924931 }
@@ -942,7 +949,7 @@ export class FsManager {
942949 if ( e instanceof ResourceNotFound ) {
943950 return {
944951 type : "success" ,
945- }
952+ } ;
946953 }
947954
948955 throw e ;
@@ -1006,7 +1013,7 @@ export class FsManager {
10061013 ) : Promise < FileSystemResult < string > > {
10071014 const collectionFolder = this . createResource ( {
10081015 id,
1009- type : ' folder'
1016+ type : " folder" ,
10101017 } ) ;
10111018 const descriptionFileResource = this . createRawResource ( {
10121019 path : appendPath ( collectionFolder . path , DESCRIPTION_FILE ) ,
@@ -1044,7 +1051,7 @@ export class FsManager {
10441051 ) : Promise < FileSystemResult < Static < typeof Auth > > > {
10451052 const collectionFolder = this . createResource ( {
10461053 id,
1047- type : ' folder'
1054+ type : " folder" ,
10481055 } ) ;
10491056
10501057 const authFileResource = this . createRawResource ( {
@@ -1071,7 +1078,9 @@ export class FsManager {
10711078 id : string ,
10721079 newParentId : string
10731080 ) : Promise < FileSystemResult < Collection > > {
1074- const parentPath = newParentId . length ? fileIndex . getPath ( newParentId ) : this . rootPath ;
1081+ const parentPath = newParentId . length
1082+ ? fileIndex . getPath ( newParentId )
1083+ : this . rootPath ;
10751084 if ( ! parentPath ) {
10761085 throw new Error ( `Path not found for collection/root id ${ newParentId } ` ) ;
10771086 }
@@ -1120,7 +1129,9 @@ export class FsManager {
11201129 id : string ,
11211130 newParentId : string
11221131 ) : Promise < FileSystemResult < API > > {
1123- const parentPath = newParentId . length ? fileIndex . getPath ( newParentId ) : this . rootPath ;
1132+ const parentPath = newParentId . length
1133+ ? fileIndex . getPath ( newParentId )
1134+ : this . rootPath ;
11241135 if ( ! parentPath ) {
11251136 throw new Error ( `Path not found for collection/root id ${ newParentId } ` ) ;
11261137 }
@@ -1214,7 +1225,7 @@ export class FsManager {
12141225 name : patch . name ,
12151226 } ) ;
12161227
1217- if ( fileResourceResult . type === ' error' ) {
1228+ if ( fileResourceResult . type === " error" ) {
12181229 return fileResourceResult ;
12191230 }
12201231
@@ -1243,7 +1254,6 @@ export class FsManager {
12431254 }
12441255
12451256 return parseFileToApi ( this . rootPath , fileResource ) ;
1246-
12471257 }
12481258
12491259 @HandleError
@@ -1292,9 +1302,16 @@ export class FsManager {
12921302 return folderCreationResult ;
12931303 }
12941304
1295- const newName = getNewNameIfQuickCreate ( {
1305+ let newName = getNewNameIfQuickCreate ( {
1306+ name : sanitizeFsResourceName ( environmentName ) ,
1307+ baseName : "New Environment" ,
1308+ parentPath : getNormalizedPath ( environmentFolderPath ) ,
1309+ } ) ;
1310+
1311+ // re-used to handle name conflicts by appending a number when an environment with the same name already exists during import
1312+ newName = getNewNameIfQuickCreate ( {
12961313 name : sanitizeFsResourceName ( environmentName ) ,
1297- baseName : 'New Environment' ,
1314+ baseName : sanitizeFsResourceName ( environmentName ) ,
12981315 parentPath : getNormalizedPath ( environmentFolderPath ) ,
12991316 } ) ;
13001317
@@ -1346,7 +1363,7 @@ export class FsManager {
13461363 name : ( patch as any ) . name ,
13471364 } ) ;
13481365
1349- if ( fileResourceResult . type === ' error' ) {
1366+ if ( fileResourceResult . type === " error" ) {
13501367 return fileResourceResult ;
13511368 }
13521369
@@ -1463,7 +1480,9 @@ export class FsManager {
14631480 collection : CollectionRecord ,
14641481 id : string
14651482 ) : Promise < FileSystemResult < Collection > > {
1466- const parentPath = collection . collectionId . length ? fileIndex . getPath ( collection . collectionId ) : this . rootPath ;
1483+ const parentPath = collection . collectionId . length
1484+ ? fileIndex . getPath ( collection . collectionId )
1485+ : this . rootPath ;
14671486 if ( ! parentPath ) {
14681487 throw new Error ( `Could not find path for id ${ collection . collectionId } ` ) ;
14691488 }
@@ -1479,12 +1498,18 @@ export class FsManager {
14791498 collection . name = newName ;
14801499 }
14811500
1482- const path = appendPath ( parentPath , sanitizeFsResourceName ( collection . name ) ) ;
1501+ const path = appendPath (
1502+ parentPath ,
1503+ sanitizeFsResourceName ( collection . name )
1504+ ) ;
14831505 const collectionFolder = this . createRawResource ( {
14841506 path,
14851507 type : "folder" ,
14861508 } ) ;
1487- const createResult = await createFolder ( collectionFolder , { errorIfExist : true , useId : id } ) ;
1509+ const createResult = await createFolder ( collectionFolder , {
1510+ errorIfExist : true ,
1511+ useId : id ,
1512+ } ) ;
14881513 if ( createResult . type === "error" ) {
14891514 return createResult ;
14901515 }
0 commit comments