const os = require('os'); const fs = require('fs'); const ps = require('promise-streams'); const got = require('got'); const unzip = require('unzip-stream'); const ProgressBar = require('progress'); const colors = require('ansi-colors'); const log = require('fancy-log'); class Sdk { static set dir(value) { Sdk._dir = value } static get dir() { return Sdk._dir || `${os.homedir()}/sdk`; } static path(name, version) { return `${this.dir}/${name}/${version}`; } constructor(name, version) { this.name = name; this.tag = colors.green(`[${name}]`); this.version = version; this.path = Sdk.path(name, version); } get intVersion() { const vArr = this.version.split('\.').reverse(); let m = 1; let r = 0; for (let v of vArr) { r += parseInt(v) * m; m *= 10; } return r; } get prepared() { throw "Not implemented"; } get link() { throw "Not implemented"; } prepare() { log(this.tag, `version: ${colors.magenta(this.version)}`); if (this.prepared) { return Promise.resolve(); } else { const bar = new ProgressBar(`${this.tag} [:bar] :percent :etas`, {width: 40, total: 1000, clear: true}); let stream = got.stream(this.link); stream = stream.on('downloadProgress', (p) => bar.update(p.percent)); //stream = stream.pipe(fs.createWriteStream(this.path + '.zip')); if (this.link.endsWith('.zip')) { stream = stream.pipe(unzip.Extract({path: this.path})); } else if (this.link.endsWith('tar.gz')) { // ToDo: unpack tar.gz //stream = stream.pipe(gunzip()).pipe(untar()) } else { stream = stream.pipe(fs.createWriteStream(this.path)); } return ps.wait(stream); } } } module.exports = Sdk;