"use strict"; const os = require('os'); const gulp = require('gulp'); const gutil = require('gulp-util'); const col = gutil.colors; const download = require('./download'); const unzip = require('gulp-unzip'); const gunzip = require('gulp-gunzip'); const untar = require('gulp-untar'); const Promise = require('bluebird'); const ProgressBar = require('progress'); 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 = col.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() { gutil.log(this.tag, `version: ${col.magenta(this.version)}`); return new Promise((success, fail) => { if (this.prepared) { success(); } else { const bar = new ProgressBar(`${this.tag} [:bar] :percent :etas`, {width: 40, total: 1000, clear: true}); let stream = download(this.link) .on('error', fail) .on('progress', (p) => bar.update(p.percent)) .on('end', () => bar.update(1)); if (this.link.endsWith('.zip')) { stream = stream.pipe(unzip()); } else if (this.link.endsWith('tar.gz')) { stream = stream.pipe(gunzip()).pipe(untar()) } return stream .pipe(gulp.dest(this.path)) .on('end', success) .on('error', fail); } }); } } module.exports = Sdk;