Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ui/desktop/src/components/schedule/CreateScheduleModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Select } from '../ui/Select';
import cronstrue from 'cronstrue';
import * as yaml from 'yaml';
import { Recipe, decodeRecipe } from '../../recipe';
import { getStorageDirectory } from '../../recipe/recipeStorage';
import ClockIcon from '../../assets/clock-icon.svg';

type FrequencyValue = 'once' | 'every' | 'daily' | 'weekly' | 'monthly';
Expand Down Expand Up @@ -361,7 +362,9 @@ export const CreateScheduleModal: React.FC<CreateScheduleModalProps> = ({
};

const handleBrowseFile = async () => {
const filePath = await window.electron.selectFileOrDirectory();
// Default to global recipes directory, but fallback to local if needed
const defaultPath = getStorageDirectory(true);
const filePath = await window.electron.selectFileOrDirectory(defaultPath);
if (filePath) {
if (filePath.endsWith('.yaml') || filePath.endsWith('.yml')) {
setRecipeSourcePath(filePath);
Expand Down
2 changes: 1 addition & 1 deletion ui/desktop/src/goosed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createServer } from 'net';
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import { getBinaryPath } from './utils/binaryPath';
import { getBinaryPath } from './utils/pathUtils';
import log from './utils/logger';
import { App } from 'electron';
import { Buffer } from 'node:buffer';
Expand Down
49 changes: 32 additions & 17 deletions ui/desktop/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenDialogReturnValue } from 'electron';
import type { OpenDialogReturnValue, OpenDialogOptions } from 'electron';
import {
app,
App,
Expand All @@ -20,10 +20,11 @@ import fs from 'node:fs/promises';
import fsSync from 'node:fs';
import started from 'electron-squirrel-startup';
import path from 'node:path';
import os from 'node:os';
import { spawn } from 'child_process';
import 'dotenv/config';
import { startGoosed } from './goosed';
import { getBinaryPath } from './utils/binaryPath';
import { getBinaryPath, expandTilde } from './utils/pathUtils';
import { loadShellEnv } from './utils/loadEnv';
import log from './utils/logger';
import { ensureWinShims } from './utils/winShims';
Expand Down Expand Up @@ -1177,10 +1178,32 @@ ipcMain.handle('get-wakelock-state', () => {
});

// Add file/directory selection handler
ipcMain.handle('select-file-or-directory', async () => {
const result = (await dialog.showOpenDialog({
ipcMain.handle('select-file-or-directory', async (_event, defaultPath?: string) => {
const dialogOptions: OpenDialogOptions = {
properties: process.platform === 'darwin' ? ['openFile', 'openDirectory'] : ['openFile'],
})) as unknown as OpenDialogReturnValue;
};

// Set default path if provided
if (defaultPath) {
// Expand tilde to home directory
const expandedPath = expandTilde(defaultPath);

// Check if the path exists
try {
const stats = await fs.stat(expandedPath);
if (stats.isDirectory()) {
dialogOptions.defaultPath = expandedPath;
} else {
dialogOptions.defaultPath = path.dirname(expandedPath);
}
} catch (error) {
// If path doesn't exist, fall back to home directory and log error
console.error(`Default path does not exist: ${expandedPath}, falling back to home directory`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're not actually checking that the file didn't exist here though - when would this error be triggered?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.stat(path) throws when the file/dir does not exist.

dialogOptions.defaultPath = os.homedir();
}
}

const result = (await dialog.showOpenDialog(dialogOptions)) as unknown as OpenDialogReturnValue;

if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
Expand Down Expand Up @@ -1450,9 +1473,7 @@ ipcMain.handle('get-binary-path', (_event, binaryName) => {
ipcMain.handle('read-file', (_event, filePath) => {
return new Promise((resolve) => {
// Expand tilde to home directory
const expandedPath = filePath.startsWith('~')
? path.join(app.getPath('home'), filePath.slice(1))
: filePath;
const expandedPath = expandTilde(filePath);

const cat = spawn('cat', [expandedPath]);
let output = '';
Expand Down Expand Up @@ -1485,9 +1506,7 @@ ipcMain.handle('read-file', (_event, filePath) => {
ipcMain.handle('write-file', (_event, filePath, content) => {
return new Promise((resolve) => {
// Expand tilde to home directory
const expandedPath = filePath.startsWith('~')
? path.join(app.getPath('home'), filePath.slice(1))
: filePath;
const expandedPath = expandTilde(filePath);

// Create a write stream to the file
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -1506,9 +1525,7 @@ ipcMain.handle('write-file', (_event, filePath, content) => {
ipcMain.handle('ensure-directory', async (_event, dirPath) => {
try {
// Expand tilde to home directory
const expandedPath = dirPath.startsWith('~')
? path.join(app.getPath('home'), dirPath.slice(1))
: dirPath;
const expandedPath = expandTilde(dirPath);

await fs.mkdir(expandedPath, { recursive: true });
return true;
Expand All @@ -1521,9 +1538,7 @@ ipcMain.handle('ensure-directory', async (_event, dirPath) => {
ipcMain.handle('list-files', async (_event, dirPath, extension) => {
try {
// Expand tilde to home directory
const expandedPath = dirPath.startsWith('~')
? path.join(app.getPath('home'), dirPath.slice(1))
: dirPath;
const expandedPath = expandTilde(dirPath);

const files = await fs.readdir(expandedPath);
if (extension) {
Expand Down
5 changes: 3 additions & 2 deletions ui/desktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type ElectronAPI = {
fetchMetadata: (url: string) => Promise<string>;
reloadApp: () => void;
checkForOllama: () => Promise<boolean>;
selectFileOrDirectory: () => Promise<string | null>;
selectFileOrDirectory: (defaultPath?: string) => Promise<string | null>;
startPowerSaveBlocker: () => Promise<number>;
stopPowerSaveBlocker: () => Promise<void>;
getBinaryPath: (binaryName: string) => Promise<string>;
Expand Down Expand Up @@ -151,7 +151,8 @@ const electronAPI: ElectronAPI = {
fetchMetadata: (url: string) => ipcRenderer.invoke('fetch-metadata', url),
reloadApp: () => ipcRenderer.send('reload-app'),
checkForOllama: () => ipcRenderer.invoke('check-ollama'),
selectFileOrDirectory: () => ipcRenderer.invoke('select-file-or-directory'),
selectFileOrDirectory: (defaultPath?: string) =>
ipcRenderer.invoke('select-file-or-directory', defaultPath),
startPowerSaveBlocker: () => ipcRenderer.invoke('start-power-save-blocker'),
stopPowerSaveBlocker: () => ipcRenderer.invoke('stop-power-save-blocker'),
getBinaryPath: (binaryName: string) => ipcRenderer.invoke('get-binary-path', binaryName),
Expand Down
2 changes: 1 addition & 1 deletion ui/desktop/src/recipe/recipeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function parseLastModified(val: string | Date): Date {
/**
* Get the storage directory path for recipes
*/
function getStorageDirectory(isGlobal: boolean): string {
export function getStorageDirectory(isGlobal: boolean): string {
return isGlobal ? '~/.config/goose/recipes' : '.goose/recipes';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';
import Electron from 'electron';
import log from './logger';

Expand Down Expand Up @@ -111,3 +112,15 @@ const addPaths = (
}
}
};

/**
* Expands tilde (~) to the user's home directory
* @param filePath - The file path that may contain tilde
* @returns The expanded path with tilde replaced by home directory
*/
export function expandTilde(filePath: string): string {
if (filePath.startsWith('~')) {
return path.join(os.homedir(), filePath.slice(1));
}
return filePath;
}
Loading