88 lines
2.6 KiB
JavaScript
88 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
import https from 'https';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import unzip from 'unzip';
|
|
import mkdirp from 'mkdirp';
|
|
|
|
const config = {
|
|
versionToDownload: '20160923',
|
|
urlBase: 'https://nicdex.com/files/goes/'
|
|
};
|
|
|
|
// supported platforms: darwin, linux, win32
|
|
// supported architecture: x64
|
|
function detect() {
|
|
console.log('Detecting platform...');
|
|
const { platform, arch } = process;
|
|
if (platform !== 'win32' && platform !== 'linux') {
|
|
return Promise.reject(new Error('Only Windows and Linux are supported for now.'));
|
|
}
|
|
if (arch !== 'x64') {
|
|
return Promise.reject(new Error('Only x64 processor architecture is supported.'));
|
|
}
|
|
const ext = platform === 'win32' ? 'zip' : 'tar.gz';
|
|
const { versionToDownload, urlBase } = config;
|
|
const fileName = `goes-${platform}-${arch}-${versionToDownload}.${ext}`;
|
|
const tmpdir = os.tmpdir();
|
|
const pathsep = path.sep;
|
|
const options = {
|
|
platform,
|
|
arch,
|
|
ext,
|
|
fileName,
|
|
remoteUrl: `${urlBase}${fileName}`,
|
|
localPath: `${tmpdir}${pathsep}${fileName}`
|
|
};
|
|
console.log(`- Platform: ${platform}\n- Architecture: ${arch}\n`);
|
|
return Promise.resolve(options);
|
|
}
|
|
|
|
function download(options) {
|
|
return new Promise((resolve, reject) => {
|
|
console.log('Downloading archive from server...');
|
|
const {remoteUrl, localPath, fileName} = options;
|
|
const fileStream = fs.createWriteStream(localPath);
|
|
fileStream.on('error', err => reject(err));
|
|
fileStream.on('finish', () => {
|
|
console.log(`- Downloaded ${fileName}\n`);
|
|
resolve(options);
|
|
});
|
|
https.get(remoteUrl, (res) => {
|
|
if (res.statusCode !== 200) return reject(new Error(`Request failed: GET ${remoteUrl} returned ${res.statusCode}.`));
|
|
res.pipe(fileStream);
|
|
}).on('error', (err) => {
|
|
reject(new Error(`Failed to download archive from ${remoteUrl}. ${err}`));
|
|
});
|
|
});
|
|
}
|
|
|
|
function install(options) {
|
|
return new Promise((resolve, reject) => {
|
|
console.log('Installing GoES...');
|
|
const destParts = ['.deps', 'goes'];
|
|
if (options.platform === 'win32') {
|
|
destParts.push('bin');
|
|
}
|
|
const destPath = destParts.join(path.sep);
|
|
mkdirp(destPath, err => {
|
|
if (err) return reject(err);
|
|
fs.createReadStream(options.localPath)
|
|
.pipe(unzip.Extract({ path: destPath })
|
|
.on('finish', () => {
|
|
console.log(`- GoES ${config.version} installed in .deps${path.sep}goes\n`);
|
|
}))
|
|
.on('error', reject);
|
|
});
|
|
});
|
|
}
|
|
|
|
detect()
|
|
.then(download)
|
|
.then(install)
|
|
.catch(err => {
|
|
console.log(err.message);
|
|
process.exit(-1);
|
|
}); |