diff --git a/.goreleaser.yaml b/.goreleaser.yaml index baf6ea9..49082e5 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,7 +16,9 @@ checksum: name_template: 'checksums.txt' archives: - - files: + - format: 'binary' + name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" + files: - completions/* snapshot: diff --git a/npm/constants.js b/npm/constants.js index bd679d3..c09d828 100644 --- a/npm/constants.js +++ b/npm/constants.js @@ -21,8 +21,9 @@ const name = 'safebox'; const version = packageJson.version; const platform = PLATFORM_MAPPING[process.platform]; const arch = ARCH_MAPPING[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 ext = platform === 'win32' ? '.exe' : ''; +const binaryName = `${name}.${ext}`; +const binaryUrl = `https://github.com/adikari/safebox/releases/download/v${version}/safebox_${version}_${platform}_${arch}${ext}`; const bin = path.join(__dirname, "bin"); const constants = { @@ -32,7 +33,7 @@ const constants = { arch, binaryName, bin, - tarUrl, + binaryUrl, }; module.exports = constants; diff --git a/npm/install.js b/npm/install.js index af345b4..679dbe1 100644 --- a/npm/install.js +++ b/npm/install.js @@ -6,10 +6,9 @@ const request = require('request'), os = require('os'), fs = require('fs'), path = require('path'), - cp = require('child_process'), constants = require('./constants'); -const { name, platform, arch, binaryName, bin, tarUrl } = constants; +const { name, platform, arch, binaryName, bin, binaryUrl } = constants; if (!arch) { error(`${name} is not supported for this architecture: ${arch}`); @@ -28,17 +27,25 @@ const MAX_RETRIES = 3; const install = () => { const tmpdir = os.tmpdir(); - const req = request({ uri: tarUrl }); + const binary = path.join(tmpdir, binaryName); - const tarFile = `${tmpdir}/${name}-${Date.now()}.tar.gz`; - const download = fs.createWriteStream(tarFile); + const copyBinary = () => fs.copyFileSync(binary, path.join(bin, binaryName)); + if (fs.existsSync(binary)) { + console.log('safebox binary already downloaded'); + copyBinary(); + return; + } + + const req = request({ uri: binaryUrl }); if (retries > 0) { console.log(`retrying to install safebox - retry ${retries} out of ${MAX_RETRIES}`) } console.log(`downloading safebox binary`); + const download = fs.createWriteStream(binary); + req.on('response', res => { if (res.statusCode !== 200) { error(`Error downloading safebox binary. HTTP Status Code: ${res.statusCode}`); @@ -51,12 +58,11 @@ const install = () => { console.log('download complete. installing safebox.') try { - if (!fs.existsSync(tarFile)) { - throw new Error(`${tarFile} does not exist`) + if (!fs.existsSync(binary)) { + throw new Error(`${binary} does not exist`) } - cp.execSync(`tar -xf ${tarFile} -C ${tmpdir}`); - fs.copyFileSync(path.join(tmpdir, binaryName), path.join(bin, binaryName)); + copyBinary(); } catch (error) { console.error('failed to extract binary.', error.message) diff --git a/scripts/install.sh b/scripts/install.sh index 1fc07b2..cc1e4d6 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -25,7 +25,6 @@ download_binary_and_run_installer() { need_cmd mkdir need_cmd rm need_cmd rmdir - need_cmd tar need_cmd which need_cmd dirname need_cmd awk @@ -43,10 +42,9 @@ download_binary_and_run_installer() { esac local _current_dir=$(pwd) - local _tardir="safebox_${PACKAGE_VERSION:1}"_"${_arch}" - local _url="$BINARY_DOWNLOAD_PREFIX/$PACKAGE_VERSION/${_tardir}.tar.gz" + local _binary="safebox_${PACKAGE_VERSION:1}"_"${_arch}" + local _url="$BINARY_DOWNLOAD_PREFIX/$PACKAGE_VERSION/${_binary}$_ext" local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t test)" - local _file="$_dir/input.tar.gz" local _safebox="$_dir/safebox$_ext" local _bin_dir="/usr/local/bin" @@ -60,7 +58,6 @@ download_binary_and_run_installer() { exit 1 fi - ensure tar xf "$_file" sudo mv "$_safebox" "$_bin_dir" local _retval=$?