Last active
December 10, 2018 14:35
-
-
Save jurekbarth/368fa6a41188f2d951635b9666b5e856 to your computer and use it in GitHub Desktop.
Node install binary to NPM folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const os = require('os'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const https = require('https'); | |
| const unzip = require('unzip-stream'); | |
| const zlib = require('zlib'); | |
| const platform = os.platform(); | |
| const arch = os.arch(); | |
| const platformArch = { | |
| 'x64': 'amd64' | |
| } | |
| const repo = 'jurekbarth/rpgo'; | |
| const version = 'v0.1.3'; | |
| const projectname = 'rpgo'; | |
| let retry = 0; | |
| const destinationPath = path.resolve(__dirname, 'node_modules/.bin'); | |
| const downloadUrl = `https://github.com/jurekbarth/rpgo/releases/download/v0.1.3/rpgo_0.1.3_Windows_x86_64.zip`; | |
| let writeTo = `${destinationPath}/${projectname}`; | |
| if (platform === 'win32') { | |
| writeTo = `${destinationPath}/${projectname}.exe`; | |
| } | |
| // create a bin directory if one doesn't exist | |
| fs.access(destinationPath, (err) => { | |
| if (err) { | |
| if (err.code === "ENOENT") { | |
| console.log('creating bin directory'); | |
| fs.mkdirSync(destinationPath); | |
| } else { | |
| throw err; | |
| } | |
| } else { | |
| console.log(`${destinationPath} directory already exists`); | |
| }; | |
| }); | |
| const download = (uri, dest) => { | |
| if (retry === 3) { | |
| console.log("Retried 3 times. Sorry."); | |
| return; | |
| } | |
| console.log("Downloading " + platform + " " + arch + " executable...") | |
| https.get(uri, function (res) { | |
| // github will redirect | |
| if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location) { | |
| uri = res.headers.location; | |
| console.log("Redirecting and retrying ...") | |
| download(uri, dest); | |
| } else if (res.statusCode === 200) { | |
| saveAndUnzip(res, uri, dest); | |
| } else { | |
| // something bad happened, return the status code and exit. | |
| console.log(`could not download zip archive: ${res.statusCode}, ... retrying`); | |
| retry++; | |
| download(uri, dest); | |
| } | |
| }); | |
| }; | |
| const saveAndUnzip = (response, uri, dest) => { | |
| console.log("Extracting ...") | |
| console.log(dest) | |
| const file = fs.createWriteStream(dest); | |
| const str = response.pipe(unzip.Parse()); | |
| str.on('entry', (f) => { | |
| console.log(f.path); | |
| if (f.path === 'rpgo.exe') { | |
| f.pipe(file); | |
| } | |
| }); | |
| str.on('error', (err) => { | |
| console.log(err); | |
| }); | |
| file.on('finish', () => { | |
| // close the file | |
| file.close(() => { | |
| console.log("Done!"); | |
| }); | |
| }); | |
| // // something went wrong. unlink the file. | |
| // file.on('error', () => { | |
| // fs.unlink(file); | |
| // console.log("Something went wrong while downloading or unzipping ...Retrying"); | |
| // retry++; | |
| // download(uri, dest); | |
| // }); | |
| } | |
| download(downloadUrl, writeTo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment