Repository navigation
Feat/npm distribution #871
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
94ee78e
cf8b02e
713f385
474d683
79e1580
9b7573a
611cee3
d3110df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Publish the npm package for a release tag. Called by the publish-npm job of | ||
| # .github/workflows/release-on-tag.yml. | ||
| # | ||
| # Checks that the tag matches package.json, then publishes. Pre-releases | ||
| # (e.g. 2.7.0-rc.1) go to the `next` dist-tag so `npm install` keeps getting | ||
| # the stable version. | ||
| # | ||
| # Reads from the environment: RELEASE_TAG, NODE_AUTH_TOKEN (until trusted | ||
| # publishing is configured), DRY_RUN (set to 1 to run `npm publish --dry-run`). | ||
| # | ||
| # Run locally: RELEASE_TAG=2.6.0 DRY_RUN=1 .github/scripts/publish-npm.sh | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| cd "$(dirname "$0")/../.." | ||
|
|
||
| package_version=$(jq -r '.version' package.json) | ||
| if [ "$RELEASE_TAG" != "$package_version" ]; then | ||
| echo "Tag $RELEASE_TAG does not match package.json version $package_version" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [[ "$RELEASE_TAG" == *-* ]]; then | ||
| dist_tag=next | ||
| else | ||
| dist_tag=latest | ||
| fi | ||
|
|
||
| publish_flags=(--tag "$dist_tag") | ||
| # provenance can only be generated inside GitHub Actions | ||
| if [ -n "${GITHUB_ACTIONS:-}" ]; then | ||
| publish_flags+=(--provenance) | ||
| fi | ||
| if [ "${DRY_RUN:-}" = 1 ]; then | ||
| publish_flags+=(--dry-run) | ||
| fi | ||
|
|
||
| npm ci | ||
| npm publish "${publish_flags[@]}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,3 +58,28 @@ jobs: | |
| git commit -a -m "Set new development version: $NEW_VERSION" | ||
| git push --set-upstream origin bump-version-to-$NEW_VERSION | ||
| git gh pr create --title "Bump Version to $NEW_VERSION" --body "Bump Version to $NEW_VERSION" --label "bump-version-pr" --head "bump-version-to-$NEW_VERSION" --base main | ||
|
|
||
| publish-npm: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| id-token: write # lets npm verify this workflow (trusted publishing + provenance) | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 22 | ||
| registry-url: https://registry.npmjs.org | ||
|
|
||
| - name: Update npm (trusted publishing needs 11.5.1 or newer) | ||
| run: npm install --global npm@11 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be checked if we can set npm version in actions/setup-node@v4 |
||
|
|
||
| - name: Publish to npm | ||
| env: | ||
| RELEASE_TAG: ${{ github.ref_name }} | ||
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # only until trusted publishing is configured | ||
| run: .github/scripts/publish-npm.sh | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ node_modules | |
| samples/invalid-fablo-config.json | ||
| .DS_Store | ||
| dist | ||
| version.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| // Entry point of the npm package: runs fablo.sh with generator commands | ||
| // executed by the local Node.js instead of the Fablo Docker image. | ||
| import { spawnSync } from "node:child_process"; | ||
| import { dirname, join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const packageDir = join(dirname(fileURLToPath(import.meta.url)), ".."); | ||
|
|
||
| const result = spawnSync("bash", [join(packageDir, "fablo.sh"), ...process.argv.slice(2)], { | ||
| stdio: "inherit", | ||
| env: { ...process.env, FABLO_PACKAGE_DIR: packageDir }, | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't want we should map |
||
|
|
||
| if (result.error) { | ||
| console.error(`Error: cannot run fablo.sh with bash: ${result.error.message}`); | ||
| process.exit(1); | ||
| } | ||
| process.exit(result.status ?? 1); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,16 +260,68 @@ executeOnFabloDocker() { | |
| 2>&1 | ||
| } | ||
|
|
||
| # Set by the npm package (bin/fablo.mjs): generator commands run on the local | ||
| # Node.js from the package directory, mirroring the Fablo Docker image layout. | ||
| executeOnLocalFablo() { | ||
| local command_with_params="$1" | ||
| local fablo_workspace="${2:-$FABLO_TEMP_DIR}" | ||
| local fablo_config="$3" | ||
|
|
||
| if ! command -v node >/dev/null 2>&1; then | ||
| echo "Error: Node.js is required to run Fablo installed from npm" | ||
| exit 1 | ||
| fi | ||
|
|
||
| fablo_workspace="$(cd "$fablo_workspace" && pwd)" | ||
|
|
||
| local fablo_config_env=() | ||
| if [ -n "$fablo_config" ]; then | ||
| if [ ! -f "$fablo_config" ]; then | ||
| echo "File $fablo_config does not exist" | ||
| exit 1 | ||
| fi | ||
|
|
||
| fablo_config="$(cd "$(dirname "$fablo_config")" && pwd)/$(basename "$fablo_config")" | ||
| fablo_config_env=( | ||
| "FABLO_CONFIG=$fablo_config" | ||
| "CHAINCODES_BASE_DIR=$(dirname "$fablo_config")" | ||
| ) | ||
| # The image sees the config as fablo-config.json in the workspace; keep that | ||
| # name, but outside the workspace so no file lands in the user's directories. | ||
| mkdir -p "$FABLO_TEMP_DIR/config" | ||
| cp "$fablo_config" "$FABLO_TEMP_DIR/config/fablo-config.json" | ||
| command_with_params="${command_with_params/\/network\/workspace\/fablo-config.json/$FABLO_TEMP_DIR/config/fablo-config.json}" | ||
| if [ "$command_with_params" = "${command_with_params%% *}" ]; then | ||
| command_with_params="$command_with_params $FABLO_TEMP_DIR/config/fablo-config.json" | ||
| fi | ||
| fi | ||
|
|
||
| env "${fablo_config_env[@]}" FABLO_WORKSPACE="$fablo_workspace" \ | ||
| sh -c "\"$FABLO_PACKAGE_DIR/docker-entrypoint.sh\" $command_with_params" \ | ||
| 2>&1 | ||
| } | ||
|
|
||
| executeFabloGenerator() { | ||
| if [ -n "$FABLO_PACKAGE_DIR" ]; then | ||
| executeOnLocalFablo "$@" | ||
| else | ||
| executeOnFabloDocker "$@" | ||
| fi | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems to call local node.js Fablo package, but the whole execution is:
instead node.js |
||
|
|
||
| useVersion() { | ||
| printSplash | ||
| local version="$1" | ||
|
|
||
| if [ -n "$version" ]; then | ||
| if [ -n "$version" ] && [ -n "$FABLO_PACKAGE_DIR" ]; then | ||
| echo "Fablo is installed from npm. To switch versions, run: npm install -g @fablo-io/fablo@$version" | ||
| exit 1 | ||
| elif [ -n "$version" ]; then | ||
| echo "Updating '$0' to version $version..." | ||
| set +e | ||
| curl -Lf https://github.com/hyperledger-labs/fablo/releases/download/"$version"/fablo.sh -o "$0" && chmod +x "$0" | ||
| else | ||
| executeOnFabloDocker "list-versions" | ||
| executeFabloGenerator "list-versions" | ||
| fi | ||
| } | ||
|
|
||
|
|
@@ -279,25 +331,25 @@ initConfig() { | |
| for arg in "$@"; do | ||
| args="$args \"$arg\"" | ||
| done | ||
| executeOnFabloDocker "init$args" | ||
| executeFabloGenerator "init$args" | ||
| cp -R -i "$FABLO_TEMP_DIR/." "$COMMAND_CALL_ROOT/" | ||
| } | ||
|
|
||
| validateConfig() { | ||
| local fablo_config=${1:-$(getDefaultFabloConfig)} | ||
| executeOnFabloDocker "validate" "" "$fablo_config" | ||
| executeFabloGenerator "validate" "" "$fablo_config" | ||
| } | ||
|
|
||
| extendConfig() { | ||
| local fablo_config=${1:-$(getDefaultFabloConfig)} | ||
| executeOnFabloDocker "extend-config" "" "$fablo_config" | ||
| executeFabloGenerator "extend-config" "" "$fablo_config" | ||
| } | ||
|
|
||
| exportNetworkTopology() { | ||
| local fablo_config=${1:-$(getDefaultFabloConfig)} | ||
| local output_file=${2:-network-topology.mmd} | ||
|
|
||
| executeOnFabloDocker "export-network-topology /network/workspace/fablo-config.json $output_file" "$(dirname "$output_file")" "$fablo_config" | ||
| executeFabloGenerator "export-network-topology /network/workspace/fablo-config.json $output_file" "$(dirname "$output_file")" "$fablo_config" | ||
| } | ||
|
|
||
| generateNetworkConfig() { | ||
|
|
@@ -311,7 +363,7 @@ generateNetworkConfig() { | |
| echo " FABLO_NETWORK_ROOT: $fablo_target" | ||
|
|
||
| mkdir -p "$fablo_target" | ||
| executeOnFabloDocker "setup-network" "$fablo_target" "$fablo_config" | ||
| executeFabloGenerator "setup-network" "$fablo_target" "$fablo_config" | ||
| if [ -f "$fablo_target/hooks/post-generate.sh" ]; then | ||
| chmod +x "$fablo_target/hooks/post-generate.sh" || true | ||
| ("$fablo_target/hooks/post-generate.sh") | ||
|
|
@@ -433,7 +485,11 @@ startMcpServer() { | |
| # Stdout carries the MCP protocol, so everything else goes to stderr. The | ||
| # server, the Fablo skill, and the config schema ship in the Fablo image to | ||
| # match this script's version. | ||
| docker run --rm "$FABLO_IMAGE" tar -C /fablo -cf - mcp skills/fablo docs/schema.json | tar -xf - -C "$FABLO_TEMP_DIR" >&2 | ||
| if [ -n "$FABLO_PACKAGE_DIR" ]; then | ||
| tar -C "$FABLO_PACKAGE_DIR" -cf - mcp skills/fablo docs/schema.json | tar -xf - -C "$FABLO_TEMP_DIR" >&2 | ||
| else | ||
| docker run --rm "$FABLO_IMAGE" tar -C /fablo -cf - mcp skills/fablo docs/schema.json | tar -xf - -C "$FABLO_TEMP_DIR" >&2 | ||
| fi | ||
| if [ ! -f "$FABLO_TEMP_DIR/mcp/start.sh" ]; then | ||
| echo "Error: Cannot read the MCP server from $FABLO_IMAGE" >&2 | ||
| exit 1 | ||
|
|
@@ -451,7 +507,7 @@ elif [ "$COMMAND" = "help" ] || [ "$COMMAND" = "--help" ]; then | |
| printHelp | ||
|
|
||
| elif [ "$COMMAND" = "version" ]; then | ||
| executeOnFabloDocker "version $2" | ||
| executeFabloGenerator "version $2" | ||
|
|
||
| elif [ "$COMMAND" = "use" ]; then | ||
| useVersion "$2" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be verified