Compare commits

..

6 Commits

9 changed files with 124 additions and 31 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
config
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

42
github.js Normal file
View File

@ -0,0 +1,42 @@
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}

View File

@ -11,21 +11,20 @@
<body>
<div class="titlebar">NCOS Companion</div>
<div class="content">
<h1 class="centered">Latest Version: V<span id="latest">0.0.0</span></h1>
<h1 class="centered">Latest Version: <span id="latest">0.0.0</span></h1>
<div class="row">
<div class="col-5">
<h4 class="centered">Installed: V<span id="installed">0.0.0</span></h4>
<h4 class="centered">Installed: <span id="installed">0.0.0</span></h4>
<button class="download" id="download">Update to V<span id="latest">0.0.0</span></button>
</div>
<div class="col-7" id="download-section">
<h4 class="centered" id="progress-step">Downloading...</h4>
<div id="progress-bar"><span id="progress-label">100%</span></div>
<h4 class="centered" id="progress-step">idling<h4>
<div id="progress-bar"><span id="progress-label">0%</span></div>
</div>
</div>
</div>
</div>
<!-- You can also require other files to run in this process -->
<script src="./render.js"></script>
<script src="./renderer.js"></script>
</body>
</html>

View File

@ -1,23 +1,15 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const { Octokit, App } = require("octokit");
const { app, BrowserWindow, webContents, ipcRenderer } = require('electron')
const path = require('path')
const {githubtoken} = require("./package.json");
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: });
// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
const { data: { login } } = await octokit.rest.users.getAuthenticated({auth: githubtoken});
console.log("Hello, %s", login);
const {initGithub, fetchLatest} = require("./github")
var mainWindow;
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: 600,
height: 300,
backgroundColor: '#2f3241',
symbolColor: '#74b1be',
resizable: false,
titleBarStyle: 'hidden',
titleBarOverlay: true,
@ -34,20 +26,30 @@ const createWindow = () => {
// and load the index.html of the app.
mainWindow.loadFile('index.html')
mainWindow.on("ready-to-show", async () => {
initGithub(mainWindow);
fetchLatest(mainWindow, {
owner: "Vortex-Dynamics",
repo: "NervUpdaterClient"
});
});
// Open the DevTools.
// mainWindow.webContents.openDevTools()
mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
app.whenReady().then(async () => {
createWindow()
app.on('activate', () => {
app.on('activate', async () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
})

View File

@ -3,7 +3,6 @@
"version": "1.0.0",
"description": "update companion for ncos dvised",
"main": "index.js",
"githubtoken": "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",
"scripts": {
"start": "electron ."
},

View File

@ -1,5 +1,13 @@
// All the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
handleError: (callback) => ipcRenderer.on("error-message", callback),
updateProgress: (callback) => ipcRenderer.on('github-lookup', callback),
updateFound: (callback) => ipcRenderer.on('github-update', callback)
});
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
@ -9,4 +17,4 @@ window.addEventListener('DOMContentLoaded', () => {
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency])
}
})
});

View File

@ -1,3 +1,38 @@
window.electronAPI.updateProgress((_event, value) => {
updateProgressStep(value);
});
window.electronAPI.updateFound((_event, value) => {
toggleDownloadButton(true);
updateLatestHeader(value.tag);
});
window.electronAPI.handleError((_event, value) => {
let error = `
<div class="titlebar">NCOS Companion</div>
<div class="content">
<h1 class="centered">An Error Occured</h1>
<div class="row">
<div class="col-12">
<h4 class="centered">${value.label}</h4>
<h4 class="centered">pleas report this to <a class="link" target="_blank" href="https://github.com/Vortex-Dynamics/NervUpdaterClient/issues">us</a></h4>
<pre class="centered">${value.err}</pre>
</div>
</div>
</div>
`;
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 + "%";
document.getElementById("progress-label").textContent = progress + "%";

View File

@ -1,8 +0,0 @@
// You can also put expose this code to the renderer
// process with the `contextBridge` API
const { ipcRenderer } = require('electron')
// render proccess receiver for github api data
ipcRenderer.on('github-lookup', (_event, arg) => {
console.log(arg) // prints "pong" in the DevTools console
});

View File

@ -13,6 +13,13 @@ h4 {
margin: 0;
margin-bottom: 10px;
}
pre {
border: 1px solid #74b1be;
border-radius: 5px;
padding: 5px;
}
.content {
padding-left: 15.5%;
padding-right: 15.5%;
@ -35,6 +42,7 @@ h4 {
width: 90%;
font-size: 15px;
height: 33px;
display: none;
}
.download:hover {
@ -45,6 +53,7 @@ h4 {
width: 100%;
border-radius: 5px;
border: 1px solid #74b1be;
display: none;
}
#progress-bar {
@ -52,18 +61,24 @@ h4 {
height: 33px;
border-radius: 5px;
background-color: #74b1be;
display:none;
}
#progress-label {
position: absolute;
color: white;
padding: 1.5%;
display: none;
}
div[class^=col-] {
padding: 1rem 0
}
.link {
color: #74b1be;
}
.row {
display: flex;
flex-wrap: wrap