-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
867 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
const request = require('request'), | ||
os = require('os'), | ||
fs = require('fs'), | ||
exec = require('child_process').exec, | ||
packageJson = require('./package.json'); | ||
|
||
// Mapping from Node's `process.arch` to Golang's `$GOARCH` | ||
const ARCH_MAPPING = { | ||
ia32: '386', | ||
x64: 'amd64', | ||
arm: 'arm', | ||
arm64: 'arm64' | ||
}; | ||
|
||
// Mapping between Node's `process.platform` to Golang's | ||
const PLATFORM_MAPPING = { | ||
darwin: 'darwin', | ||
linux: 'linux', | ||
win32: 'windows', | ||
freebsd: 'freebsd' | ||
}; | ||
|
||
const name = 'safebox'; | ||
const version = packageJson.version; | ||
const platform = process.platform; | ||
const arch = process.arch; | ||
const binaryName = platform === 'win32' ? `${name}.ext` : name; | ||
const tarUrl = `https://github.com/adikari/safebox/releases/download/v${version}/safebox_${version}_${platform}_${arch}.tar.gz`; | ||
const nodeModulesBin = './node_modules/.bin'; | ||
|
||
const error = msg => { | ||
console.error(msg); | ||
process.exit(1); | ||
}; | ||
|
||
if (!(arch in ARCH_MAPPING)) { | ||
error(`${name} is not supported for this architecture: ${arch}`); | ||
return; | ||
} | ||
|
||
if (!(platform in PLATFORM_MAPPING)) { | ||
error(`${name} is not supported for this platform: ${platform}`); | ||
return; | ||
} | ||
|
||
const ensureDir = dir => { | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
} | ||
|
||
return dir; | ||
}; | ||
|
||
const install = () => { | ||
const tmpdir = os.tmpdir(); | ||
const req = request({ uri: tarUrl }); | ||
|
||
const tarFile = `${tmpdir}/${name}.tar.gz`; | ||
const download = fs.createWriteStream(tarFile); | ||
|
||
req.on('response', res => { | ||
if (res.statusCode !== 200) { | ||
return callback('Error downloading safebox binary. HTTP Status Code: ' + res.statusCode); | ||
} | ||
|
||
req.pipe(download); | ||
}); | ||
|
||
req.on('complete', () => { | ||
exec(`tar -xf ${tarFile} -C ${tmpdir}`, error => { | ||
if (error) { | ||
error(`error: ${error.message}`); | ||
} | ||
const dest = ensureDir(nodeModulesBin); | ||
fs.copyFileSync(`${tmpdir}/${binaryName}`, `${dest}/${binaryName}`); | ||
}); | ||
}); | ||
}; | ||
|
||
install(); |
Oops, something went wrong.