-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
adding a "check for update" option to the menu
- Loading branch information
Showing
6 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.modal .updater { | ||
text-align: center; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: space-around; | ||
min-height: 100%; | ||
} | ||
|
||
.modal .updater .credit { | ||
font-size: 1.6rem; | ||
margin: 10px auto; | ||
} | ||
|
||
.modal .updater a { | ||
color: #ffda00; | ||
text-decoration: none; | ||
opacity: 0.9; | ||
} | ||
.modal .updater a:hover { | ||
opacity: 1; | ||
text-decoration: underline; | ||
} | ||
|
||
.modal .updater .head, | ||
.modal .updater .foot { | ||
flex: 0; | ||
} | ||
|
||
.modal .updater .body { | ||
flex: auto; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
padding-bottom: 30px; | ||
} | ||
|
||
.modal .updater .icon { | ||
font-size: 7rem; | ||
padding-bottom: 30px; | ||
color: currentColor; | ||
} | ||
|
||
.modal .updater .body a { | ||
margin: 10px 0; | ||
font-size: 1.2rem; | ||
font-weight: bold; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const semver = require('semver'); | ||
const fetch = require('node-fetch'); | ||
|
||
const name = 'updater'; | ||
const style = fs.readFileSync(path.resolve(__dirname, `${name}.css`), 'utf8'); | ||
const log = require('../../lib/log.js')(name); | ||
|
||
const pkg = require('../../package.json'); | ||
const dom = require('../tools/dom.js'); | ||
const appName = pkg.productName || pkg.name; | ||
const appVersion = pkg.version; | ||
|
||
const get = async (url) => { | ||
const res = await fetch(url, { | ||
headers: { 'user-agent': `Raw-Viewer-v${appVersion}` } | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(`unexpected response: ${res.status} ${res.statusText}`); | ||
} | ||
|
||
return await res.json(); | ||
}; | ||
|
||
module.exports = ({ events }) => { | ||
const elem = dom.div(name); | ||
const head = dom.div('head'); | ||
const foot = dom.div('foot'); | ||
|
||
const title = dom.h1(`${appName} v${pkg.version}`); | ||
head.appendChild(title); | ||
|
||
[ | ||
dom.link(appName, 'https://github.com/catdad/raw-viewer'), | ||
dom.text(' is made with 💛 by '), | ||
dom.link('catdad', 'https://github.com/catdad') | ||
].forEach(el => foot.appendChild(el)); | ||
|
||
events.on('check-for-update', async () => { | ||
dom.empty(elem); | ||
elem.appendChild(head); | ||
|
||
const body = dom.div('body'); | ||
|
||
try { | ||
const latest = await get('https://api.github.com/repos/catdad/raw-viewer/releases/latest'); | ||
|
||
const icon = dom.div('icon'); | ||
body.appendChild(icon); | ||
|
||
if (semver.gt(latest.tag_name, appVersion)) { | ||
icon.appendChild(dom.text('🎁')); | ||
body.appendChild(dom.p(`There is an update to Raw Viewer ${latest.tag_name}`)); | ||
body.appendChild(dom.link('Download', latest.html_url)); | ||
} else { | ||
icon.appendChild(dom.text('🚀')); | ||
body.appendChild(dom.p('You are already using the latest version of Raw Viewer.')); | ||
} | ||
} catch (e) { | ||
log.error(e); | ||
body.appendChild(dom.p('Something went wrong when checking for an update.')); | ||
body.appendChild(dom.p('Please check again later.')); | ||
} | ||
|
||
body.appendChild(dom.link('Visit the website', 'https://github.com/catdad/raw-viewer')); | ||
|
||
elem.appendChild(body); | ||
elem.appendChild(foot); | ||
|
||
events.emit('modal', { content: elem }); | ||
}); | ||
|
||
return { style }; | ||
}; |