From 7a4bf443e10a5cc4c0ed4939593b19ac10947d1c Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Sat, 24 Sep 2016 20:42:23 -0700 Subject: [PATCH] Initial commit --- .babelrc | 3 ++ .gitignore | 5 +++ package.json | 36 +++++++++++++++++++++ src/index.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 .babelrc create mode 100644 .gitignore create mode 100644 package.json create mode 100644 src/index.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..af0f0c3 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d943c01 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +lib/ +.deps/ +.idea/ +*.log diff --git a/package.json b/package.json new file mode 100644 index 0000000..1121a69 --- /dev/null +++ b/package.json @@ -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 ", + "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" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..73cebbd --- /dev/null +++ b/src/index.js @@ -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); + }); \ No newline at end of file