Skip to content

Commit

Permalink
fix: add binary runner script
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Oct 4, 2022
1 parent 40df877 commit 7a9fa21
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 33 deletions.
38 changes: 38 additions & 0 deletions npm/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const packageJson = require('./package.json');
const path = require('path');

// 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 = 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 bin = path.join(__dirname, "bin");

const constants = {
name,
version,
platform,
arch,
binaryName,
bin,
tarUrl,
};

module.exports = constants;
32 changes: 2 additions & 30 deletions npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,9 @@ const request = require('request'),
fs = require('fs'),
path = require('path'),
cp = require('child_process'),
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'
};
constants = require('./constants');

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 { name, platform, arch, binaryName, bin, tarUrl } = constants;

if (!arch) {
error(`${name} is not supported for this architecture: ${arch}`);
Expand All @@ -40,17 +19,10 @@ if (!platform) {
error(`${name} is not supported for this platform: ${platform}`);
}

const bin = path.join(__dirname, "bin");

if (!fs.existsSync(bin)){
fs.mkdirSync(bin);
}

const error = msg => {
console.error(msg);
process.exit(1);
};

const install = () => {
const tmpdir = os.tmpdir();
const req = request({ uri: tarUrl });
Expand Down
2 changes: 1 addition & 1 deletion npm/package-lock.json

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

6 changes: 4 additions & 2 deletions npm/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "@adikari/safebox",
"version": "1.1.5",
"version": "1.1.6",
"description": "A Fast and Flexible secret manager built with love by adikari in Go",
"main": "index.js",
"bin": "./bin/safebox",
"bin": {
"safebox": "./run.js"
},
"scripts": {
"postinstall": "node install.js install",
"preuninstall": "node install.js uninstall"
Expand Down
17 changes: 17 additions & 0 deletions npm/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { spawnSync } = require('child_process'),
constants = require('./constants');

const binaryPath = `${constants.bin}/${constants.binaryName}`;

const [, , ...args] = process.argv;

const options = { cwd: process.cwd(), stdio: "inherit" };

const result = spawnSync(binaryPath, args, options);

if (result.error) {
console.error(result.error);
process.exit(1);
}

process.exit(result.status);

0 comments on commit 7a9fa21

Please sign in to comment.