Skip to content

Commit

Permalink
feat!: automatic update check
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax committed Jan 1, 2024
1 parent cf53bd8 commit a25785d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ locale.value = $settings.lang
useHead({
title: $settings.title,
bodyAttrs: {
class: 'relative',
},
})
</script>
15 changes: 15 additions & 0 deletions components/Update.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<Teleport v-if="data && data.available" to="body">
<div class="fixed z-20 bottom-[30px] right-[30px] opacity-80 hover:opacity-100 transition-all">
<div class="w-[160px] h-[160px] -z-1 rounded-full bg-brand-600 blur-3xl right-0 -bottom-20 absolute" />
<div class="z-1 relative text-end text-fg-dimmed">
A new version is available <strong>{{ data.version }}</strong><br>
<a :href="`https://github.com/hywax/mafl/releases/tag/v${data.version}`" class="border-b border-dashed" target="_blank">Visit on github →</a>
</div>
</div>
</Teleport>
</template>

<script setup lang="ts">
const { data } = await useFetch('/api/update')
</script>
3 changes: 2 additions & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
</p>
</template>
</Message>
<Update v-if="$settings.checkUpdates" />
</template>

<script setup lang="ts">
const { $services } = useNuxtApp()
const { $services, $settings } = useNuxtApp()
</script>
15 changes: 15 additions & 0 deletions server/api/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import currentPackage from '~/package.json'

export default defineEventHandler(async () => {
const latestPackage = await $fetch<typeof currentPackage>('https://raw.githubusercontent.com/hywax/mafl/main/package.json', {
parseResponse: (json) => JSON.parse(json),
})

const parseVersion = (version: string): number => Number.parseInt(version.replace(/\./g, ''), 10)
const difference = parseVersion(latestPackage.version) - parseVersion(currentPackage.version)

return {
available: difference > 0,
version: latestPackage.version,
}
})
1 change: 1 addition & 0 deletions server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function getDefaultConfig(): CompleteConfig {
lang: 'en',
theme: 'system',
services: [],
checkUpdates: true,
}
}

Expand Down
1 change: 1 addition & 0 deletions types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Config {
lang: 'en' | 'ru'
theme?: 'system' | 'light' | 'dark' | 'deep'
services: ServicesGroup[]
checkUpdates: boolean
}

export type CompleteConfig = Required<Config>

0 comments on commit a25785d

Please sign in to comment.