[sdk] update progress bar

This commit is contained in:
2019-08-26 23:02:16 +03:00
parent 2b703fb66a
commit bd4fc36ff1
4 changed files with 50 additions and 13 deletions

View File

@@ -5,7 +5,7 @@ const ps = require('promise-streams');
const got = require('got');
const unzip = require('unzip-stream');
const tar = require('tar');
const ProgressBar = require('progress');
const progress = require('cli-progress');
const fse = require('fs-extra');
const Log = require('./log');
@@ -37,19 +37,52 @@ class System {
class Downloader {
static download(url, dest, strip = 1) {
static createProgressBar() {
if (Downloader.bar == null) {
Downloader.bar = new progress.MultiBar({
clearOnComplete: true,
}, progress.Presets.shades_grey);
}
return Downloader.bar.create();
}
static releaseProgressBar(bar) {
bar.stop();
if (Downloader.bar != null) {
Downloader.bar.remove(bar);
if (Downloader.bar.bars.length === 0) {
Downloader.bar.stop();
Downloader.bar = null;
}
}
}
static download(url, dest, strip = 1, executable = false) {
fse.ensureDirSync(dest);
const bar = new ProgressBar(`download [:bar] :percent :etas`, {width: 40, total: 1000, clear: true});
const bar = Downloader.createProgressBar();
let stream = got.stream(url);
stream = stream.on('downloadProgress', (p) => bar.update(p.percent));
stream = stream
.on('request', r => {
bar.start(1, 0);
})
.on('downloadProgress', p => {
bar.setTotal(p.total);
bar.update(p.transferred);
if (p.total === p.transferred) {
Downloader.releaseProgressBar(bar);
}
});
if (url.endsWith('.zip')) {
stream = stream.pipe(unzip.Parse()).on('entry', (entry) => {
stream = stream.pipe(unzip.Parse()).on('entry', entry => {
const filePath = entry.path.split('/').slice(strip).join(path.sep);
if (filePath.length > 0) {
if (entry.type === 'Directory') {
fse.ensureDirSync(path.join(dest, path.normalize(filePath)));
} else if (entry.type === 'File') {
entry.pipe(fs.createWriteStream(path.join(dest, path.normalize(filePath))));
entry.pipe(fs.createWriteStream(path.join(dest, path.normalize(filePath)), {
// ToDo: zip entry file mode?
mode: executable ? 0o755 : undefined,
}));
}
} else {
entry.autodrain();
@@ -104,7 +137,7 @@ class Sdk {
throw "Not implemented";
}
prepare(strip=1) {
prepare(strip = 1) {
this.log.i('version: *%s*', this.version);
if (this.prepared) {
return Promise.resolve();