mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-22 12:15:38 +00:00
fix: default workspace error checking
This commit is contained in:
@@ -425,20 +425,12 @@ const registerWorkspaceIpc = (mainWindow, workspaceWatcher) => {
|
||||
ipcMain.handle('renderer:get-default-workspace', async (event) => {
|
||||
try {
|
||||
const result = await defaultWorkspaceManager.ensureDefaultWorkspaceExists();
|
||||
|
||||
if (!result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { workspacePath, workspaceUid } = result;
|
||||
|
||||
const workspaceFilePath = path.join(workspacePath, 'workspace.yml');
|
||||
if (!fs.existsSync(workspaceFilePath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const yamlContent = fs.readFileSync(workspaceFilePath, 'utf8');
|
||||
const workspaceConfig = yaml.load(yamlContent);
|
||||
const workspaceConfig = readWorkspaceConfig(workspacePath);
|
||||
|
||||
return {
|
||||
workspaceConfig: {
|
||||
@@ -449,32 +441,30 @@ const registerWorkspaceIpc = (mainWindow, workspaceWatcher) => {
|
||||
workspacePath
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting default workspace:', error);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('main:renderer-ready', async (win) => {
|
||||
try {
|
||||
// Load default workspace
|
||||
const defaultResult = await defaultWorkspaceManager.ensureDefaultWorkspaceExists();
|
||||
if (defaultResult) {
|
||||
const { workspacePath, workspaceUid } = defaultResult;
|
||||
const workspaceFilePath = path.join(workspacePath, 'workspace.yml');
|
||||
const workspaceConfig = readWorkspaceConfig(workspacePath);
|
||||
|
||||
if (fs.existsSync(workspaceFilePath)) {
|
||||
const yamlContent = fs.readFileSync(workspaceFilePath, 'utf8');
|
||||
const workspaceConfig = yaml.load(yamlContent);
|
||||
win.webContents.send('main:workspace-opened', workspacePath, workspaceUid, {
|
||||
...workspaceConfig,
|
||||
type: 'default'
|
||||
});
|
||||
|
||||
win.webContents.send('main:workspace-opened', workspacePath, workspaceUid, {
|
||||
...workspaceConfig,
|
||||
type: 'default'
|
||||
});
|
||||
|
||||
if (workspaceWatcher) {
|
||||
workspaceWatcher.addWatcher(win, workspacePath);
|
||||
}
|
||||
if (workspaceWatcher) {
|
||||
workspaceWatcher.addWatcher(win, workspacePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Load other workspaces
|
||||
const workspacePaths = lastOpenedWorkspaces.getAll();
|
||||
const invalidPaths = [];
|
||||
|
||||
|
||||
@@ -2,17 +2,18 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { app } = require('electron');
|
||||
const { generateUidBasedOnHash } = require('../utils/common');
|
||||
const { writeFile, createDirectory } = require('../utils/filesystem');
|
||||
const { writeFile } = require('../utils/filesystem');
|
||||
const { getPreferences, savePreferences } = require('./preferences');
|
||||
const { globalEnvironmentsStore } = require('./global-environments');
|
||||
const { generateYamlContent } = require('../utils/workspace-config');
|
||||
const { generateYamlContent, readWorkspaceConfig, validateWorkspaceConfig } = require('../utils/workspace-config');
|
||||
|
||||
const OPENCOLLECTION_VERSION = '1.0.0';
|
||||
const WORKSPACE_TYPE = 'workspace';
|
||||
const DEFAULT_WORKSPACE_UID = 'default';
|
||||
|
||||
class DefaultWorkspaceManager {
|
||||
constructor() {
|
||||
this.defaultWorkspacePath = null;
|
||||
this.defaultWorkspaceUid = null;
|
||||
this.initializationPromise = null;
|
||||
}
|
||||
|
||||
@@ -27,16 +28,7 @@ class DefaultWorkspaceManager {
|
||||
}
|
||||
|
||||
getDefaultWorkspaceUid() {
|
||||
const workspacePath = this.getDefaultWorkspacePath();
|
||||
if (!workspacePath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.defaultWorkspaceUid) {
|
||||
this.defaultWorkspaceUid = generateUidBasedOnHash(workspacePath);
|
||||
}
|
||||
|
||||
return this.defaultWorkspaceUid;
|
||||
return DEFAULT_WORKSPACE_UID;
|
||||
}
|
||||
|
||||
async setDefaultWorkspacePath(workspacePath) {
|
||||
@@ -48,11 +40,29 @@ class DefaultWorkspaceManager {
|
||||
await savePreferences(preferences);
|
||||
|
||||
this.defaultWorkspacePath = workspacePath;
|
||||
this.defaultWorkspaceUid = generateUidBasedOnHash(workspacePath);
|
||||
|
||||
return workspacePath;
|
||||
}
|
||||
|
||||
isValidDefaultWorkspace(workspacePath) {
|
||||
if (!workspacePath || !fs.existsSync(workspacePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const workspaceYmlPath = path.join(workspacePath, 'workspace.yml');
|
||||
if (!fs.existsSync(workspaceYmlPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const config = readWorkspaceConfig(workspacePath);
|
||||
validateWorkspaceConfig(config);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async ensureDefaultWorkspaceExists() {
|
||||
if (this.initializationPromise) {
|
||||
return this.initializationPromise;
|
||||
@@ -60,27 +70,27 @@ class DefaultWorkspaceManager {
|
||||
|
||||
const existingPath = this.getDefaultWorkspacePath();
|
||||
|
||||
if (existingPath && fs.existsSync(existingPath)) {
|
||||
// If existing path is valid, return it
|
||||
if (this.isValidDefaultWorkspace(existingPath)) {
|
||||
return {
|
||||
workspacePath: existingPath,
|
||||
workspaceUid: this.getDefaultWorkspaceUid()
|
||||
};
|
||||
}
|
||||
|
||||
// Need to create/recreate default workspace
|
||||
this.initializationPromise = (async () => {
|
||||
try {
|
||||
const shouldMigrate = this.needsMigration();
|
||||
const newWorkspacePath = await this.initializeDefaultWorkspace(null, { migrateFromPreferences: shouldMigrate });
|
||||
const workspaceYmlPath = path.join(newWorkspacePath, 'workspace.yml');
|
||||
if (!fs.existsSync(workspaceYmlPath)) {
|
||||
this.defaultWorkspacePath = null;
|
||||
return null;
|
||||
} else {
|
||||
return {
|
||||
workspacePath: newWorkspacePath,
|
||||
workspaceUid: this.getDefaultWorkspaceUid()
|
||||
};
|
||||
}
|
||||
const newWorkspacePath = await this.initializeDefaultWorkspace(existingPath, { migrateFromPreferences: shouldMigrate });
|
||||
|
||||
return {
|
||||
workspacePath: newWorkspacePath,
|
||||
workspaceUid: this.getDefaultWorkspaceUid()
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize default workspace:', error);
|
||||
return null;
|
||||
} finally {
|
||||
this.initializationPromise = null;
|
||||
}
|
||||
@@ -89,35 +99,32 @@ class DefaultWorkspaceManager {
|
||||
return this.initializationPromise;
|
||||
}
|
||||
|
||||
async initializeDefaultWorkspace(workspacePath = null, options = {}) {
|
||||
async initializeDefaultWorkspace(existingPath = null, options = {}) {
|
||||
const { migrateFromPreferences = true } = options;
|
||||
|
||||
if (!workspacePath) {
|
||||
let workspacePath = existingPath;
|
||||
|
||||
if (!workspacePath || !fs.existsSync(workspacePath)) {
|
||||
const configDir = app.getPath('userData');
|
||||
const baseWorkspacePath = path.join(configDir, 'default-workspace');
|
||||
|
||||
let finalPath = baseWorkspacePath;
|
||||
workspacePath = baseWorkspacePath;
|
||||
let counter = 1;
|
||||
while (fs.existsSync(finalPath)) {
|
||||
finalPath = `${baseWorkspacePath}-${counter}`;
|
||||
while (fs.existsSync(workspacePath)) {
|
||||
workspacePath = `${baseWorkspacePath}-${counter}`;
|
||||
counter++;
|
||||
}
|
||||
|
||||
workspacePath = finalPath;
|
||||
}
|
||||
|
||||
if (!fs.existsSync(workspacePath)) {
|
||||
await createDirectory(workspacePath);
|
||||
}
|
||||
|
||||
await createDirectory(path.join(workspacePath, 'collections'));
|
||||
await createDirectory(path.join(workspacePath, 'environments'));
|
||||
fs.mkdirSync(workspacePath, { recursive: true });
|
||||
fs.mkdirSync(path.join(workspacePath, 'collections'), { recursive: true });
|
||||
fs.mkdirSync(path.join(workspacePath, 'environments'), { recursive: true });
|
||||
|
||||
const workspaceConfig = {
|
||||
opencollection: OPENCOLLECTION_VERSION,
|
||||
info: {
|
||||
name: 'My Workspace',
|
||||
type: 'default'
|
||||
type: WORKSPACE_TYPE
|
||||
},
|
||||
collections: [],
|
||||
specs: [],
|
||||
|
||||
Reference in New Issue
Block a user