Skip to content

Commit

Permalink
Update checking and pulling from github
Browse files Browse the repository at this point in the history
  • Loading branch information
mqxf committed Sep 24, 2022
1 parent 15cdb39 commit 7d01f8c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
42 changes: 32 additions & 10 deletions src/download/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export async function download(url: string | URL, destination?: Destination, opt
return;
}

const reader = response.body!.getReader();
const body = response.clone().body!;
const reader = body.getReader();

const contentLength = Math.floor(parseInt(response.headers.get('Content-Length')!) / 1024);

Expand All @@ -50,21 +51,42 @@ export async function download(url: string | URL, destination?: Destination, opt
bgWhite(green("▋")),
bgWhite(green("▊")),
bgWhite(green("▉")),
]
],
display: ':percent :bar :time (:completed/:total) :title'
});
let now = Date.now();
let last = now;
let lenStr = '0.0KiB';
let speedStr = '0.0KiB/s';
let lastKiB = 0;

let receivedLength = 0;

while (true) {
const { done, value } = await reader.read();
const interval = Date.now() - last;
if (interval > 100) {
const { done, value } = await reader.read();

if (done) {
break;
}
if (done) {
break;
}

receivedLength += value.length;
lastKiB += value.length;

last = now;
now = Date.now();

receivedLength += value.length;
const kiBLen = receivedLength / 1024;
lenStr = kiBLen < 1024 ? `${kiBLen.toFixed(2)}KiB` : (kiBLen < 1048576 ? `${(kiBLen / 1024).toFixed(2)}MiB` : `${(receivedLength / 1048576).toFixed(2)}GiB`);

progress.render(Math.floor(receivedLength / 1024));
const speed = (lastKiB / 1024) / (interval / 1000);
lastKiB = 0;
speedStr = speed < 1024 ? `${speed.toFixed(2)}KiB/s` : (speed < 1048576 ? `${(speed / 1024).toFixed(2)}MiB/s` : `${(speed / 1048576).toFixed(2)}GiB/s`);
}
progress.render(Math.floor(receivedLength / 1024), {
title: `${lenStr} | ${speedStr}`
});
}

const blob = await response.blob();
Expand All @@ -73,14 +95,14 @@ export async function download(url: string | URL, destination?: Destination, opt
const unit8arr = new Buffer(buffer).bytes();
if (typeof destination === 'undefined' || typeof destination.dir === 'undefined') {
dir = Deno.makeTempDirSync({ prefix: 'deno_dwld' });
}
}
else {
dir = destination.dir;
}

if (typeof destination === 'undefined' || typeof destination.file === 'undefined') {
file = finalUrl.substring(finalUrl.lastIndexOf('/') + 1);
}
}
else {
file = destination.file;
}
Expand Down
7 changes: 7 additions & 0 deletions src/update/repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ export async function getVersion(repo: string) {
return release.tag_name;
}

export async function getToolVersion() {
const res = await fetch(`https://files.mvteam.dev/mvc-version`, {
method: "GET"
});
return (await (await res.blob()).text()).trimEnd();
}

export async function getRelease(repo: string) {
const res = await fetch(`${repo}/releases/latest`, {
method: "GET"
Expand Down
13 changes: 7 additions & 6 deletions src/update/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Destination, download } from "../download/download.ts";
import { sh } from "../utils.ts";
import { getRelease, getVersion, repos } from "./repos.ts";
import { getRelease, getToolVersion, getVersion, repos } from "./repos.ts";

const env = {
version: "v1.1.0",
Expand All @@ -18,7 +18,7 @@ export async function upgrade() {
}

try {
console.log("Checking local versions");
console.log("Checking local versions...");
await Deno.stat("/tmp/mvc-newVersion-G63fwFw3f8w");
console.log("Local version found!");
await sh("chmod +x /tmp/mvc-newVersion-G63fwFw3f8w", true);
Expand All @@ -28,13 +28,14 @@ export async function upgrade() {
} catch (_err) {
console.log("No local versions found.");
}

const version = await getVersion(repos.mvc);
console.log("Checking external version...");
const version = await getToolVersion();
if (version != env.version) {
console.log("New version detected!");
console.log("Finding version release...");
const release = await getRelease(repos.mvc);
if (release == null) {
if (release == null || release.assets == null) {
console.log(release);
console.log("Version release not found!");
return;
}
Expand Down Expand Up @@ -72,7 +73,7 @@ export async function upgrade() {
export async function checkVersion() {
try {
console.log("Checking for new version...");
const version = await getVersion(repos.mvc);
const version = await getToolVersion();
if (version != env.version) {
console.log("New version detected!");
console.log(`
Expand Down

0 comments on commit 7d01f8c

Please sign in to comment.