forked from Oreoxmt/pingcap-docsite-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·76 lines (64 loc) · 2.27 KB
/
build.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
set -e
# Get the directory of this script.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
cd "$SCRIPT_DIR"
# Select appropriate versions of find and sed depending on the operating system.
FIND=$(which gfind || which find)
SED=$(which gsed || which sed)
replace_image_path() {
# Update image paths in Markdown files.
( cd markdown-pages
$FIND . -maxdepth 3 -mindepth 3 | while IFS= read -r DIR; do
DIR="${DIR#./}"
PREFIX="$(dirname "$DIR")"
$FIND "$DIR" -name '*.md' | while IFS= read -r FILE; do
$SED -r -i "s~]\(/media(/$PREFIX)?~](/media/$PREFIX~g" "$FILE"
done
done
)
}
move_images() {
# Move all image files to the target directory.
( cd markdown-pages
$FIND . -maxdepth 3 -mindepth 3 | while IFS= read -r DIR; do
PREFIX="$(dirname "$DIR")"
# Check if the media directory exists.
if [ -d "$PREFIX/master/media" ]; then
# Create the target directory.
mkdir -p "../website-docs/public/media/$PREFIX"
# Copy all image files to the target directory.
cp -r "$PREFIX/master/media/." "../website-docs/public/media/$PREFIX"
fi
done
)
}
# The default command is build, which builds the website for production.
CMD=build
# If the argument is develop or dev, change the command to start, which builds the website for development.
if [ "$1" == "develop" ] || [ "$1" == "dev" ]; then
CMD=start
fi
if [ ! -e website-docs/.git ]; then
if [ -d "website-docs" ]; then
rm -rf website-docs
fi
# Clone the pingcap/website-docs repository.
git clone https://github.com/pingcap/website-docs
fi
# Create a symlink to markdown-pages in website-docs/docs.
if [ ! -e website-docs/docs/markdown-pages ]; then
ln -s ../../markdown-pages website-docs/docs/markdown-pages
fi
# Copy docs.json to website-docs/docs.
cp docs.json website-docs/docs/docs.json
# Run the start command for development environment. <https://www.gatsbyjs.com/docs/reference/gatsby-cli/#develop>
if [ "$CMD" == "start" ]; then
(cd website-docs && yarn && yarn start)
fi
# Run the build command for production environment. <https://www.gatsbyjs.com/docs/reference/gatsby-cli/#build>
if [ "$CMD" == "build" ]; then
replace_image_path
(cd website-docs && yarn && yarn build)
move_images
fi