Skip to content

Commit

Permalink
Added download with progress, and new version check
Browse files Browse the repository at this point in the history
  • Loading branch information
mqxf committed Sep 24, 2022
1 parent e2d294e commit 15cdb39
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 16 deletions.
19 changes: 10 additions & 9 deletions .mvc/config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "mvc",
"language": "ts",
"type": "app",
"framework": "none",
"author": "Maxim Savenkov",
"git": true,
"gitLink": "https://github.com/TeamMV/mvc.git",
"licence": "MIT"
}
"name": "mvc",
"language": "ts",
"type": "app",
"framework": "none",
"author": "Maxim Savenkov",
"git": true,
"gitLink": "https://github.com/TeamMV/mvc.git",
"licence": "MIT",
"frameworkVersion": "v0.0.0"
}
2 changes: 1 addition & 1 deletion .mvc/scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"name": "build",
"script": "bWFrZQpzdWRvIG12IGJpbi9tdmMgL3Vzci9sb2NhbC9iaW4vLgo=",
"script": "bWFrZQpzdWRvIG12IGJpbi9tdmMgL3Vzci9iaW4vLgo=",
"type": "sh",
"args": 0
}
Expand Down
4 changes: 3 additions & 1 deletion src/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function createProject(args: string[]) {
},
{
name: "None",
value: ""
value: "none"
}
]
}]);
Expand Down Expand Up @@ -230,5 +230,7 @@ export async function createProject(args: string[]) {
case "java":
finalizeJava(setup);
break;
case "ts":
break;
}
}
4 changes: 4 additions & 0 deletions src/create/java.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Input, prompt } from "https://deno.land/x/[email protected]/prompt/mod.ts";
import { ConfigFile, ScriptsFile, writeConfig, writeScripts } from "../file.ts";
import { updateVersion } from "../update/repos.ts";
import { shScript, sh } from "../utils.ts";

import { Setup } from "./create.ts";
Expand Down Expand Up @@ -68,6 +69,7 @@ export async function finalizeJava(setup: Setup) {
language: "java",
type: setup.type,
framework: setup.framework,
frameworkVersion: "none",
author: setup.author,
git: setup.git,
gitLink: setup.gitLink,
Expand All @@ -92,4 +94,6 @@ export async function finalizeJava(setup: Setup) {

await writeConfig(config);
await writeScripts(scripts);

await updateVersion("java", config.framework);
}
5 changes: 4 additions & 1 deletion src/create/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Confirm, Input, Select, prompt } from "https://deno.land/x/[email protected]/prompt/mod.ts";
import { writeConfig, writeScripts } from "../file.ts";
import { printSetupHelpMenu } from "../help.ts";
import { getRepo } from "../update/repos.ts";
import { getVersion } from "../update/repos.ts";
import { sh } from "../utils.ts";

export interface SetupEmpty {
Expand Down Expand Up @@ -169,7 +171,7 @@ export async function setupProject(args: string[]) {
},
{
name: "None",
value: ""
value: "none"
}
]
}]);
Expand All @@ -185,6 +187,7 @@ export async function setupProject(args: string[]) {
language: setup.language!,
type: setup.type!,
framework: setup.framework!,
frameworkVersion: await getVersion(getRepo(setup.language, setup.framework)),
author: setup.author!,
git: setup.git!,
gitLink: setup.gitLink!,
Expand Down
97 changes: 97 additions & 0 deletions src/download/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// deno-lint-ignore-file prefer-const ban-types
import ProgressBar from "https://deno.land/x/[email protected]/mod.ts";
import { bgWhite, green, } from "https://deno.land/[email protected]/fmt/colors.ts";
import { Buffer } from "https://deno.land/[email protected]/io/buffer.ts";

export interface Destination {
dir?: string,
file?: string,
mode?: number
}

export interface DownlodedFile {
file: string,
dir: string,
fullPath: string,
size: number
}

export async function download(url: string | URL, destination?: Destination, options?: RequestInit) {
let file: string;
let fullPath: string;
let dir = '';
let mode: object = {};
let finalUrl: string;
let size: number;

const response = await fetch(url, options);
finalUrl = response.url.replace(/\/$/, "");
if (response.status != 200) {
return Promise.reject(
new Deno.errors.Http(`status ${response.status}-'${response.statusText}' received instead of 200`)
);
}

if (response == null) {
return;
}

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

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

const progress = new ProgressBar({
total: contentLength,
preciseBar: [
bgWhite(green("▏")),
bgWhite(green("▎")),
bgWhite(green("▍")),
bgWhite(green("▌")),
bgWhite(green("▋")),
bgWhite(green("▊")),
bgWhite(green("▉")),
]
});

let receivedLength = 0;

while (true) {
const { done, value } = await reader.read();

if (done) {
break;
}

receivedLength += value.length;

progress.render(Math.floor(receivedLength / 1024));
}

const blob = await response.blob();
size = blob.size;
const buffer = await blob.arrayBuffer();
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;
}

if (typeof destination != 'undefined' && typeof destination.mode != 'undefined') {
mode = { mode: destination.mode }
}

dir = dir.replace(/\/$/, "");

fullPath = `${dir}/${file}`;
Deno.writeFileSync(fullPath, unit8arr, mode);
return Promise.resolve({ file, dir, fullPath, size });
}
1 change: 1 addition & 0 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface ConfigFile {
language: string;
type: string;
framework: string;
frameworkVersion: string;
author: string;
git: boolean;
gitLink: string;
Expand Down
28 changes: 24 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,68 @@ import { printMainHelpMenu } from "./help.ts";
import { build, commit, other, push } from "./script/basic.ts";
import { runScript } from "./script/run.ts";
import { editScript } from "./script/script.ts";
import { update } from "./update/update.ts";
import { checkVersion, upgrade } from "./update/upgrade.ts";
import { checkDir, info } from "./utils.ts";

async function main(args: string[]) {
if (args.length < 1) {
console.log("No command specified. Run 'mv help' for a list of commands.");
console.log("No command specified. Run 'mvc help' for a list of commands.");
return;
}
if (args[0] == "help") {
printMainHelpMenu();
return;
}
switch (args[0]) {
case "help":
printMainHelpMenu();
break;
case "create":
await checkVersion();
await createProject(args);
break;
case "setup":
await checkVersion();
await setupProject(args);
break;
case "upgrade":
upgrade();
break;
case "info":
await checkVersion();
checkDir();
info();
break;
case "update":
await checkVersion();
checkDir();
update();
break;
case "run":
await checkVersion();
checkDir();
await runScript(args);
break;
case "script":
await checkVersion();
checkDir();
await editScript(args);
break;
case "push":
await checkVersion();
checkDir();
await push(args);
break;
case "commit":
await checkVersion();
checkDir();
await commit(args);
break;
case "build":
await checkVersion();
checkDir();
await build(args);
break;
default:
await checkVersion();
checkDir();
await other(args);
break;
Expand Down
Loading

0 comments on commit 15cdb39

Please sign in to comment.