diff --git a/github.js b/github.js index c1f18eb..e131035 100644 --- a/github.js +++ b/github.js @@ -1 +1,42 @@ -let fetchLatest = async () => {} \ No newline at end of file +const { Octokit, App } = require("octokit"); +const {token} = require("./config/gittoken.json"); + +// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo +var octokit; + +/** + * initializes githubs octokit to handle interactions with the github api + * @param {Electron.BrowserWindow} mainWindow the main window for error handling + */ +let initGithub = async (mainWindow) => { + + octokit = new Octokit({auth: token}); + + mainWindow.webContents.send("github-lookup", "logging in..."); + + const { data: { login }, } = await octokit.rest.users.getAuthenticated().catch(async err => { + if (err) { + console.log(err); + mainWindow.webContents.send("error-message", {label: "authentication failed", err: err}) + } + }); +} + +/** + * fetches the latest release on a set repository + * @param {Electron.BrowserWindow} mainWindow the main window for error handling + */ +let fetchLatest = async (mainWindow, repo) => { + let releases = await octokit.rest.repos.getLatestRelease(repo).catch(async err => { + mainWindow.webContents.send("github-lookup", "no releases found"); + }); + + if (releases != undefined) { + console.log(releases.data.tag_name); + if (releases.data.tag_name != "0.0.0") { + mainWindow.webContents.send("github-update", {"tag": releases.data.tag_name}); + } + } +} + +module.exports = {initGithub, fetchLatest} \ No newline at end of file diff --git a/index.html b/index.html index 94400e3..d5a9671 100644 --- a/index.html +++ b/index.html @@ -11,10 +11,10 @@
NCOS Companion
-

Latest Version: V0.0.0

+

Latest Version: 0.0.0

-

Installed: V0.0.0

+

Installed: 0.0.0

diff --git a/index.js b/index.js index a9988e1..50d0f8c 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,9 @@ // Modules to control application life and create native browser window const { app, BrowserWindow, webContents, ipcRenderer } = require('electron') -const { Octokit, App } = require("octokit"); const path = require('path') -const {token} = require("./config/gittoken.json"); - -// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo -const octokit = new Octokit({auth: token}); +const {initGithub, fetchLatest} = require("./github") var mainWindow; - const createWindow = () => { // Create the browser window. mainWindow = new BrowserWindow({ @@ -32,26 +27,15 @@ const createWindow = () => { mainWindow.loadFile('index.html') mainWindow.on("ready-to-show", async () => { - initGithub() - }) + initGithub(mainWindow); + fetchLatest(mainWindow, { + owner: "Vortex-Dynamics", + repo: "NervUpdaterClient" + }); + }); // Open the DevTools. - // mainWindow.webContents.openDevTools() -} - -async function initGithub (){ - console.log(`token: %s`, token) - mainWindow.webContents.send("github-lookup", "looking up releases"); - // Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user - const { data: { login }, } = await octokit.rest.users.getAuthenticated().catch(async err => { - if (err) { - console.log(err); - mainWindow.webContents.send("error-message", {label: "authentication failed", err: err}) - } - }); - console.log("Hello, %s", login); - mainWindow.webContents.send("github-lookup", "no releases found"); - + mainWindow.webContents.openDevTools() } // This method will be called when Electron has finished diff --git a/preload.js b/preload.js index abe7a1f..785e562 100644 --- a/preload.js +++ b/preload.js @@ -4,8 +4,9 @@ const { contextBridge, ipcRenderer } = require('electron') contextBridge.exposeInMainWorld('electronAPI', { handleError: (callback) => ipcRenderer.on("error-message", callback), - updateProgress: (callback) => ipcRenderer.on('github-lookup', callback) -}) + updateProgress: (callback) => ipcRenderer.on('github-lookup', callback), + updateFound: (callback) => ipcRenderer.on('github-update', callback) +}); window.addEventListener('DOMContentLoaded', () => { const replaceText = (selector, text) => { @@ -16,4 +17,4 @@ window.addEventListener('DOMContentLoaded', () => { for (const dependency of ['chrome', 'node', 'electron']) { replaceText(`${dependency}-version`, process.versions[dependency]) } -}) \ No newline at end of file +}); \ No newline at end of file diff --git a/render.js b/render.js index 2845203..9c88a67 100644 --- a/render.js +++ b/render.js @@ -1,8 +1,11 @@ window.electronAPI.updateProgress((_event, value) => { - console.log(_event) - console.log(value) updateProgressStep(value); -}) +}); + +window.electronAPI.updateFound((_event, value) => { + toggleDownloadButton(true); + updateLatestHeader(value.tag); +}); window.electronAPI.handleError((_event, value) => { let error = ` @@ -20,7 +23,15 @@ window.electronAPI.handleError((_event, value) => { `; document.querySelector("body").innerHTML = error; -}) +}); + +function updateLatestHeader(version) { + document.getElementById("latest").innerHTML = version; +} + +function updateCurrentHeader(version) { + document.getElementById("installed").innerHTML = version; +} function updateProgressBar(progress){ document.getElementById("progress-bar").style.width = progress + "%";