Skip to content

Commit c13ecba

Browse files
version bumper
1 parent 706e37e commit c13ecba

4 files changed

Lines changed: 108 additions & 3 deletions

File tree

.pre-commit-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.2.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
- repo: local
12+
hooks:
13+
- id: check-version-bump
14+
name: Check version bump in package.json
15+
entry: .pre-commit-hooks/check-version-bump.sh
16+
language: script
17+
stages: [commit, push]
18+
always_run: true
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
# Get current branch name
4+
CURRENT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD)
5+
6+
# Only check version bump on main branch
7+
if [ "$CURRENT_BRANCH" != "main" ]; then
8+
echo "ℹ️ Skipping version check (not on main branch)"
9+
exit 0
10+
fi
11+
12+
# Function to bump patch version
13+
bump_patch_version() {
14+
local current_version=$1
15+
local major=$(echo "$current_version" | cut -d. -f1)
16+
local minor=$(echo "$current_version" | cut -d. -f2)
17+
local patch=$(echo "$current_version" | cut -d. -f3)
18+
local new_patch=$((patch + 1))
19+
echo "$major.$minor.$new_patch"
20+
}
21+
22+
# Determine if this is a pre-push or pre-commit hook
23+
# Check PRE_COMMIT_HOOK_TYPE environment variable set by pre-commit
24+
if [ "$PRE_COMMIT_FROM_REF" != "" ] || [ "$PRE_COMMIT_TO_REF" != "" ]; then
25+
# This is a pre-push hook - check if ANY files are being pushed
26+
REMOTE_REF="origin/main"
27+
28+
# Check if there are any commits being pushed (any file changes)
29+
if ! git diff --quiet HEAD "$REMOTE_REF" 2>/dev/null; then
30+
# Get current version (what we're pushing)
31+
CURRENT_VERSION=$(git show HEAD:package.json | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/')
32+
33+
# Get remote version (what's on origin)
34+
REMOTE_VERSION=$(git show "$REMOTE_REF":package.json 2>/dev/null | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/' || echo "0.0.0")
35+
36+
# Extract patch versions
37+
CURRENT_PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3)
38+
REMOTE_PATCH=$(echo "$REMOTE_VERSION" | cut -d. -f3)
39+
40+
if [ "$CURRENT_PATCH" -le "$REMOTE_PATCH" ]; then
41+
echo "❌ Error: patch version must be bumped before pushing to main"
42+
echo " Remote version: $REMOTE_VERSION"
43+
echo " Local version: $CURRENT_VERSION"
44+
echo ""
45+
echo " Cannot auto-bump during push. Please run:"
46+
echo " git reset --soft HEAD~1"
47+
echo " # Edit package.json to bump version"
48+
echo " git add package.json"
49+
echo " git commit"
50+
exit 1
51+
else
52+
echo "✓ Patch version bumped: $REMOTE_VERSION$CURRENT_VERSION"
53+
fi
54+
fi
55+
else
56+
# This is a pre-commit hook - check if ANY files are staged
57+
if ! git diff --cached --quiet; then
58+
# Get the version from package.json in working tree
59+
CURRENT_VERSION=$(cat package.json | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/')
60+
61+
# Get the version from HEAD (last commit)
62+
HEAD_VERSION=$(git show HEAD:package.json 2>/dev/null | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/' || echo "0.0.0")
63+
64+
# Extract patch version (third number)
65+
CURRENT_PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3)
66+
HEAD_PATCH=$(echo "$HEAD_VERSION" | cut -d. -f3)
67+
68+
# Check if patch version was bumped
69+
if [ "$CURRENT_PATCH" -le "$HEAD_PATCH" ]; then
70+
# Auto-bump the patch version
71+
NEW_VERSION=$(bump_patch_version "$HEAD_VERSION")
72+
73+
echo "🔧 Auto-bumping patch version: $HEAD_VERSION$NEW_VERSION"
74+
75+
# Update package.json
76+
sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" package.json
77+
rm -f package.json.bak
78+
79+
# Stage the updated package.json
80+
git add package.json
81+
82+
echo "✓ Version bumped and staged"
83+
else
84+
echo "✓ Patch version already bumped: $HEAD_VERSION$CURRENT_VERSION"
85+
fi
86+
fi
87+
fi
88+
89+
exit 0

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Run `yarn test` to run the tests.
1818

1919
Once your plugin is ready to test within Amplenote, you can build and test it within Amplenote by following these steps:
2020

21-
0. Create a note for your plugin, if you haven't already. It needs to have at least a) settings table and b) code block.
22-
More details on a valid plugin note are in the [Guide to Building Plugins](https://www.amplenote.com/help/guide_to_developing_amplenote_plugins)
2321
1. [Install the Github Developers Plugin](https://www.amplenote.com/plugins/FZf22PXCKTRTB1tJwta1Nepq).
2422
2. Compile your plugin using `npm run build` or `node esbuild.js` from the root folder for your project
2523
3. Commit the resulting file (default location: `build/compiled.js`) to your git repo (e.g., `git add build/compiled.js && git commit -m "Compiled plugin"`)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
},
2121
"testEnvironment": "jsdom",
2222
"type": "module",
23-
"version": "0.1.1"
23+
"version": "0.1.3"
2424
}

0 commit comments

Comments
 (0)