Compare commits

..

1 Commits

Author SHA1 Message Date
c20c1fee6a added github api authentification 2023-02-06 10:42:07 +01:00
9 changed files with 31 additions and 124 deletions

1
.gitignore vendored
View File

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

View File

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

View File

@ -1,15 +1,23 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow, webContents, ipcRenderer } = require('electron') const { app, BrowserWindow } = require('electron')
const { Octokit, App } = require("octokit");
const path = require('path') const path = require('path')
const {initGithub, fetchLatest} = require("./github") 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);
var mainWindow;
const createWindow = () => { const createWindow = () => {
// Create the browser window. // Create the browser window.
mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 600, width: 600,
height: 300, height: 300,
backgroundColor: '#2f3241', backgroundColor: '#2f3241',
symbolColor: '#74b1be',
resizable: false, resizable: false,
titleBarStyle: 'hidden', titleBarStyle: 'hidden',
titleBarOverlay: true, titleBarOverlay: true,
@ -26,30 +34,20 @@ const createWindow = () => {
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadFile('index.html') mainWindow.loadFile('index.html')
mainWindow.on("ready-to-show", async () => {
initGithub(mainWindow);
fetchLatest(mainWindow, {
owner: "Vortex-Dynamics",
repo: "NervUpdaterClient"
});
});
// Open the DevTools. // Open the DevTools.
mainWindow.webContents.openDevTools() // mainWindow.webContents.openDevTools()
} }
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.whenReady().then(async () => { app.whenReady().then(() => {
createWindow() createWindow()
app.on('activate', async () => { app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the // 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. // dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) { if (BrowserWindow.getAllWindows().length === 0) createWindow()
createWindow();
}
}) })
}) })

View File

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

View File

@ -1,13 +1,5 @@
// All the Node.js APIs are available in the preload process. // All the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension. // 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', () => { window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => { const replaceText = (selector, text) => {
const element = document.getElementById(selector) const element = document.getElementById(selector)
@ -17,4 +9,4 @@ window.addEventListener('DOMContentLoaded', () => {
for (const dependency of ['chrome', 'node', 'electron']) { for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency]) replaceText(`${dependency}-version`, process.versions[dependency])
} }
}); })

View File

@ -1,38 +1,3 @@
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){ function updateProgressBar(progress){
document.getElementById("progress-bar").style.width = progress + "%"; document.getElementById("progress-bar").style.width = progress + "%";
document.getElementById("progress-label").textContent = progress + "%"; document.getElementById("progress-label").textContent = progress + "%";

8
renderer.js Normal file
View File

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