forked from braze-inc/braze-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdocs
More file actions
executable file
·242 lines (213 loc) · 6.84 KB
/
bdocs
File metadata and controls
executable file
·242 lines (213 loc) · 6.84 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
# This is a wrapper script for interacting with the files in './scripts/'.
#
# Usage: ./bdocs [option]
set -e
# The project's root directory, primary branch, and redirect file.
export PROJECT_ROOT="$(dirname "$(realpath "$0")")"
export PRIMARY_BRANCH="main"
export REDIRECT_FILE="$PROJECT_ROOT/assets/js/broken_redirect_list.js"
export REDIRECT_MATCHES="$PROJECT_ROOT/scripts/temp/redirect_matches.json"
# DEPRECATED: both repositories now use 'main' as the base branch.
# Set the primary branch depending which Braze Docs repository.
#if [[ "$PROJECT_ROOT" == *"braze-docs-hidden"* ]]; then
# export PRIMARY_BRANCH="main"
#else
# export PRIMARY_BRANCH="master"
#fi
# All scripts exported so they can source bdocs and call each other if needed.
export DEPLOY="$PROJECT_ROOT/scripts/create_deploy_text.sh"
export RELEASE="$PROJECT_ROOT/scripts/create_release_text.sh"
export TLINKS="$PROJECT_ROOT/scripts/transform_reference_links.py"
export RLINKS="$PROJECT_ROOT/scripts/remove_unused_reference_links.rb"
export ULINKS="$PROJECT_ROOT/scripts/update_old_links.py"
export MREDIRECTS="$PROJECT_ROOT/scripts/make_redirects.py"
export LREDIRECTS="$PROJECT_ROOT/scripts/list_new_redirect_urls.sh"
export CLANGS="$PROJECT_ROOT/scripts/clean_orphaned_translations.py"
export RAKE="$PROJECT_ROOT/scripts/print_rake_params.sh"
export SYNTAX="$PROJECT_ROOT/scripts/print_unique_syntax.sh"
# Utility scripts that are not directly used in bdocs:
export MRD="$PROJECT_ROOT/scripts/utils/merge_redirect_descendants.py"
export TEMP_DIR="$PROJECT_ROOT/scripts/temp"
# Displays usage for bdocs
display_help() {
cat << EOF
bdocs is a CLI tool for executing Braze Docs scripts.
USAGE:
./bdocs [option]
OPTIONS:
deploy Create the deploy body text for weekly deployments
release Create the release body text for monthly releases
tlinks Transform reference links to inline links on 1 or more pages
rlinks Remove unused reference links on 1 or more pages
ulinks Update old links using newest redirect on 1 or more pages
mredirects Make redirects for all renamed files in this branch
lredirects Test new redirects by listing old URLs in this branch
fblinks Finds broken links throughout the docs site
clangs Clean language directories by deleting orphaned files
rake Print all supported parameters for 'rake'
syntax Print all unique Markdown syntax supported by Braze Docs
help Display this help message and exit
EOF
}
# If no './scripts/temp' directory, create one.
if [ ! -d "$TEMP_DIR" ]; then
mkdir "$TEMP_DIR"
fi
# If a file or directory is required, pass or fail.
require_path_or_file() {
if [[ -z "$1" ]]; then
echo "Error: A file or directory path is required."
exit 1
fi
}
# If new merges into 'develop' are required, pass or fail.
require_new_merges() {
LATEST_COMMIT_HASH=$(git log --max-count=1 --format="%H" origin/$PRIMARY_BRANCH ^origin/develop)
COMMIT_LOGS=$(git log --first-parent "$LATEST_COMMIT_HASH"..origin/develop)
if [ -z "$COMMIT_LOGS" ]; then
echo "Error: No new merges into 'develop' since the last deployment."
exit 1
fi
}
# If a the 'jq' package is required, pass or fail. This is needed to parse JSON.
# For more info, see: https://jqlang.org/
require_jq() {
if ! command -v jq &> /dev/null; then
echo "Error: 'jq' is required to run this command. On MacOS, run:"
echo " brew install jq"
exit 1
fi
}
# If yarn-related project dependencies are required, pass or fail.
require_yarn() {
if [ ! -d "$PROJECT_ROOT/node_modules" ]; then
echo "Error: 'node_modules' are required to run this command. On MacOS, run:"
echo " brew install yarn"
echo " yarn install"
exit 1
fi
}
# Stop commands that are unsupported in the hidden repository.
# Add more commands to this list as needed.
readonly UNSUPPORTED_HIDDEN_COMMANDS="release,ulinks,mredirects,fblinks,lredirects"
not_supported_for_hidden() {
local cmd="$1"
local repo_url
repo_url="$(git config --get remote.origin.url)"
if [[ "$repo_url" == *"braze-inc/braze-docs-hidden"* ]]; then
IFS=',' read -ra blocked_cmds <<< "$UNSUPPORTED_HIDDEN_COMMANDS"
for blocked in "${blocked_cmds[@]}"; do
if [[ "$cmd" == "$blocked" ]]; then
echo "error: '$cmd' is not supported in the braze-docs-hidden repository."
exit 1
fi
done
fi
}
# Fetch the latest changes from the remote quietly.
require_git_fetch() {
git fetch origin develop --quiet
}
# Check if no arguments were provided
if [[ $# -eq 0 ]]; then
display_help
exit 1
fi
# Indicates that the current process is still running and not hung.
logger() {
# Create a log file to capture command I/O.
local output_file="$TEMP_DIR/logger_output.log"
"$@" > "$output_file" 2>&1 &
local pid=$!
# A "loading spinner" to visually indicate process is running.
local spin='-\|/'
local i=0
while kill -0 "$pid" 2>/dev/null; do
i=$(( (i+1) % 4 ))
printf "\r[%c] Running..." "${spin:$i:1}"
sleep 0.1
done
wait $pid
local exit_code=$?
if [ $exit_code -eq 0 ]; then
printf "\r[✓] Complete. \n"
else
printf "\r[✗] Failed. \n"
fi
# Return any command-level logs if applicable
if [ -s "$output_file" ]; then
printf "\n"
cat "$output_file"
fi
return $exit_code
}
# Always check if the command is unsupported in hidden repo
not_supported_for_hidden "$1"
# Argument parsing
case $1 in
deploy)
require_git_fetch
require_new_merges
if [[ $# -eq 3 ]]; then
"$DEPLOY" "$2" "$3"
else
"$DEPLOY"
fi
;;
release)
require_git_fetch
require_new_merges
require_jq
"$RELEASE"
;;
tlinks)
require_path_or_file "$2"
"$TLINKS" "$2"
"$RLINKS" "$2" # Run rlinks next, to clean up unused reference links.
;;
rlinks)
require_path_or_file "$2"
"$RLINKS" "$2"
;;
ulinks)
require_git_fetch
require_path_or_file "$2"
touch "$REDIRECT_MATCHES"
"$MRD"
logger "$ULINKS" "$2"
# rm "$REDIRECT_MATCHES"
;;
mredirects)
"$MREDIRECTS"
;;
lredirects)
require_git_fetch
if [[ $# -eq 2 ]]; then
"$LREDIRECTS" "$2"
else
"$LREDIRECTS"
fi
;;
fblinks)
# require_git_fetch
require_yarn
npx ts-node ./scripts/find_broken_links.ts
;;
clangs)
"$CLANGS"
;;
rake)
"$RAKE"
;;
syntax)
"$SYNTAX"
;;
help)
display_help
;;
*)
echo "Error: Invalid choice: '$1'. To see all options, run: ./bdocs help"
exit 1
;;
esac