Skip to content

Commit

Permalink
Distribute binaries via npm module
Browse files Browse the repository at this point in the history
  • Loading branch information
kailan committed Aug 5, 2024
1 parent e4da985 commit fe9e4f3
Show file tree
Hide file tree
Showing 10 changed files with 829 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/tag_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ jobs:
GOHOSTOS: ${{ env.GOHOSTOS }}
GOHOSTARCH: ${{ env.GOHOSTARCH }}
GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }}
- name: Update npm packages to latest version
working-directory: ./npm/@fastly/cli
run: npm install && npm version "${{ github.ref_name }}" --allow-same-version
- name: Publish npm packages
working-directory: ./npm/@fastly
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: for dir in *; do (echo $dir && cd $dir && npm publish); done
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ pkg/config/config.toml
# Ignore commitlint tool
commitlint.config.js
callvis.svg

# Ignore generated npm packages
npm/@fastly/cli-*/
1 change: 1 addition & 0 deletions npm/@fastly/cli/LICENSE
1 change: 1 addition & 0 deletions npm/@fastly/cli/README.md
20 changes: 20 additions & 0 deletions npm/@fastly/cli/fastly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node
import { execFileSync } from "node:child_process";

import { pkgForCurrentPlatform } from "./package-helpers.js";

const pkg = pkgForCurrentPlatform();

let location;
try {
// Check for the binary package from our "optionalDependencies". This
// package should have been installed alongside this package at install time.
location = (await import(pkg)).default;
} catch (e) {
throw new Error(`The package "${pkg}" could not be found, and is needed by @fastly/cli.
If you are installing @fastly/cli with npm, make sure that you don't specify the
"--no-optional" flag. The "optionalDependencies" package.json feature is used
by @fastly/cli to install the correct binary executable for your current platform.`);
}

execFileSync(location, process.argv.slice(2), { stdio: "inherit" });
17 changes: 17 additions & 0 deletions npm/@fastly/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { pkgForCurrentPlatform } from "./package-helpers.js";

const pkg = pkgForCurrentPlatform();

let location;
try {
// Check for the binary package from our "optionalDependencies". This
// package should have been installed alongside this package at install time.
location = (await import(pkg)).default;
} catch (e) {
throw new Error(`The package "${pkg}" could not be found, and is needed by @fastly/cli.
If you are installing @fastly/cli with npm, make sure that you don't specify the
"--no-optional" flag. The "optionalDependencies" package.json feature is used
by @fastly/cli to install the correct binary executable for your current platform.`);
}

export default location;
22 changes: 22 additions & 0 deletions npm/@fastly/cli/package-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { platform, arch } from "node:process";

const knownPackages = {
"darwin arm64": "@fastly/cli-darwin-arm64",
"darwin x64": "@fastly/cli-darwin-x64",
"linux arm64": "@fastly/cli-linux-arm64",
"linux x64": "@fastly/cli-linux-x64",
"linux x64": "@fastly/cli-linux-386",
"win32 arm64": "@fastly/cli-win32-arm64",
"win32 x64": "@fastly/cli-win32-x64",
"win32 x32": "@fastly/cli-win32-386",
};

export function pkgForCurrentPlatform() {
let platformKey = `${platform} ${arch}`;
if (platformKey in knownPackages) {
return knownPackages[platformKey];
}
throw new Error(
`Unsupported platform: "${platformKey}". "@fastly/cli does not have a precompiled binary for the platform/architecture you are using. You can open an issue on https://github.com/fastly/cli/issues to request for your platform/architecture to be included."`
);
}
Loading

0 comments on commit fe9e4f3

Please sign in to comment.