-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-version.sh
More file actions
137 lines (124 loc) · 3.81 KB
/
create-version.sh
File metadata and controls
137 lines (124 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
set -e
help()
{
echo
echo "Creates new version of a package to be published on NPM."
echo "Before pushing changes to remote the following NPM scripts - if present - are executed:"
echo "build, lint, check-types, test."
echo
echo "Syntax:"
echo " create-version.sh <version-type> [<package-name>]"
echo
echo "Environment variables:"
echo " MAIN_BRANCH - (optional) Repository main branch (default: master)"
echo
echo "Arguments:"
echo " version-type - version type supported by NPM version script"
echo " examples: patch, minor, major, prepatch, preminor, premajor, prerelease,"
echo " see https://docs.npmjs.com/cli/v8/commands/npm-version for details"
echo " package-name - package directory name, relevant only for cloudify-ui-common repository,"
echo " examples: backend, configs, cypress, frontend, scripts"
false
}
log() {
LOG_PREFIX="create-version.sh:"
echo -e $LOG_PREFIX $@
}
error() {
log "ERROR: " $@
}
VERSION_TYPE=$1
PACKAGE_NAME=$2
if [ -z "$VERSION_TYPE" ]; then
error "No version type provided."
help
fi
if [ -n "$PACKAGE_NAME" ]; then
IS_WORKSPACE_PACKAGE=1
PACKAGE_DIRECTORY="packages/$PACKAGE_NAME"
cd $PACKAGE_DIRECTORY || {
error "Invalid package name provided. Directory '$PACKAGE_DIRECTORY' does not exist."
false
}
fi
NPM_PACKAGE_NAME=`cut -d "=" -f 2 <<< $(npm run env | grep "npm_package_name")`
MAIN_BRANCH=${MAIN_BRANCH:-master}
log "Switching to main branch (${MAIN_BRANCH}) and checking if its up-to-date...";
git checkout ${MAIN_BRANCH} || {
error "Error during checking out ${MAIN_BRANCH} branch"
false
}
git fetch || {
error "Error during fetching data from repository"
false
}
git pull || {
error "Error during pulling ${MAIN_BRANCH} branch"
false
}
git diff origin/${MAIN_BRANCH} --exit-code --compact-summary || {
error "Working directory not clean. Delete or push your changes to ${MAIN_BRANCH}."
false
}
log "Running tests and build..."
npm run lint --if-present || {
error "Running lint check failed"
false
}
npm run check-types --if-present || {
error "Running types check failed"
false
}
npm run test --if-present || {
error "Running tests failed"
false
}
npm run build --if-present || {
error "Running build failed"
false
}
NPM_VERSION_OPTIONS="--no-git-tag-version $VERSION_TYPE"
if [[ $VERSION_TYPE == pre* ]]; then
NPM_VERSION_OPTIONS="$NPM_VERSION_OPTIONS --preid pre"
fi
echo NPM_VERSION_OPTIONS = $NPM_VERSION_OPTIONS
npm version $NPM_VERSION_OPTIONS
NEXT_VERSION=v$(node -p "require('./package.json').version")
log "Creating ${NPM_PACKAGE_NAME} ${NEXT_VERSION}...";
log "Creating version publish branch...";
if [ -n "$IS_WORKSPACE_PACKAGE" ]; then
NEXT_VERSION_BRANCH="publish_${PACKAGE_NAME}_${NEXT_VERSION}"
COMMIT_MESSAGE="Bump ${PACKAGE_NAME} version to ${NEXT_VERSION}"
GIT_TAG="${PACKAGE_NAME}_${NEXT_VERSION}"
else
NEXT_VERSION_BRANCH="publish_${NEXT_VERSION}"
COMMIT_MESSAGE="Bump version to ${NEXT_VERSION}"
GIT_TAG="${NEXT_VERSION}"
fi
git checkout -b ${NEXT_VERSION_BRANCH}
git add package.json
if [ -n "$IS_WORKSPACE_PACKAGE" ]; then
git add ../../package-lock.json
else
git add package-lock.json
fi
git commit -m "${COMMIT_MESSAGE}"
git tag ${GIT_TAG}
log "This is the last step before CI-based npm publish.";
log "Are you sure you want to trigger publish ${NPM_PACKAGE_NAME} ${NEXT_VERSION}?";
select choice in "Yes" "No"; do
case "$choice" in
Yes)
log "Pushing changes...";
git push origin ${NEXT_VERSION_BRANCH} tag ${GIT_TAG}
git checkout ${MAIN_BRANCH}
break;;
*)
log "Reverting changes...";
git checkout ${MAIN_BRANCH}
git tag -d ${GIT_TAG}
git branch -D ${NEXT_VERSION_BRANCH}
break;;
esac
done