Skip to content
Open
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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ By default the action will look for a `.node-version` file in the root level
directory. If present it will setup the node environment to use that version of
node, installing the specified version if necessary.

This will also attempt to install the `yarn` command and make it available to
future steps in the workflow. If a yarn version is not provided, right now we
only support installing yarn classic (i.e. v1.x) by default.
This will also detect and install the appropriate package manager based on lock
files present in the repository:

- **npm**: Detected via `package-lock.json`
- **pnpm**: Detected via `pnpm-lock.yaml`
- **yarn**: Detected via `yarn.lock`
- **bun**: Detected via `bun.lock`

If a specific version is provided via inputs, that version will be installed.
Otherwise, the package manager will be installed with a sensible default version
when its corresponding lock file is detected.

The Node.js tools are made available via
[nodenv](https://github.com/nodenv/nodenv).
Expand Down
91 changes: 75 additions & 16 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ inputs:
node:
description: "Node.js version (e.g. 18)"
required: false
npm:
description: "NPM version (e.g. 11.7.0)"
required: false
pnpm:
description: "PNPM version (e.g. 9.9.0)"
required: false
yarn:
description: "Yarn version (e.g. 1.22.22)"
required: false
bun:
description: "Bun version (e.g. 1.3.5)"
required: false
go:
description: "Go version (e.g. 1.22)"
required: false
Expand Down Expand Up @@ -74,35 +83,85 @@ runs:
with:
node-version: ${{ steps.detect-node.outputs.VERSION }}

# Detects if yarn is being used as package manager
# This step enables conditional execution of the setup action,
# either when a version is explicitly provided or when a `yarn.lock` file exists.
# Right now, we only support installing yarn v1.x if the version is not explicitly provided.
- id: detect-yarn
# Detect which package manager to use based on lock files or explicit inputs.
# Priority order: npm > pnpm > yarn > bun > default to yarn v1
- id: detect-package-manager
shell: bash
env:
NPM_INPUT: ${{ inputs.npm }}
PNPM_INPUT: ${{ inputs.pnpm }}
YARN_INPUT: ${{ inputs.yarn }}
BUN_INPUT: ${{ inputs.bun }}
run: |
if [ -n "$YARN_INPUT" ]; then
# Check for explicit version inputs first
if [ -n "$NPM_INPUT" ]; then
echo "PACKAGE_MANAGER=npm" >> $GITHUB_OUTPUT
echo "VERSION=$NPM_INPUT" >> $GITHUB_OUTPUT
elif [ -n "$PNPM_INPUT" ]; then
echo "PACKAGE_MANAGER=pnpm" >> $GITHUB_OUTPUT
echo "VERSION=$PNPM_INPUT" >> $GITHUB_OUTPUT
elif [ -n "$YARN_INPUT" ]; then
echo "PACKAGE_MANAGER=yarn" >> $GITHUB_OUTPUT
echo "VERSION=$YARN_INPUT" >> $GITHUB_OUTPUT
elif [ -n "$BUN_INPUT" ]; then
echo "PACKAGE_MANAGER=bun" >> $GITHUB_OUTPUT
echo "VERSION=$BUN_INPUT" >> $GITHUB_OUTPUT
# Check for lock files
elif [ -f package-lock.json ]; then
echo "PACKAGE_MANAGER=npm" >> $GITHUB_OUTPUT
echo "VERSION=" >> $GITHUB_OUTPUT
elif [ -f pnpm-lock.yaml ]; then
echo "PACKAGE_MANAGER=pnpm" >> $GITHUB_OUTPUT
echo "VERSION=" >> $GITHUB_OUTPUT
elif [ -f yarn.lock ]; then
echo "LOCK_FILE=yarn.lock" >> $GITHUB_OUTPUT
echo "PACKAGE_MANAGER=yarn" >> $GITHUB_OUTPUT
echo "VERSION=" >> $GITHUB_OUTPUT
elif [ -f bun.lock ] || [ -f bun.lockb ]; then
echo "PACKAGE_MANAGER=bun" >> $GITHUB_OUTPUT
echo "VERSION=" >> $GITHUB_OUTPUT
# Default to yarn v1 if no package manager is detected
else
echo "PACKAGE_MANAGER=yarn" >> $GITHUB_OUTPUT
echo "VERSION=" >> $GITHUB_OUTPUT
echo "LOCK_FILE=" >> $GITHUB_OUTPUT
fi

- name: Setup Yarn
- name: Setup Package Manager
shell: bash
env:
YARN_VERSION: ${{ steps.detect-yarn.outputs.VERSION }}
YARN_LOCK_FILE: ${{ steps.detect-yarn.outputs.LOCK_FILE }}
PACKAGE_MANAGER:
${{ steps.detect-package-manager.outputs.PACKAGE_MANAGER }}
VERSION: ${{ steps.detect-package-manager.outputs.VERSION }}
run: |
if [ -n "$YARN_VERSION" ]; then
npm install -g yarn@$YARN_VERSION
elif [ -n "$YARN_LOCK_FILE" ]; then
npm install -g yarn
fi
case "$PACKAGE_MANAGER" in
npm)
if [ -n "$VERSION" ]; then
npm install -g npm@$VERSION
else
npm install -g npm@latest
fi
;;
pnpm)
if [ -n "$VERSION" ]; then
npm install -g pnpm@$VERSION
else
npm install -g pnpm
fi
;;
yarn)
if [ -n "$VERSION" ]; then
npm install -g yarn@$VERSION
else
npm install -g yarn
fi
;;
bun)
if [ -n "$VERSION" ]; then
npm install -g bun@$VERSION
else
npm install -g bun
fi
;;
esac

# Detect version for Go from input or common version files.
# This step enables conditional execution of the setup action,
Expand Down
Loading