Skip to content

Commit

Permalink
add package-lock.json bumping
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Jan 23, 2021
1 parent fc282a0 commit 0189643
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 31 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ jobs:
uses: actions/checkout@v2
-
name: Build docker
run: docker build . --tag bump-everywhere
run: docker build . --tag bump-everywhere

run-tests:
name: Run tests
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: bump-npm-version
run: bash ./tests/bump-npm-version.test.sh
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ It supports safe re-runs, it means that if you can run it for an already bumped
### Option 4. Use scripts

1. Ensure `bash` (4 or newer), `git`, `curl`, `jq` exists in your environment
- run e.g. `apk add bash git curl jq`
- On Alpine: `apk add bash git curl jq`
- On Windows: `choco install git curl jq` and use Git Bash
2. Clone this repository: `git clone https://github.com/undergroundwires/bump-everywhere`
- or optionally add this repository as git submodule: `git submodule add https://github.com/undergroundwires/bump-everywhere`
3. Call the script: `bash "scripts/bump-everywhere.sh" <parameters>`
Expand Down Expand Up @@ -135,11 +136,7 @@ You can also use following scripts individually (check script files for usage, p
- [ez-consent](https://github.com/undergroundwires/ez-consent#gitops)
- [aws-static-site-with-cd](https://github.com/undergroundwires/aws-static-site-with-cd)

## GitOps

CI/CD is fully automated for this repo using different GIT events & GitHub actions.

[![GitOps flow](./img/gitops.png)](./.github/workflows)
[↑](#bump-everywhere)

## Support

Expand All @@ -152,3 +149,18 @@ CI/CD is fully automated for this repo using different GIT events & GitHub actio
```

[↑](#bump-everywhere)

## Tests

- **Automated tests**: Run `.test.sh` files in [`./tests`](./tests/)
- **Manual tests**: See documentation in [docker-entrypoint](./tests/docker-entrypoint)

[↑](#bump-everywhere)

## GitOps

CI/CD is fully automated for this repo using different GIT events and GitHub actions.

[![GitOps flow](./img/gitops.png)](./.github/workflows)

[↑](#bump-everywhere)
2 changes: 1 addition & 1 deletion img/functions.drawio

Large diffs are not rendered by default.

Binary file modified img/functions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 2 additions & 6 deletions scripts/bump-everywhere.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,11 @@ create_changelog() {
}

update_npm() {
local -r file_name="package.json"
if ! file_exists "$file_name"; then
return 0
fi
echo "Updating npm version"
bash "$SCRIPTS_DIRECTORY/bump-npm-version.sh" \
|| { echo "Could not bump npm version"; exit 1; }
git add "$file_name" \
|| { echo "git add failed for $file_name"; exit 1; }
git add . \
|| { echo "git add failed"; exit 1; }
}

has_uncommited_changes() {
Expand Down
34 changes: 17 additions & 17 deletions scripts/bump-npm-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Increases NPM version to match the tag
# Example usage:
# bash "bump-npm-version.sh" --version "0.1.0"
# bash "bump-npm-version.sh"
# Prerequisites:
# - Ensure your current folder is the repository root
# Dependencies:
Expand All @@ -16,43 +16,43 @@ readonly SCRIPTS_DIRECTORY=$(dirname "$0")
# shellcheck source=scripts/shared/utilities.sh
source "$SCRIPTS_DIRECTORY/shared/utilities.sh"


match_and_replace_version() {
local -r content="$1" new_version="$2"
# shellcheck disable=SC2001
echo "$content" \
| sed 's/"version":[ \t]\{0,\}"[0-9]\{1,\}.[0-9]\{1,\}.[0-9]\{1,\}"/"version": "'"$new_version"'"/'
echo "$content" | jq --arg new "$new_version" '.version = $new'
}

bump_npm_package_version() {
local -r new_version="$1" file_name="$2"
local original
if ! original=$(cat "$file_name"); then
local original_content
if ! original_content=$(cat "$file_name"); then
echo "Could read \"$file_name\""
exit 1
fi
local updated
if ! updated="$(match_and_replace_version "$original" "$new_version")"; then
local updated_content
if ! updated_content="$(match_and_replace_version "$original_content" "$new_version")"; then
echo "Could not match and replace"
exit 1
fi
echo -e "${updated}" > "$file_name" \
echo -e "${updated_content}" > "$file_name" \
|| { echo "Could not update file: $file_name"; exit 1; }
}

main() {
local -r file_name="package.json"
if ! file_exists "$file_name"; then
echo "Skipping.. No $file_name file exists."
exit 0;
fi
local new_version
if ! new_version=$(print_latest_version); then
echo "Could not retrieve the new version. $new_version"
exit 1;
fi
bump_npm_package_version "$new_version" "$file_name"
echo "Updated npm version to $new_version"
local -r -a file_names=('package.json' 'package-lock.json')
for file_name in "${file_names[@]}"
do
if ! file_exists "$file_name"; then
echo "Skipping.. No $file_name file exists."
continue
fi
bump_npm_package_version "$new_version" "$file_name"
echo "Updated npm version in \"$file_name\" to \"$new_version\""
done
}

main
104 changes: 104 additions & 0 deletions tests/bump-npm-version.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env bash

test() {
# arrange
local -r git_dir="$1"
local sut
local -r initial_version='0.1.0'
local -r new_version='0.2.10'
local -r expected_package_content=$(print_package_json "$new_version")
local -r expected_package_lock_content=$(print_package_lock_json "$new_version")
sut=$(get_absolute_sut_path) || { echo '😢 Could not locate sut'; return 1; }
echo "Sut: $sut"
cd "$git_dir" || { echo "Cannot navigate to temp dir: \"$git_dir\""; return 1; }
setup_git_env "$initial_version" "$new_version" || { echo "Could not setup the test environment"; return 1; }

# act
echo "Setup repository in \"$git_dir\""
if ! bash "$sut"; then
echo "Unexpected exit code: $?";
return 1;
fi

# assert
local -r current_dir="$(pwd)"
echo "$current_dir"
local -r package_content="$(cat 'package.json')"
local -r package_lock_content="$(cat 'package-lock.json')"
local -i status=0
are_equal_json 'package.json' "$package_content" "$expected_package_content" || status=1
are_equal_json 'package-lock.json' "$package_lock_content" "$expected_package_lock_content" || status=1
return $status
}

setup_git_env() {
local -r initial_version="$1"
local -r new_version="$2"
echo "Setting up git environment, initial version: \"$initial_version\", new version: \"$new_version\""
git init -b master --quiet || { echo '😢 Could not initialize git repository'; return 1; }
git config --local user.email "test@privacy.sexy" || { echo '😢 Could not set user e-mail'; return 1; }
git config --local user.name "Test User" || { echo '😢 Could not set user name'; return 1; }
print_package_json "$initial_version" > "package.json"
print_package_lock_json "$initial_version" > "package-lock.json"
git add . || { echo '😢 Could not add git changes'; return 1; }
git commit -m "inital commit" --quiet || { echo '😢 Could not do initial commit'; return 1; }
git tag "$initial_version" || { echo '😢 Could tag first version'; return 1; }
git commit -m "next commit" --allow-empty --quiet || { echo '😢 Could not do next commit'; return 1; }
git tag "$new_version" || { echo '😢 Could tag next version'; return 1; }
return 0
}

main() {
# begin
local -r temp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'bumpeverywheretmpdir')
echo "Test dir: \"$temp_dir\""

# test
test "$temp_dir"
local -i -r test_exit_code="$?"
echo "Test exit code: $test_exit_code"

# cleanup
rm -rf "$temp_dir"
echo "Cleaned \"$temp_dir\""
exit "$test_exit_code"
}

get_absolute_sut_path()
{
local -r current_dir="$(pwd)"
local -r relative_script_dir=$(dirname "$0")
local -r absolute_script_dir="$current_dir/$relative_script_dir"
local -r script_dir="$absolute_script_dir/../scripts"
local normalized
if ! normalized=$(cd "${script_dir}" || return 1;pwd; return 0); then
echo "Dir does not exist: ${script_dir}"
return 1
fi
local -r script_path="$normalized/bump-npm-version.sh"
echo "$script_path"
}

print_package_json() {
local -r version="$1"
echo "{ \"version\": \"$version\" }"
}

print_package_lock_json() {
local -r version="$1"
echo "{ \"version\": \"$version\", \"dependencies\": { \"ez-consent\": { \"version\": \"1.2.1\" } } }"
}

are_equal_json() {
local -r name="$1"
local -r actual="$(echo "$2" | jq -cS)"
local -r expected="$(echo "$3" | jq -cS)"
if [ "$actual" == "$expected" ]; then
return 0
else
echo "Unexpected $name. Actual: \"$actual\". Expected: \"$expected\""
return 1
fi
}

main

0 comments on commit 0189643

Please sign in to comment.