go-npm

repository·master·Indexed 41 days ago

https://github.com/sanathkr/go-npm

A utility for distributing and installing cross-platform Go binaries via the NPM registry. It automates the downloading and installation of platform-specific binaries during the NPM installation lifecycle using postinstall and preuninstall scripts, leveraging NPM's path management and update lifecycle.

Tokens
1.6K
Snippets
5
Records
7
Agent score
2%

What's inside go-npm

  1. Distribute Go binaries via NPM using go-npm

    master

    The go-npm package allows you to distribute cross-platform Go binaries through the NPM registry. This approach leverages NPM's existing cross-platform installer, automatic $PATH management, and simple update lifecycle (npm install -g and npm update -g).

    To use this, you must host your compiled Go binaries on a web server (like GitHub Releases, S3, or Dropbox) and configure an NPM package to download and install them during the postinstall lifecycle hook.

  2. Setup steps to publish a Go app to NPM

    master

    Follow these steps to prepare your Go project for NPM distribution:

    1. Publish Binaries: Use a tool like GoReleaser to compile and upload your binaries to a server (e.g., GitHub Releases).
    2. Initialize NPM: In your project directory (or a separate Git repo), run npm init to create a package.json.
    3. Install go-npm: Add go-npm as a development dependency:
      npm install go-npm --save
    4. Cleanup: You can delete the node_modules folder after installation, as it is only needed during the publishing process.
    5. Configure: Add the postinstall, preuninstall, and goBinary settings to package.json as described in the configuration guide.
    6. Publish: Run the following command to make your app available on NPM:
      npm publish
    npm init
    npm install go-npm --save
    rm -r node_modules
    npm publish
  3. Configure go-npm in package.json

    master

    To integrate go-npm into your application, you must configure your package.json with specific lifecycle scripts and a goBinary configuration object. This tells NPM to trigger the binary download and installation process automatically.

    1. Add Lifecycle Scripts

    Add postinstall and preuninstall scripts to your package.json to ensure binaries are downloaded upon installation and cleaned up upon uninstallation:

    {
        "postinstall": "go-npm install",
        "preuninstall": "go-npm uninstall"
    }

    2. Define the goBinary Configuration

    The goBinary object specifies how to locate and install your binaries. Add this to your package.json:

    "goBinary": {
          "name": "command-name",
          "path": "./bin",
          "url": "https://github.com/user/my-go-package/releases/download/v{{version}}/myGoPackage_{{version}}_{{platform}}_{{arch}}.tar.gz"
    }

    Configuration Keys

    • name: The command name users will type in their terminal to run your binary.
    • path: The temporary local directory where binaries are downloaded before being moved to NPM's bin directory.
    • url: The template URL for your hosted binaries. Use the following placeholders:
      • {{version}}: The version number from your package.json.
      • {{platform}}: The target OS (e.g., windows, darwin, linux).
      • {{arch}}: The target architecture (e.g., amd64, arm64).
    {
        "postinstall": "go-npm install",
        "preuninstall": "go-npm uninstall",
        "goBinary": {
          "name": "command-name",
          "path": "./bin",
          "url": "https://github.com/user/my-go-package/releases/download/v{{version}}/myGoPackage_{{version}}_{{platform}}_{{arch}}.tar.gz"
        }
    }
  4. Install and update the distributed Go app

    master

    Once your package is published to NPM, end-users can interact with your Go application using standard NPM commands:

    To install globally:

    npm install -g your-app-name

    To update to the latest version:

    npm update -g your-app-name
    npm install -g your-app-name
    npm update -g your-app-name
  5. Configure `package.json` for `go-npm` binary distribution

    master

    To use go-npm to distribute your Go binaries via NPM, you must add a goBinary object to your package.json. The script uses these values to construct the download URL and identify the binary.

    Required fields in goBinary:

    • name: The name of the binary file (the script automatically appends .exe on Windows).
    • path: The local directory where the binary should be temporarily extracted (e.g., ./bin).
    • url: The download URL template. You must use placeholders to support different architectures and platforms.

    Required top-level field:

    • version: The version of your package (the script strips a leading 'v' if present).

    Supported URL placeholders:

    • {{arch}}: Maps Node's process.arch to Go's $GOARCH (e.g., amd64, arm, 386).
    • {{platform}}: Maps Node's process.platform to Go's platform names (e.g., darwin, linux, windows, freebsd).
    • {{version}}: The version string from package.json (without a leading 'v').
    • {{bin_name}}: The value of goBinary.name (with .exe appended on Windows).
    {
      "version": "1.0.0",
      "goBinary": {
        "name": "my-tool",
        "path": "./bin",
        "url": "https://example.com/downloads/my-tool-{{version}}-{{platform}}-{{arch}}.tar.gz"
      }
    }
  6. Supported Architectures and Platforms

    master

    The go-npm tool supports specific mappings between Node.js runtime properties and Go build targets. If your current environment does not match one of these mappings, installation will fail.

    Architecture Mapping ({{arch}}):

    Node.js process.archGo $GOARCH
    ia32386
    x64amd64
    armarm

    Platform Mapping ({{platform}}):

    Node.js process.platformGo Platform
    darwindarwin
    linuxlinux
    win32windows
    freebsdfreebsd
  7. Use the `go-npm` CLI to install or uninstall binaries

    master

    The go-npm script is a CLI tool that automates the downloading, extracting, and moving of Go binaries into your system's NPM bin directory. It must be run from the root of the package containing the configured package.json.

    Commands:

    • install: Downloads the binary from the configured URL, extracts it to the specified goBinary.path, and then moves it to the system's NPM binary directory (determined via npm bin or npm_config_prefix).
    • uninstall: Removes the binary from the system's NPM binary directory.
    # To install the binary
    node src/index.js install
    
    # To uninstall the binary
    node src/index.js uninstall