From 12eb5de20dcc2180a095ac38e41fceed8ce6854d Mon Sep 17 00:00:00 2001 From: srborines Date: Fri, 25 Mar 2022 13:35:41 +0100 Subject: [PATCH 1/2] Added private repo token access --- index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c00987a..5eea70c 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ const fs = require('fs'); var app_library = (process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share")) + "\\"; var git_api; +var git_headers; var new_version = "unknown"; var current_version = "unknown"; @@ -95,6 +96,10 @@ function setOptions(options) { function setupGitProtocol(options) { options.gitRepo = options.gitRepo.toString().replace('/ /gi', '-'); git_api = `https://api.github.com/repos/${options.gitUsername}/${options.gitRepo}/releases/latest`; + git_headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36' }; + if(options.isGitRepoPrivate) { + git_headers['Authorization'] = `token ${options.gitRepoToken}` + } } @@ -125,7 +130,7 @@ function createDirectories(options) { */ async function GetUpdateURL(options) { - return fetch(git_api).then(response => response.json()).then(data => { json = data; }).catch(e => { + return fetch(git_api, {headers: git_headers}).then(response => response.json()).then(data => { json = data; }).catch(e => { try { // Electron alert(`Something went wrong: ${e}`); @@ -139,7 +144,7 @@ async function GetUpdateURL(options) { for (i = 0; i < json['assets'].length; i++) { if (json['assets'][i]['name'] === `${options.appName}.zip`) zip = json['assets'][i]; } - return zip['browser_download_url']; + return zip['url']; }); } @@ -147,7 +152,8 @@ async function GetUpdateURL(options) { * Gets the current relase version from GitHub */ async function GetUpdateVersion() { - return fetch(git_api).then(response => response.json()).then(data => { json = data; }).catch(e => { + + return fetch(git_api, {headers: git_headers}).then(response => response.json()).then(data => { json = data; }).catch(e => { try { // Electron alert(`Something went wrong: ${e}`); @@ -286,9 +292,11 @@ function Download(url, path, options) { updateHeader(options.stageTitles.Downloading) let received_bytes = 0; let total_bytes = 0; + let headers = {...git_headers, 'Accept': 'application/octet-stream'}; var req = request( { + headers, method: 'GET', uri: url } From 16b41c97242f885c6b6b49e344f80dc29a46cc5e Mon Sep 17 00:00:00 2001 From: srborines Date: Fri, 25 Mar 2022 15:31:54 +0100 Subject: [PATCH 2/2] addapt slash to windows/linux and chmod command at linux --- index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 5eea70c..e5e28a4 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,8 @@ const fs = require('fs'); //////////////////// GLOBAL VARIABLES //////////////////// //#region GLOBAL VARIABLES -var app_library = (process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share")) + "\\"; +var slash = (process.platform == 'win32') ? "\\" : "/"; +var app_library = (process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + `${slash}.local${slash}share`)) + slash; var git_api; var git_headers; @@ -38,8 +39,8 @@ const defaultOptions = { appExecutableName: this.appName + "", appDirectory: app_library + this.appName, - versionFile: this.appDirectory + "/settings/version.json", - tempDirectory: this.appDirectory + "/tmp", + versionFile: `${this.appDirectory}${slash}settings${slash}version.json`, + tempDirectory: `${this.appDirectory}${slash}tmp`, progressBar: null, label: null, @@ -72,8 +73,8 @@ function setOptions(options) { options.useGithub = options.useGithub == null ? true : options.appDirectory; options.forceUpdate = options.forceUpdate == null ? false : options.forceUpdate; options.appDirectory = options.appDirectory == null ? app_library + options.appName : options.appDirectory; - options.versionFile = options.versionFile == null ? options.appDirectory + "\\settings\\version.json" : options.versionFile; - options.tempDirectory = options.tempDirectory == null ? options.appDirectory + "\\tmp" : options.tempDirectory; + options.versionFile = options.versionFile == null ? options.appDirectory + `${slash}settings${slash}version.json` : options.versionFile; + options.tempDirectory = options.tempDirectory == null ? options.appDirectory + `${slash}tmp` : options.tempDirectory; options.appExecutableName = options.appExecutableName == null ? options.appName : options.appExecutableName; options.stageTitles = options.stageTitles == null ? defaultOptions.stageTitles : options.stageTitles; @@ -237,7 +238,7 @@ async function Update(options = defaultOptions) { updateHeader(options.stageTitles.Found); await sleep(1000); let url = await GetUpdateURL(options); - Download(url, `${options.tempDirectory}\\${options.appName}.zip`, options); + Download(url, `${options.tempDirectory}${slash}${options.appName}.zip`, options); UpdateCurrentVersion(options); } else { updateHeader(options.stageTitles.NotFound); @@ -325,7 +326,7 @@ function Download(url, path, options) { function Install(options) { updateHeader(options.stageTitles.Unzipping); var AdmZip = require('adm-zip'); - var zip = new AdmZip(`${options.tempDirectory}/${options.appName}.zip`); + var zip = new AdmZip(`${options.tempDirectory}${slash}${options.appName}.zip`); zip.extractAllTo(options.appDirectory, true); setTimeout(() => CleanUp(options), 2000); @@ -350,7 +351,9 @@ function LaunchApplication(options) { if (fs.existsSync(executablePath)) { updateHeader(options.stageTitles.Launch); let child = require('child_process').exec; - child(`"${executablePath}"`, function (err, data) { + let command = `"${executablePath}"`; + if (process.platform == 'linux') command = `chmod +x ${executablePath}; ${command}` + child(command, function (err, data) { if (err) { console.error(err); return;