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

Keep the download URL consistent #398

Merged
merged 3 commits into from
Nov 28, 2022
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
120 changes: 16 additions & 104 deletions nuxt/components/DownloadButton.vue
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
<template>
<div>
<button
<a
:class="buttonStyle"
class="mx-auto lg:mx-0 hover:underline font-bold rounded-full py-4 px-8"
@click="download"
class="mx-auto lg:mx-0 hover:underline font-bold rounded-full py-4 px-8 block"
:href="mainLink"
>
התקן עכשיו
</button>
<div
v-if="showMoreDownloads"
class="mx-auto text-center"
>
<button @click="moreDownloads">
</a>
<div class="mx-auto text-center">
<a :href="releasesUrl">
הורדות נוספות
</button>
<div
class="w-full text-gray-800"
:style="{ visibility: showDownloads ? 'visible' : 'hidden' }"
>
<span
v-for="obj in downloads"
:key="obj.name"
>
<a
class="no-underline hover:underline text-blue-600"
:href="obj.url"
:title="obj.name"
>{{ obj.extension }}</a>,
</span>
<a
class="font-bold hover:underline"
:href="releasesUrl"
target="_blank"
>עוד...</a>
</div>
</a>
</div>
</div>
</template>

<script>
import axios from 'axios';

const getOS = () => {
if (process.client) {
// This script sets OSName variable as follows:
Expand All @@ -60,33 +35,14 @@ const getOS = () => {
};

const osToExtension = {
Linux: {
default: 'AppImage',
extensions: ['AppImage', 'deb'],
},
Windows: {
default: 'exe',
extensions: ['exe'],
},
MacOS: {
default: 'dmg',
extensions: ['dmg', 'zip'],
},
Linux: 'AppImage',
Windows: 'exe',
MacOS: 'dmg',
};

const excludeExtensions = ['blockmap', 'yml'];

const isWithExtension = (extensions, name) => extensions.filter((ext) => name.toLowerCase().endsWith(ext.toLowerCase()))
.length > 0;

export default {
name: 'DownloadButton',
props: {
showMoreDownloads: {
type: Boolean,
default: true,
required: false,
},
buttonStyle: {
type: String,
default: '',
Expand All @@ -95,61 +51,17 @@ export default {
},
data() {
return {
downloads: [],
os: 'Unknown',
mainLink: '',
showDownloads: false,
releasesUrl: `https://github.com/${GITHUB_REPO}/releases`,
releasesUrl: `https://github.com/${GITHUB_REPO}/releases/latest`,
};
},
created() {
this.os = getOS();

axios
.get(`https://api.github.com/repos/${GITHUB_REPO}/releases`)
.then((response) => response.data)
.then((releases) => releases
.sort((a, b) => new Date(b.published_at) - new Date(a.published_at))
.find((release) => !release.draft))
.then((release) => {
const osExtension = osToExtension[this.os];
const extension = osToExtension[getOS()];

const filteredAssets = osExtension && osExtension.extensions.length > 0
? release.assets.filter((asset) => isWithExtension(osExtension.extensions, asset.name))
: release.assets;

this.downloads = filteredAssets
.filter((asset) => !isWithExtension(excludeExtensions, asset.name))
.map(({ name, browser_download_url: url }) => {
const splitted = name.split('.');
const extension = splitted[splitted.length - 1];
return {
name,
url,
extension,
};
});
});
},
methods: {
download() {
const defaultExt = osToExtension[this.os].default;
const download = this.downloads.find(({ name }) => name.toLowerCase().endsWith(defaultExt.toLowerCase()));
if (download) {
window.open(download.url);
} else {
this.openReleasesPage();
}
},
moreDownloads() {
if (this.downloads.length > 1) {
this.showDownloads = !this.showDownloads;
} else {
this.openReleasesPage();
}
},
openReleasesPage() {
window.open(this.releasesUrl, '_blank');
},
this.mainLink = extension
? `${this.releasesUrl}/download/caspion.${extension}`
: this.releasesUrl;
},
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion nuxt/test/__snapshots__/HeroContent.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`HeroContent renders properly 1`] = `
בצורה מאובטחת, ללא צד שלישי ובפיקוח הקהילה
</p>
<div class=\\"mx-auto my-6\\">
<downloadbutton-stub showmoredownloads=\\"true\\" buttonstyle=\\"bg-white text-gray-800 shadow-lg\\"></downloadbutton-stub>
<downloadbutton-stub buttonstyle=\\"bg-white text-gray-800 shadow-lg\\"></downloadbutton-stub>
</div>
</div>
<div class=\\"w-full md:w-3/5 py-6 md:pr-2 text-center\\"><img src=\\"~/assets/img/main-screenshot.png\\" alt=\\"\\" class=\\"w-full \\"></div>
Expand Down
2 changes: 2 additions & 0 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = {
releaseType: 'release'
}
],
// eslint-disable-next-line no-template-curly-in-string
artifactName: '${productName}.${ext}',
dmg: {
contents: [
{
Expand Down