99 lines
2.4 KiB
JavaScript
Executable File
99 lines
2.4 KiB
JavaScript
Executable File
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');
|
|
|
|
|
|
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 {
|
|
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.Extract({path: this.path}));
|
|
} else if (this.link.endsWith('tar.gz')) {
|
|
stream = stream.pipe(tar.x({C: this.path}));
|
|
} else {
|
|
stream = stream.pipe(fs.createWriteStream(this.path));
|
|
}
|
|
return ps.wait(stream);
|
|
}
|
|
}
|
|
}
|
|
|
|
Sdk.System = System;
|
|
|
|
module.exports = Sdk; |