@@ -24,6 +24,7 @@ import {
2424 COLLECTION_AUTH_FILE ,
2525 COLLECTION_VARIABLES_FILE ,
2626 CONFIG_FILE ,
27+ CORE_CONFIG_FILE_VERSION ,
2728 DESCRIPTION_FILE ,
2829 DS_STORE_FILE ,
2930 ENVIRONMENT_VARIABLES_FOLDER ,
@@ -36,8 +37,8 @@ import {
3637 EnvironmentRecord ,
3738 Variables ,
3839 EnvironmentVariableType ,
39- GlobalConfig ,
4040 Auth ,
41+ GlobalConfig ,
4142} from "./schemas" ;
4243import { Stats } from "node:fs" ;
4344import { FileType } from "./file-types/file-type.interface" ;
@@ -49,6 +50,7 @@ import {
4950 GlobalConfigRecordFileType ,
5051 ReadmeRecordFileType ,
5152} from "./file-types/file-types" ;
53+ import path from "node:path" ;
5254
5355export async function getFsResourceStats (
5456 resource : FsResource
@@ -372,7 +374,7 @@ export async function addWorkspaceToGlobalConfig(params: {
372374 path : string ;
373375} ) : Promise < FileSystemResult < { name : string ; id : string ; path : string } > > {
374376 const fileType = new GlobalConfigRecordFileType ( ) ;
375- const { name, path } = params ;
377+ const { name, path : workspacePath } = params ;
376378 const globalConfigFolderResource = createFsResource ( {
377379 rootPath : GLOBAL_CONFIG_FOLDER_PATH ,
378380 path : GLOBAL_CONFIG_FOLDER_PATH ,
@@ -393,16 +395,19 @@ export async function addWorkspaceToGlobalConfig(params: {
393395 path : appendPath ( GLOBAL_CONFIG_FOLDER_PATH , GLOBAL_CONFIG_FILE_NAME ) ,
394396 type : "file" ,
395397 } ) ;
396- const configRecord : Static < typeof GlobalConfig > [ 0 ] = {
398+ const newWorkspace = {
397399 id : uuidv4 ( ) ,
398400 name,
399- path,
401+ path : workspacePath ,
400402 } ;
401403 const globalConfigFileExists = await getIfFileExists (
402404 globalConfigFileResource
403405 ) ;
404406 if ( ! globalConfigFileExists ) {
405- const config : Static < typeof GlobalConfig > = [ configRecord ] ;
407+ const config : Static < typeof GlobalConfig > = {
408+ version : CORE_CONFIG_FILE_VERSION ,
409+ workspaces : [ newWorkspace ] ,
410+ } ;
406411 const result = await writeContent (
407412 globalConfigFileResource ,
408413 config ,
@@ -413,7 +418,7 @@ export async function addWorkspaceToGlobalConfig(params: {
413418 }
414419 return {
415420 type : "success" ,
416- content : configRecord ,
421+ content : newWorkspace ,
417422 } ;
418423 }
419424
@@ -426,10 +431,10 @@ export async function addWorkspaceToGlobalConfig(params: {
426431 return readResult ;
427432 }
428433
429- const updatedConfig : Static < typeof GlobalConfig > = [
430- ... readResult . content ,
431- configRecord ,
432- ] ;
434+ const updatedConfig = {
435+ version : readResult . content . version ,
436+ workspaces : [ ... readResult . content . workspaces , newWorkspace ] ,
437+ } ;
433438
434439 const writeResult = await writeContent (
435440 globalConfigFileResource ,
@@ -442,18 +447,18 @@ export async function addWorkspaceToGlobalConfig(params: {
442447
443448 return {
444449 type : "success" ,
445- content : configRecord ,
450+ content : newWorkspace ,
446451 } ;
447452}
448453
449454export async function createWorkspaceFolder (
450455 name : string ,
451- path : string
452- ) : Promise < FileSystemResult < Static < typeof GlobalConfig > [ 0 ] > > {
456+ workspacePath : string
457+ ) : Promise < FileSystemResult < { name : string ; id : string ; path : string } > > {
453458 const folderCreationResult = await createFolder (
454459 createFsResource ( {
455- rootPath : path ,
456- path,
460+ rootPath : workspacePath ,
461+ path : workspacePath ,
457462 type : "folder" ,
458463 } )
459464 ) ;
@@ -463,8 +468,8 @@ export async function createWorkspaceFolder(
463468 }
464469 const configFileCreationResult = await writeContentRaw (
465470 createFsResource ( {
466- rootPath : path ,
467- path : appendPath ( path , "requestly.json" ) ,
471+ rootPath : workspacePath ,
472+ path : appendPath ( workspacePath , "requestly.json" ) ,
468473 type : "file" ,
469474 } ) ,
470475 {
@@ -477,34 +482,67 @@ export async function createWorkspaceFolder(
477482
478483 return addWorkspaceToGlobalConfig ( {
479484 name,
480- path,
485+ path : workspacePath ,
481486 } ) ;
482487}
483488
489+ export async function migrateGlobalConfig ( oldConfig : any ) {
490+ if ( ! oldConfig . version ) {
491+ return {
492+ version : CORE_CONFIG_FILE_VERSION ,
493+ workspaces : oldConfig ,
494+ } ;
495+ }
496+
497+ return oldConfig ;
498+ }
499+
484500export async function getAllWorkspaces ( ) : Promise <
485- FileSystemResult < Static < typeof GlobalConfig > >
501+ FileSystemResult < Static < typeof GlobalConfig > [ "workspaces" ] >
486502> {
487503 const globalConfigFileResource = createFsResource ( {
488504 rootPath : GLOBAL_CONFIG_FOLDER_PATH ,
489505 path : appendPath ( GLOBAL_CONFIG_FOLDER_PATH , GLOBAL_CONFIG_FILE_NAME ) ,
490506 type : "file" ,
491507 } ) ;
492508
493- const readResult = await parseFile ( {
509+ const readResult = await parseFileRaw ( {
494510 resource : globalConfigFileResource ,
495- fileType : new GlobalConfigRecordFileType ( ) ,
496511 } ) ;
497512
498- return readResult ;
513+ if ( readResult . type === "error" ) {
514+ return readResult ;
515+ }
516+
517+ const { content } = readResult ;
518+ const parsedContent = JSON . parse ( content ) ;
519+ // @ts -ignore
520+ if ( parsedContent . version !== CORE_CONFIG_FILE_VERSION ) {
521+ const migratedConfig = await migrateGlobalConfig ( parsedContent ) ;
522+ const writeResult = await writeContent (
523+ globalConfigFileResource ,
524+ migratedConfig ,
525+ new GlobalConfigRecordFileType ( )
526+ ) ;
527+ if ( writeResult . type === "error" ) {
528+ return writeResult ;
529+ }
530+
531+ return {
532+ type : "success" ,
533+ content : migratedConfig . workspaces ,
534+ } ;
535+ }
536+
537+ // @ts -ignore
538+ return readResult . content . workspaces as FileSystemResult <
539+ Static < typeof GlobalConfig > [ "workspaces" ]
540+ > ;
499541}
500542
501543export function getParentFolderPath ( fsResource : FsResource ) {
502- const { path } = fsResource ;
503- const name = getNameOfResource ( fsResource ) ;
504- const normalizedName =
505- fsResource . type === "folder" ? getNormalizedPath ( name ) : name ;
506- const [ rawParent ] = path . split ( `/${ normalizedName } ` ) ;
507- const parent = getNormalizedPath ( rawParent ) ;
544+ const { path : resourcePath } = fsResource ;
545+ const parent = getNormalizedPath ( path . dirname ( resourcePath ) ) ;
508546 return parent ;
509547}
510548
@@ -785,10 +823,10 @@ export function parseToEnvironmentEntity(
785823 return newVariables ;
786824}
787825
788- export function getFileNameFromPath ( path : string ) {
789- if ( path . endsWith ( "/" ) ) {
826+ export function getFileNameFromPath ( filePath : string ) {
827+ if ( filePath . endsWith ( "/" ) ) {
790828 throw new Error ( 'Path seems to be a folder, ends with "/"' ) ;
791829 }
792- const parts = path . split ( "/" ) ;
830+ const parts = filePath . split ( "/" ) ;
793831 return parts [ parts . length - 1 ] ;
794832}
0 commit comments