diff --git a/src/renderer/actions/local-sync/constants.ts b/src/renderer/actions/local-sync/constants.ts index eca6e7b..35e1e5b 100644 --- a/src/renderer/actions/local-sync/constants.ts +++ b/src/renderer/actions/local-sync/constants.ts @@ -9,6 +9,7 @@ import { import { FileType } from "./types"; import { TSchema, Type } from "@sinclair/typebox"; +export const CURRENT_CONFIG_FILE_VERSION = 0.1; export const CONFIG_FILE = "requestly.json"; export const COLLECTION_AUTH_FILE = "auth.json"; export const DESCRIPTION_FILE = "description.md"; diff --git a/src/renderer/actions/local-sync/fs-utils.ts b/src/renderer/actions/local-sync/fs-utils.ts index 3001cef..a0bd9b9 100644 --- a/src/renderer/actions/local-sync/fs-utils.ts +++ b/src/renderer/actions/local-sync/fs-utils.ts @@ -24,6 +24,7 @@ import { COLLECTION_AUTH_FILE, COLLECTION_VARIABLES_FILE, CONFIG_FILE, + CURRENT_CONFIG_FILE_VERSION, DESCRIPTION_FILE, DS_STORE_FILE, ENVIRONMENT_VARIABLES_FOLDER, @@ -359,7 +360,7 @@ export async function addWorkspaceToGlobalConfig(params: { path: appendPath(GLOBAL_CONFIG_FOLDER_PATH, GLOBAL_CONFIG_FILE_NAME), type: "file", }); - const configRecord: Static[0] = { + const newWorkspace = { id: uuidv4(), name, path, @@ -368,7 +369,10 @@ export async function addWorkspaceToGlobalConfig(params: { globalConfigFileResource ); if (!globalConfigFileExists) { - const config: Static = [configRecord]; + const config: Static = { + version: CURRENT_CONFIG_FILE_VERSION, + workspaces: [newWorkspace], + }; const result = await writeContent( globalConfigFileResource, config, @@ -379,7 +383,7 @@ export async function addWorkspaceToGlobalConfig(params: { } return { type: "success", - content: configRecord, + content: newWorkspace, }; } @@ -392,10 +396,10 @@ export async function addWorkspaceToGlobalConfig(params: { return readResult; } - const updatedConfig: Static = [ - ...readResult.content, - configRecord, - ]; + const updatedConfig = { + version: readResult.content.version, + workspaces: [...readResult.content.workspaces, newWorkspace], + }; const writeResult = await writeContent( globalConfigFileResource, @@ -408,14 +412,15 @@ export async function addWorkspaceToGlobalConfig(params: { return { type: "success", - content: configRecord, + content: newWorkspace, }; } export async function createWorkspaceFolder( name: string, path: string -): Promise[0]>> { +): Promise> { + console.log("createWorkspaceFolder called", name, path); const folderCreationResult = await createFolder( createFsResource({ rootPath: path, @@ -448,9 +453,21 @@ export async function createWorkspaceFolder( }); } +export async function migrateGlobalConfig(oldConfig: any) { + if (!oldConfig.version) { + return { + version: CURRENT_CONFIG_FILE_VERSION, + workspaces: oldConfig, + }; + } + + return oldConfig; +} + export async function getAllWorkspaces(): Promise< - FileSystemResult> + FileSystemResult["workspaces"]> > { + console.log("getAllWorkspaces called"); const globalConfigFileResource = createFsResource({ rootPath: GLOBAL_CONFIG_FOLDER_PATH, path: appendPath(GLOBAL_CONFIG_FOLDER_PATH, GLOBAL_CONFIG_FILE_NAME), @@ -459,10 +476,33 @@ export async function getAllWorkspaces(): Promise< const readResult = await parseFile({ resource: globalConfigFileResource, - validator: GlobalConfig, }); - return readResult; + if (readResult.type === "error") { + return readResult; + } + // @ts-ignore + if (readResult.content.version !== CURRENT_CONFIG_FILE_VERSION) { + const migratedConfig = await migrateGlobalConfig(readResult.content); + const writeResult = await writeContent( + globalConfigFileResource, + migratedConfig, + GlobalConfig + ); + if (writeResult.type === "error") { + return writeResult; + } + + return { + type: "success", + content: migratedConfig.workspaces, + }; + } + + // @ts-ignore + return readResult.content.workspaces as FileSystemResult< + Static["workspaces"] + >; } export function getParentFolderPath(fsResource: FsResource) { diff --git a/src/renderer/actions/local-sync/schemas.ts b/src/renderer/actions/local-sync/schemas.ts index 1242ccd..ea1faa5 100644 --- a/src/renderer/actions/local-sync/schemas.ts +++ b/src/renderer/actions/local-sync/schemas.ts @@ -127,10 +127,13 @@ export const EnvironmentRecord = Type.Object({ variables: Variables, }); -export const GlobalConfig = Type.Array( - Type.Object({ - id: Type.String(), - name: Type.String(), - path: Type.String(), - }) -); +export const GlobalConfig = Type.Object({ + version: Type.Number(), + workspaces: Type.Array( + Type.Object({ + id: Type.String(), + name: Type.String(), + path: Type.String(), + }) + ), +});