Created
February 22, 2025 16:30
-
-
Save Strajk/52310b7bf52fc8d10b1afa68ce5ec744 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Finicky Switch Default Browser | |
// Description: Switches the default browser in Finicky config | |
// Author: Strajk | |
import '@johnlindquist/kit' | |
import fs from 'fs' | |
let finickyPath = home('.finicky.js') | |
// Check if Finicky config exists | |
if (!fs.existsSync(finickyPath)) { | |
await div({ | |
html: ` | |
<div class="p-4 bg-red-100 text-red-800"> | |
<h3 class="font-bold">Finicky not installed</h3> | |
<p>Or at least, ~/.finicky.js was not found</p> | |
<p>Please install Finicky first: <code>brew install finicky</code></p> | |
</div> | |
` | |
}) | |
exit() | |
} | |
// Resolve symlink if finickyPath is a symlink, e.g. /Users/strajk/.finicky.js -> /Users/strajk/Projects/setup/home/.finicky.js | |
finickyPath = fs.realpathSync(finickyPath) | |
// List of possible browsers to select from | |
const possibleBrowsers = [ | |
'Google Chrome', | |
'Google Chrome Dev', | |
'Firefox', | |
'Firefox Developer Edition', | |
'Safari', | |
'Opera', | |
'Brave', | |
'Microsoft Edge', | |
'Browserosaurus', | |
] | |
try { | |
// Read the content of the Finicky configuration file | |
const configContent = await readFile(finickyPath, 'utf8') | |
// Log number of lines in config file | |
const numLines = configContent.split('\n').length | |
console.log('===' + new Date().toISOString()) | |
console.log(`Config file has ${numLines} lines`) | |
// Regular expression to find the default browser setting in the config | |
const defaultBrowserRegex = /defaultBrowser: "([^"]*?)".?/ | |
const match = defaultBrowserRegex.exec(configContent) | |
const currentDefaultBrowser = match ? match[1] : null | |
if (!currentDefaultBrowser) { | |
console.log('No default browser found') | |
exit() | |
} | |
// Prompt the user to select a new default browser | |
const selectedBrowser = await arg({ | |
placeholder: 'Select a default browser', | |
choices: possibleBrowsers, | |
hint: 'Current default browser: ' + currentDefaultBrowser, | |
}) | |
// Create a new comment based on the previously selected browser | |
const newComment = currentDefaultBrowser ? ` Previously: ${currentDefaultBrowser}` : '' | |
// Replace the old default browser setting with the new one, including the updated comment | |
const newConfigContent = configContent.replace( | |
defaultBrowserRegex, | |
`defaultBrowser: "${selectedBrowser}", //${newComment}` | |
) | |
// Write the updated content back to the Finicky configuration file | |
await writeFile(finickyPath, newConfigContent) | |
// Display a notification to confirm the update | |
await notify({ | |
title: 'Finicky Config Updated', | |
body: `Default browser set to ${selectedBrowser}`, | |
}) | |
} catch (error) { | |
// Handle errors, such as the config file not existing | |
console.error(`Failed to update Finicky config: ${error}`) | |
await notify({ | |
title: 'Finicky Config Update Failed', | |
body: `Error: ${error}`, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment