const os = require('os'); const fs = require('fs'); const ps = require('promise-streams'); const got = require('got'); const unzip = require('unzip-stream'); const tar = require('tar'); const ProgressBar = require('progress'); const colors = require('ansi-colors'); const log = require('fancy-log'); const mkdirp = require('mkdirp'); class System { static get isWindows() { return os.type() === 'Windows_NT'; } static get isLinux() { return os.type() === 'Linux'; } static get archInt() { if (os.arch() === 'ia32') return 32; if (os.arch() === 'x64') return 64; } static get isArch32() { return this.archInt === 32; } static get isArch64() { return this.archInt === 64; } } 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 { mkdirp(this.path); 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)); if (this.link.endsWith('.zip')) { const sep = '/'; stream = stream.pipe(unzip.Parse()).on('entry', (entry) => { const filePath = entry.path.split(sep).slice(1).join(sep); if (filePath.length > 0) { if (entry.type === 'Directory') { mkdirp(this.path + sep + filePath); } else if (entry.type === 'File') { entry.pipe(fs.createWriteStream(this.path + sep + filePath)); } } else { entry.autodrain(); } }); } else if (this.link.endsWith('tar.gz')) { stream = stream.pipe(tar.x({C: this.path, strip: 1})); } else { stream = stream.pipe(fs.createWriteStream(this.path)); } return ps.wait(stream); } } } Sdk.System = System; module.exports = Sdk;