Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regenerate themes on fresh install #81

Merged
merged 1 commit into from
Jan 27, 2024
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
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { ConfigurationTarget, type ExtensionContext, commands, workspace } from 'vscode'
import { iconDefinitionsPath, themePaths } from './utils/paths'
import type { IconDefinitions } from './types'
import { getConfiguration } from './utils/vscode'
import { getConfiguration, isFreshInstall } from './utils/vscode'
import { updateThemes } from './hooks/updateThemes'
import { mergeTheme } from './hooks/mergeTheme'

export async function activate(context: ExtensionContext) {
const paths = themePaths(context)

if (await isFreshInstall(context)) {
const iconDefinitions = await workspace.fs
.readFile(iconDefinitionsPath(context))
.then(b => JSON.parse(b.toString()) as IconDefinitions)

// TODO only trigger if config is not the default
await updateThemes(mergeTheme(getConfiguration()), paths, iconDefinitions)
}

context.subscriptions.push(
// TODO centralize commands and factorize reset
commands.registerCommand('catppuccin-icons.reset', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ export function iconDefinitionsPath(context: ExtensionContext) {
const root = rootPath(context)
return Uri.joinPath(root, 'iconDefinitions.json')
}

export function flagPath(context: ExtensionContext) {
const root = rootPath(context)
return Uri.joinPath(root, '.flag')
}
14 changes: 13 additions & 1 deletion src/utils/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Functions to interact with VSCode window/workspace
*/
import { Buffer } from 'node:buffer'
import type { Uri } from 'vscode'
import type { ExtensionContext, Uri } from 'vscode'
import { commands, window, workspace } from 'vscode'
import { flagPath } from './paths'
import type { Config } from '~/types'

export async function writeFile(uri: Uri, content: unknown) {
Expand Down Expand Up @@ -33,3 +34,14 @@ export function getConfiguration(): Config {
hidesExplorerArrows: config.get('hidesExplorerArrows', false),
}
}

export async function isFreshInstall(context: ExtensionContext) {
const flag = flagPath(context)
return await workspace.fs.stat(flag).then(
() => false,
() => workspace.fs.writeFile(flag, Buffer.from('')).then(
() => true,
() => true,
),
)
}
Loading