UNPKG

goes-install

Version:

Install GoES for your platform using npm

110 lines (89 loc) 3.34 kB
'use strict'; var _https = require('https'); var _https2 = _interopRequireDefault(_https); var _os = require('os'); var _os2 = _interopRequireDefault(_os); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _decompress = require('decompress'); var _decompress2 = _interopRequireDefault(_decompress); var _mkdirp = require('mkdirp'); var _mkdirp2 = _interopRequireDefault(_mkdirp); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var config = { versionToDownload: '20170211', urlBase: 'https://nicdex.com/files/goes/' }; // supported platforms: darwin, linux, win32 // supported architecture: x64 function detect() { console.log('Detecting platform...'); var _process = process, platform = _process.platform, arch = _process.arch; 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.')); } var ext = platform === 'win32' ? 'zip' : 'tar.gz'; var versionToDownload = config.versionToDownload, urlBase = config.urlBase; var fileName = 'goes-' + platform + '-' + arch + '-' + versionToDownload + '.' + ext; var tmpdir = _os2.default.tmpdir(); var pathsep = _path2.default.sep; var options = { platform: platform, arch: arch, ext: ext, fileName: 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(function (resolve, reject) { console.log('Downloading archive from server...'); var remoteUrl = options.remoteUrl, localPath = options.localPath, fileName = options.fileName; var fileStream = _fs2.default.createWriteStream(localPath); fileStream.on('error', function (err) { return reject(err); }); fileStream.on('finish', function () { console.log('- Downloaded ' + fileName + '\n'); resolve(options); }); _https2.default.get(remoteUrl, function (res) { if (res.statusCode !== 200) return reject(new Error('Request failed: GET ' + remoteUrl + ' returned ' + res.statusCode + '.')); res.pipe(fileStream); }).on('error', function (err) { reject(new Error('Failed to download archive from ' + remoteUrl + '. ' + err)); }); }); } function install(options) { return new Promise(function (resolve, reject) { console.log('Installing GoES...'); var destParts = ['.deps', 'goes']; if (options.platform === 'win32') { destParts.push('bin'); } var destPath = _path2.default.resolve(destParts.join(_path2.default.sep)).replace('/node_modules/goes-install', ''); console.log('Installing to', destPath); (0, _mkdirp2.default)(destPath, function (err) { if (err) return reject(err); (0, _decompress2.default)(options.localPath, destPath).then(resolve, reject); }); }); } detect().then(download).then(install).catch(function (err) { console.log(err.message); process.exit(-1); });