Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: npm package #162

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .github/workflows/bin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Binaries

on:
release:
types: [created]
push:
branches: ["mara/npx"]

env:
PKG_CONFIG_ALLOW_CROSS: 1

jobs:
build:
name: Build Binaries
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: ubuntu-latest
target: aarch64-unknown-linux-gnu
- os: ubuntu-latest
target: arm-unknown-linux-gnueabihf
- os: macos-latest
target: x86_64-apple-darwin
- os: macos-latest
target: aarch64-apple-darwin
- os: windows-latest
target: x86_64-pc-windows-msvc
- os: windows-latest
target: i686-pc-windows-msvc
steps:
- uses: actions/checkout@v2

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
override: true

- name: Cache
uses: Swatinem/rust-cache@v2

- name: Setup Linux
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
echo "PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig" >> $GITHUB_ENV

- name: Setup macOS
if: runner.os == 'macOS'
run: |
brew install openssl
echo "PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig" >> $GITHUB_ENV
echo "OPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include" >> $GITHUB_ENV
echo "OPENSSL_LIB_DIR=/usr/local/opt/openssl/lib" >> $GITHUB_ENV

- name: Setup Windows
if: runner.os == 'Windows'
run:
echo "skipped"

- name: Build
run: cargo build --release --target ${{ matrix.target }}

- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.target }}
path: target/${{ matrix.target }}/release/*

publish:
name: Publish Binaries
needs: build
runs-on: ubuntu-latest
steps:
- name: Download Artifacts
uses: actions/download-artifact@v2
with:
path: artifacts

- name: Release Binaries
uses: softprops/action-gh-release@v1
with:
files: artifacts/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion .github/workflows/book.yml → .github/workflows/book.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Book
on:
push:
branches: ["main"]
workflow_dispatch:
paths:
- 'docs/**'

permissions:
contents: read
Expand Down
19 changes: 12 additions & 7 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ name: Buffrs CLI
on:
push:
branches: ["main"]
paths:
- 'Cargo.lock'
- 'Cargo.toml'
- 'deny.toml'
- 'src/**'
- 'registry/**'
pull_request:
paths:
- 'Cargo.lock'
- 'Cargo.toml'
- 'deny.toml'
- 'src/**'
- 'registry/**'

env:
MINIMUM_LINE_COVERAGE_PERCENT: 35
Expand Down Expand Up @@ -64,10 +76,3 @@ jobs:
- uses: Swatinem/rust-cache@v2
- run: cargo install typos-cli || true
- run: typos
nix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
- uses: DeterminateSystems/magic-nix-cache-action@main
- run: nix flake check
21 changes: 21 additions & 0 deletions .github/workflows/nix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Nix Flake

on:
push:
branches: ["main"]
paths:
- 'flake.nix'
- 'flake.lock'
pull_request:
paths:
- 'flake.nix'

jobs:
nix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
- uses: DeterminateSystems/magic-nix-cache-action@main
- run: nix flake check
- 'flake.lock'
12 changes: 12 additions & 0 deletions .github/workflows/registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ name: Buffrs Registry
on:
push:
branches: ["main"]
paths:
- 'Cargo.lock'
- 'Cargo.toml'
- 'deny.toml'
- 'src/**'
- 'registry/**'
pull_request:
paths:
- 'Cargo.lock'
- 'Cargo.toml'
- 'deny.toml'
- 'src/**'
- 'registry/**'

env:
MINIMUM_LINE_COVERAGE_PERCENT: 5
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bytes = "1.0"
clap = { version = "4.3", features = ["cargo", "derive"] }
diff-struct = { version = "0.5.3", optional = true }
flate2 = "1"
git2 = { version = "0.18.1", optional = true }
git2 = { version = "0.18.1", features = ["vendored-openssl", "vendored-libgit2"], optional = true }
hex = "0.4.3"
home = "0.5.5"
human-panic = "1"
Expand Down
1 change: 1 addition & 0 deletions pkgs/npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# buffrs
Empty file added pkgs/npm/bin/.gitkeep
Empty file.
50 changes: 50 additions & 0 deletions pkgs/npm/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs');
const https = require('https');
const os = require('os');
const pkg = require('./package.json');

const download = (url, dest) => {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download: ${response.statusCode}`));
return;
}
response.pipe(file);
});

file.on('finish', () => {
file.close(resolve);
});

file.on('error', (err) => {
fs.unlink(dest, () => reject(err));
});
});
};

const install = async () => {
const arch = os.arch();
const version = pkg.version;

let platform = os.platform();
let extension = "";

if (["win32", "cygwin"].includes(process.platform)) {
os = "windows";
extension = ".exe";
}

const url = `https://github.com/helsing-ai/buffrs/releases/download/v${version}/buffrs-${platform}-${arch}${extension}`;
const file = `./bin/buffrs-${platform}-${arch}${extension}`;

try {
await download(url, file);
console.log('Buffrs downloaded successfully');
} catch (e) {
console.error('Failed to download binary:', e);
}
};

install()
15 changes: 15 additions & 0 deletions pkgs/npm/package-lock.json

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

13 changes: 13 additions & 0 deletions pkgs/npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "buffrs",
"version": "0.0.1",
"description": "buffrs",
"main": "src/main.js",
"scripts": {
"preinstall": "node download.js"
},
"author": "Mara Schulke <[email protected]>",
"license": "MIT",
"dependencies": {},
"devDependencies": {}
}
43 changes: 43 additions & 0 deletions pkgs/npm/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node

import { spawnSync } from "child_process";

/**
* Returns the executable path which is located inside `node_modules`
* The naming convention is app-${os}-${arch}
* If the platform is `win32` or `cygwin`, executable will include a `.exe` extension.
* @see https://nodejs.org/api/os.html#osarch
* @see https://nodejs.org/api/os.html#osplatform
* @example "x/xx/node_modules/app-darwin-arm64"
*/
function getExePath() {
const arch = process.arch;
const os = process.platform as string;

let extension = "";

if (["win32", "cygwin"].includes(process.platform)) {
os = "windows";
extension = ".exe";
}

try {
// Since the binary will be located inside `node_modules`, we can simply call `require.resolve`
return require.resolve(`buffrs/bin/buffrs-${os}-${arch}${extension}`);
} catch (e) {
throw new Error(
`Couldn't find buffrs binary inside node_modules for ${os}-${arch}`
);
}
}

/**
* Runs the application with args using nodejs spawn
*/
function run() {
const args = process.argv.slice(2);
const processResult = spawnSync(getExePath(), args, { stdio: "inherit" });
process.exit(processResult.status ?? 0);
}

run();
Loading