Initial commit
This commit is contained in:
commit
7a4bf443e1
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
node_modules/
|
||||||
|
lib/
|
||||||
|
.deps/
|
||||||
|
.idea/
|
||||||
|
*.log
|
36
package.json
Normal file
36
package.json
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
{
|
||||||
|
"name": "goes-install",
|
||||||
|
"version": "0.2.0",
|
||||||
|
"description": "Install GoES for your platform using npm",
|
||||||
|
"main": "lib/index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"postinstall": "node ./lib/index.js",
|
||||||
|
"build": "babel src -d lib",
|
||||||
|
"prepublish": "npm run build",
|
||||||
|
"start": "npm run build && npm run postinstall"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/nicdex/npm-goes.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"goes",
|
||||||
|
"install",
|
||||||
|
"npm"
|
||||||
|
],
|
||||||
|
"author": "Nicolas Dextraze <nicdex@yahoo.com>",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/nicdex/goes-install/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/nicdex/goes-install#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"babel-cli": "^6.14.0",
|
||||||
|
"babel-preset-es2015": "^6.14.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"mkdirp": "^0.5.1",
|
||||||
|
"unzip": "^0.1.11"
|
||||||
|
}
|
||||||
|
}
|
88
src/index.js
Normal file
88
src/index.js
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
'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);
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user