diff --git a/macapp/src/index.ts b/macapp/src/index.ts index 28dac136..b5a2b1ab 100644 --- a/macapp/src/index.ts +++ b/macapp/src/index.ts @@ -32,7 +32,7 @@ const logger = winston.createLogger({ format: winston.format.printf(info => info.message), }) -app.on('ready', () => { +app.on('ready', async () => { const gotTheLock = app.requestSingleInstanceLock() if (!gotTheLock) { app.exit(0) @@ -54,7 +54,7 @@ app.on('ready', () => { app.focus({ steal: true }) - init() + await init() }) function firstRunWindow() { @@ -207,7 +207,7 @@ async function checkUpdate() { } } -function init() { +async function init() { if (app.isPackaged) { checkUpdate() setInterval(() => { @@ -217,44 +217,42 @@ function init() { updateTray() - if (process.platform === 'darwin') { - if (app.isPackaged) { - if (!app.isInApplicationsFolder()) { - const chosen = dialog.showMessageBoxSync({ - type: 'question', - buttons: ['Move to Applications', 'Do Not Move'], - message: 'Ollama works best when run from the Applications directory.', - defaultId: 0, - cancelId: 1, - }) + const isInstalled = await installed(); - if (chosen === 0) { - try { - app.moveToApplicationsFolder({ - conflictHandler: conflictType => { - if (conflictType === 'existsAndRunning') { - dialog.showMessageBoxSync({ - type: 'info', - message: 'Cannot move to Applications directory', - detail: - 'Another version of Ollama is currently running from your Applications directory. Close it first and try again.', - }) - } - return true - }, - }) - return - } catch (e) { - logger.error(`[Move to Applications] Failed to move to applications folder - ${e.message}}`) - } - } + if (process.platform === 'darwin' && app.isPackaged && !app.isInApplicationsFolder() && !isInstalled) { + const chosen = dialog.showMessageBoxSync({ + type: 'question', + buttons: ['Move to Applications', 'Do Not Move'], + message: 'Ollama works best when run from the Applications directory.', + defaultId: 0, + cancelId: 1, + }) + + if (chosen === 0) { + try { + app.moveToApplicationsFolder({ + conflictHandler: conflictType => { + if (conflictType === 'existsAndRunning') { + dialog.showMessageBoxSync({ + type: 'info', + message: 'Cannot move to Applications directory', + detail: + 'Another version of Ollama is currently running from your Applications directory. Close it first and try again.', + }) + } + return true + }, + }) + return + } catch (e) { + logger.error(`[Move to Applications] Failed to move to applications folder - ${e.message}}`) } } } server() - if (store.get('first-time-run') && installed()) { + if (store.get('first-time-run1') && isInstalled) { if (process.platform === 'darwin') { app.dock.hide() } diff --git a/macapp/src/install.ts b/macapp/src/install.ts index d8036542..5926c125 100644 --- a/macapp/src/install.ts +++ b/macapp/src/install.ts @@ -1,5 +1,4 @@ -import * as fs from 'fs' -import { exec as cbExec } from 'child_process' +import { exec as cbExec, spawn } from 'child_process' import * as path from 'path' import { promisify } from 'util' @@ -8,8 +7,28 @@ const ollama = app.isPackaged ? path.join(process.resourcesPath, 'ollama') : pat const exec = promisify(cbExec) const symlinkPath = '/usr/local/bin/ollama' -export function installed() { - return fs.existsSync(symlinkPath) && fs.readlinkSync(symlinkPath) === ollama +export async function installed() { + const shells = ['/bin/zsh', '/bin/bash', '/usr/local/bin/fish']; + const checks = shells.map(shell => + new Promise(resolve => { + const proc = spawn(shell, ['-l', '-c', `which ollama`]); + + proc.on('error', () => { + resolve(false); // if the shell isn't found, this will resolve false here + }); + + proc.on('close', code => { + if (code === 0) { + resolve(true); // ollama found, resolve true immediately + } else { + resolve(false); + } + }); + }) + ); + + const results = await Promise.allSettled(checks) + return results.some(result => result.status === 'fulfilled' && result.value === true) } export async function install() {