Skip to content

Commit

Permalink
add get binary path correctly to warpdive npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
gvkhna committed May 29, 2024
1 parent b0932cf commit e4925b9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install asdf & tools
uses: asdf-vm/actions/install@v3
- name: Install deps
Expand Down
14 changes: 14 additions & 0 deletions cli/scripts/create-arch-npm-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ ARCH=$2
FULL_BINARY_PATH=$3
VERSION=$4

# Function to check if an argument is missing
check_arg() {
if [[ -z "$1" ]]; then
echo "Error: Missing argument for $2."
exit 1
fi
}

# Check all arguments
check_arg "$OS" "operating system (OS)"
check_arg "$ARCH" "architecture (ARCH)"
check_arg "$FULL_BINARY_PATH" "full binary path (FULL_BINARY_PATH)"
check_arg "$VERSION" "version (VERSION)"

# Extract the binary name
BINARY=$(basename "$FULL_BINARY_PATH")

Expand Down
15 changes: 14 additions & 1 deletion cli/scripts/create-main-npm-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,38 @@ TARGET_DIR="dist/npm/warpdive"
# Get the current Git tag
VERSION=$(git describe --tags --abbrev=0)

# Semver regex pattern to validate version format
SEMVER_REGEX="^[0-9]+\.[0-9]+\.[0-9]+$"

# Check if the version is valid semver
if [[ $VERSION =~ $SEMVER_REGEX ]]; then
echo "Version $VERSION is valid semver."
else
echo "Error: Version $VERSION is not a valid semver."
exit 1
fi

# Create necessary directories
mkdir -p "${TARGET_DIR}/bin/"

# Copy the JavaScript files
cp "${SOURCE_DIR}/bin/cli" "${TARGET_DIR}/bin"
cp "${SOURCE_DIR}/index.js" "${TARGET_DIR}/"
cp "${SOURCE_DIR}/install.js" "${TARGET_DIR}/"

# Create package.json
cat > "${TARGET_DIR}/package.json" <<EOF
{
"author": "Gaurav Khanna",
"description": "Warpdive is a docker/oci container layer browser. Learn more at warpdive.xyz.",
"keywords": ["warpdive", "cli"],
"keywords": ["docker", "container", "podman"],
"name": "warpdive",
"repository": {
"type": "git",
"url": "git+https://github.com/gvkhna/warpdive.git"
},
"version": "${VERSION}",
"main": "index.js",
"bin": {
"warpdive": "bin/cli"
},
Expand Down
2 changes: 2 additions & 0 deletions cli/warpdive/bin/cli
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

const {getBinaryPath} = require('../index')

require('child_process').execFileSync(getBinaryPath(), process.argv.slice(2), {
stdio: 'inherit'
})
40 changes: 40 additions & 0 deletions cli/warpdive/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Lookup table for all platforms and binary distribution packages
const BINARY_DISTRIBUTION_PACKAGES = {
'darwin-x64': 'warpdive-darwin-amd64',
'darwin-arm64': 'warpdive-darwin-arm64',
'linux-x64': 'warpdive-linux-amd64',
'linux-arm64': 'warpdive-linux-arm64',
'win32-x64': 'warpdive-windows-amd64',
'win32-arm64': 'warpdive-windows-arm64'
}

// Windows binaries end with .exe so we need to special case them.
const binaryName = process.platform === 'win32' ? 'warpdive.exe' : 'warpdive'

function getBinaryPath() {
// Windows binaries end with .exe so we need to special case them.
const binaryName = process.platform === 'win32' ? 'my-binary.exe' : 'my-binary'

// Determine package name for this platform
const platformSpecificPackageName = BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]

try {
// Resolving will fail if the optionalDependency was not installed
return require.resolve(`${platformSpecificPackageName}/bin/${binaryName}`)
} catch (e) {
return require('path').join(__dirname, '..', binaryName)
}
}

function runBinary(...args) {
require('child_process').execFileSync(getBinaryPath(), args, {
stdio: 'inherit'
})
}

module.exports = {
BINARY_DISTRIBUTION_PACKAGES,
binaryName,
getBinaryPath,
runBinary
}
12 changes: 1 addition & 11 deletions cli/warpdive/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ const path = require('path')
const zlib = require('zlib')
const https = require('https')
const packageJson = require('./package.json')
const {BINARY_DISTRIBUTION_PACKAGES, binaryName} = require('./index')

// Lookup table for all platforms and binary distribution packages
const BINARY_DISTRIBUTION_PACKAGES = {
'darwin-x64': 'warpdive-darwin-amd64',
'darwin-arm64': 'warpdive-darwin-arm64',
'linux-x64': 'warpdive-linux-amd64',
'linux-arm64': 'warpdive-linux-arm64',
'win32-x64': 'warpdive-windows-amd64',
'win32-arm64': 'warpdive-windows-arm64'
}

// Adjust the version you want to install. You can also make this dynamic.
// const BINARY_DISTRIBUTION_VERSION = '1.0.0'
const BINARY_DISTRIBUTION_VERSION = packageJson.version

// Windows binaries end with .exe so we need to special case them.
const binaryName = process.platform === 'win32' ? 'warpdive.exe' : 'warpdive'

// Determine package name for this platform
const platformSpecificPackageName = BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]

Expand Down

0 comments on commit e4925b9

Please sign in to comment.