const os = require('os'); const path = require('path'); 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 fse = require('fs-extra'); 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 || path.join(os.homedir(), 'sdk'); } static path(name, version) { return path.join(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(strip=1) { log(this.tag, `version: ${colors.magenta(this.version)}`); if (this.prepared) { return Promise.resolve(); } else { fse.ensureDirSync(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')) { stream = stream.pipe(unzip.Parse()).on('entry', (entry) => { const filePath = entry.path.split('/').slice(1).join(path.sep); if (filePath.length > 0) { if (entry.type === 'Directory') { fse.ensureDirSync(path.join(this.path, path.normalize(filePath))); } else if (entry.type === 'File') { entry.pipe(fs.createWriteStream(path.join(this.path, path.normalize(filePath)))); } } else { entry.autodrain(); } }); } else if (this.link.endsWith('tar.gz')) { stream = stream.pipe(tar.x({C: this.path, strip: strip})); } else { stream = stream.pipe(fs.createWriteStream(this.path)); } return ps.wait(stream); } } } Sdk.System = System; module.exports = Sdk;