Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
41 changes: 41 additions & 0 deletions .github/scripts/publish-npm.sh
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
Comment on lines +32 to +35

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be verified

if [ "${DRY_RUN:-}" = 1 ]; then
publish_flags+=(--dry-run)
fi

npm ci
npm publish "${publish_flags[@]}"
25 changes: 25 additions & 0 deletions .github/workflows/release-on-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ node_modules
samples/invalid-fablo-config.json
.DS_Store
dist
version.json
20 changes: 20 additions & 0 deletions bin/fablo.mjs
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 },
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want fablo.sh because is requires docker. We want to generate network without fablo container (assume fablo container is not available).

we should map fablo.sh commands/features to call node.js fablo sources/lib, without redundant Docker container layer.


if (result.error) {
console.error(`Error: cannot run fablo.sh with bash: ${result.error.message}`);
process.exit(1);
}
process.exit(result.status ?? 1);
16 changes: 12 additions & 4 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

set -e

# The Docker image runs this from /fablo; the npm package runs it from the
# package directory with FABLO_WORKSPACE pointing at the host workspace.
fablo_home="$(cd "$(dirname "$0")" && pwd)"

executeOclifCommand() {
command_with_params=$1
# shellcheck disable=SC2086
(cd "$target_dir" && node --no-warnings /fablo/bin/run.mjs $command_with_params)
(cd "$target_dir" && node --no-warnings "$fablo_home/bin/run.mjs" $command_with_params)
}

formatGeneratedFiles() {
Expand All @@ -17,7 +21,11 @@ formatGeneratedFiles() {
# Since the templates should obey good practices, we don't use linters here
# (i.e. shellcheck and yamllint).
echo "Formatting generated files"
shfmt -i=2 -l -w "$target_dir" >/dev/null
if command -v shfmt >/dev/null 2>&1; then
shfmt -i=2 -l -w "$target_dir" >/dev/null
else
echo "Warning: shfmt not found, generated scripts are left unformatted"
fi

for yaml in "$target_dir"/**/*.yaml; do

Expand All @@ -27,15 +35,15 @@ formatGeneratedFiles() {
fi

# remove trailing spaces
sed --in-place 's/[ \t]*$//' "$yaml"
sed 's/[[:blank:]]*$//' "$yaml" >"$yaml.tmp" && mv "$yaml.tmp" "$yaml"

# remove duplicated empty/blank lines
content="$(awk -v RS= -v ORS='\n\n' '1' "$yaml")"
echo "$content" >"$yaml"
done
}

target_dir="/network/workspace"
target_dir="${FABLO_WORKSPACE:-/network/workspace}"
oclif_command=${1:-setup-network}

# Map old yeoman command format to oclif format
Expand Down
74 changes: 65 additions & 9 deletions docs/fablo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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:

  1. cal node.js fablo
  2. spawn process with fablo.sh
  3. fablo.sh calls fablo node.js dist

instead node.js fablo should call fablo node.js dist directly


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
}

Expand All @@ -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() {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
30 changes: 20 additions & 10 deletions e2e-network/docker/test-01-v3-simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ FABLO_HOME="$TEST_TMP/../../.."

export FABLO_HOME

# This test runs Fablo from the npm package packed from this checkout, not from
# fablo.sh with the Docker image, to verify that both behave the same way.
FABLO_NPM_PREFIX="$(mktemp -d -t fablo-npm.XXXXXXXX)"
FABLO="$FABLO_NPM_PREFIX/bin/fablo"

installNpmPackage() {
(cd "$FABLO_HOME" && npm ci && npm pack --pack-destination "$FABLO_NPM_PREFIX")
npm install --global --prefix "$FABLO_NPM_PREFIX" "$FABLO_NPM_PREFIX"/fablo-io-fablo-*.tgz
}

networkUp() {
"$FABLO_HOME/fablo-build.sh"
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" init node dev)
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" up)
installNpmPackage
(cd "$TEST_TMP" && "$FABLO" init node dev)
(cd "$TEST_TMP" && "$FABLO" up)
}

dumpLogs() {
Expand All @@ -22,7 +32,7 @@ dumpLogs() {

networkDown() {
(for name in $(docker ps --format '{{.Names}}'); do dumpLogs "$name"; done)
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" down)
(cd "$TEST_TMP" && "$FABLO" down)
}

waitForContainer() {
Expand Down Expand Up @@ -97,19 +107,19 @@ expectQueryWithRetry "peer1.org1.example.com" "my-channel1" "chaincode1" \
'{"error":"SORRY_NOT_FOUND"}'

# Verify channel query scripts
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" channel fetch newest my-channel1 org1 peer1)
(cd "$TEST_TMP" && "$FABLO" channel fetch newest my-channel1 org1 peer1)
expectCommand "cat \"$TEST_TMP/newest.block\"" "KVContract:put"

(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" channel fetch 3 my-channel1 org1 peer1 "another.block")
(cd "$TEST_TMP" && "$FABLO" channel fetch 3 my-channel1 org1 peer1 "another.block")
expectCommand "cat \"$TEST_TMP/another.block\"" "KVContract:put"

(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" channel fetch config my-channel1 org1 peer1 "channel-config.json")
(cd "$TEST_TMP" && "$FABLO" channel fetch config my-channel1 org1 peer1 "channel-config.json")
expectCommand "cat \"$TEST_TMP/channel-config.json\"" "\"mod_policy\": \"Admins\","

expectCommand "(cd \"$TEST_TMP\" && \"$FABLO_HOME/fablo.sh\" channel getinfo my-channel1 org1 peer1)" "\"height\":4"
expectCommand "(cd \"$TEST_TMP\" && \"$FABLO\" channel getinfo my-channel1 org1 peer1)" "\"height\":4"

# Reset and ensure the state is lost after reset
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" reset)
(cd "$TEST_TMP" && "$FABLO" reset)
waitForChaincode "peer0.org1.example.com" "my-channel1" "chaincode1" "0.0.1"
waitForChaincode "peer1.org1.example.com" "my-channel1" "chaincode1" "0.0.1"
expectQueryWithRetry "peer0.org1.example.com" "my-channel1" "chaincode1" \
Expand All @@ -123,7 +133,7 @@ expectInvoke "peer0.org1.example.com" "my-channel1" "chaincode1" \

# Test export-network-topology to Mermaid
cp -f "$FABLO_HOME/samples/fablo-config-hlf2-1org-1chaincode.json" "$TEST_TMP/simple-config.json"
(cd "$TEST_TMP" && "$FABLO_HOME/fablo.sh" export-network-topology simple-config.json network-topology.mmd)
(cd "$TEST_TMP" && "$FABLO" export-network-topology simple-config.json network-topology.mmd)
expectCommand "cat \"$TEST_TMP/network-topology.mmd\"" "graph LR"


Expand Down
Loading