diff --git a/.init-template.sh b/.init-template.sh new file mode 100755 index 0000000..81e3ff2 --- /dev/null +++ b/.init-template.sh @@ -0,0 +1,67 @@ +#!/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 github_org="$1" + local -r repository_name="$2" + local -r description="$3" + + eval "$SED_COMMAND 's|cncsc/template-stacks|$github_org/$repository_name|g' package.json" + eval "$SED_COMMAND 's/{{module_description}}/$description/g' package.json" + rm package-lock.json + npm install +} + +function update_github_env_file() { + local -r github_org="$1" + eval "$SED_COMMAND 's/cncsc/$github_org/g ./github/env.yaml" +} + +function update_remote_state_config() { + local tfc_org + echo 'Enter the name of the Terraform Cloud org (e.g. "cncsc-dev"):' + read -r tfc_org + eval "$SED_COMMAND 's/cncsc/$tfc_org/g' .remote-state-config.yaml" +} + +function main() { + local -r repository_name=$(git remote -v | grep push | sed -e 's|.*/||' | sed -e 's/\.git.*//') + local description + + echo "Initializing repository from template..." + echo "Using repository name as the module name ($repository_name)..." + + until test -n "${description:=${SHORT_DESCRIPTION}}"; do + echo "Enter a short description for the repository (package.json):" + echo ' (e.g. "Monorepo for declaratively managing the infrastructure and configuration of .")' + read -r description + done + + remote_origin="$(git config --get remote.origin.url || true)" + github_org="$(dirname "$remote_origin")" + + update_package_json "$github_org" "$repository_name" "$description" + update_github_env_file "$github_org" + update_remote_state_config + + 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 "$@" diff --git a/package.json b/package.json index 527e26c..c74006d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,8 @@ { "name": "@cncsc/template-stacks", "version": "0.0.0-development", + "description": "{{module_description}}", + "main": "terragrunt.hcl", "repository": { "type": "git", "url": "git@github.com:cncsc/template-stacks.git"