Skip to content

Commit

Permalink
fix: get npm bin path correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Oct 4, 2022
1 parent 9c0894c commit d4607b7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
25 changes: 7 additions & 18 deletions npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
const request = require('request'),
os = require('os'),
fs = require('fs'),
exec = require('child_process').exec,
path = require('path'),
cp = require('child_process'),
packageJson = require('./package.json');

// Mapping from Node's `process.arch` to Golang's `$GOARCH`
Expand All @@ -30,7 +31,8 @@ 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 nodeBin = cp.execSync("npm bin").toString().replace(/\r?\n|\r/g, "");

const error = msg => {
console.error(msg);
Expand All @@ -47,14 +49,6 @@ if (!(platform in PLATFORM_MAPPING)) {
return;
}

const ensureDir = dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}

return dir;
};

const install = () => {
const tmpdir = os.tmpdir();
const req = request({ uri: tarUrl });
Expand All @@ -64,20 +58,15 @@ const install = () => {

req.on('response', res => {
if (res.statusCode !== 200) {
return callback('Error downloading safebox binary. HTTP Status Code: ' + res.statusCode);
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}`);
});
cp.execSync(`tar -xf ${tarFile} -C ${tmpdir}`);
fs.copyFileSync(path.join(tmpdir, binaryName), path.join(nodeBin, binaryName));
});
};

Expand Down
4 changes: 2 additions & 2 deletions npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adikari/safebox",
"version": "1.1.2",
"version": "1.1.3",
"description": "A Fast and Flexible secret manager built with love by adikari in Go",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit d4607b7

Please sign in to comment.