-
Notifications
You must be signed in to change notification settings - Fork 1
/
.init-template.sh
executable file
·62 lines (49 loc) · 2.03 KB
/
.init-template.sh
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
#!/usr/bin/env bash
# This script initializes a repository deployed from this template.
# Skip prompts by setting environment variables SHORT_DESCRIPTION and/or LONG_DESCRIPTION
# If LONG_DESCRIPTION is not set then it will default to the value in SHORT_DESCRIPTION
set -e;
SED_COMMAND="sed -i"
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS doesn't use GNU sed and has a slightly different syntax
SED_COMMAND="sed -i '' -E"
fi
function update_package_json() {
local -r repository_name="$1"
local description
until test -n "${description:=${SHORT_DESCRIPTION}}"; do
echo "Enter a short description for the repository (package.json):"
echo ' (e.g. "Contains modules for deploying and managing <type> resources.")'
read -r description;
done
eval "$SED_COMMAND 's/template-terraform-module/$repository_name/g' package.json"
eval "$SED_COMMAND 's/{{module_description}}/$description/g' package.json"
rm package-lock.json
npm install
}
function update_readme() {
local -r repository_name="$1"
local description
description="${LONG_DESCRIPTION:-${SHORT_DESCRIPTION}}"
until test -n "${description:=${LONG_DESCRIPTION:-${SHORT_DESCRIPTION}}}"; do
echo "Enter a long description for the repository (README.md):"
echo ' (e.g. "This repository contains modules for deploying and managing <type> resources. <context>.")'
read -r description
done
eval "$SED_COMMAND 's/template-terraform-module/$repository_name/g' README.md"
eval "$SED_COMMAND 's/{{module_description}}/$description/g' README.md"
}
function main() {
local -r repository_name=$(git remote -v | grep push | sed -e 's|.*/||' | sed -e 's/\.git.*//')
echo "Initializing repository from template..."
echo "Using repository name as the module name ($repository_name)..."
update_package_json "$repository_name"
update_readme "$repository_name"
rm -rf .init-template.sh
echo ""
echo "Initialization complete. Committing to source control..."
git add -A
git commit -m "Initialize repository from template"
git push -u origin main
}
main "$@"