-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_all_commits.sh
executable file
·131 lines (103 loc) · 5.14 KB
/
build_all_commits.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
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
#!/bin/bash
# This bash script builds the static site for every commit in the git history
# and saves the generated static site in a folder named with the commit hash.
# This script is useful for testing the static site for every commit in the
# git history.
# Get a list of all commit hashes
# Exclude commits that start with 'DEV' (these are commits that were only
# impact the development environment and do not need to be built)
# Only include the 'public' folder, 'src' folder, and 'theme.ts' file
commits=$(git log --pretty=format:'%h' --invert-grep --grep='^DEV' -- public src theme.ts)
# Save the current branch name
current_branch=$(git symbolic-ref --short HEAD)
# Make a temporary non-versioned folder to store the files used in this script
mkdir -p tmp_bash_script_files
# Before checking out a new branch, store the currnet src/components/VersionDrawer.tsx in the directory tmp_bash_script_files
cp src/components/VersionDrawer.tsx tmp_bash_script_files/VersionDrawer.tsx
# Before checking out a new branch, store the currnet src/pages/api/commits.ts in in the directory tmp_bash_script_files
cp src/pages/api/commits.ts tmp_bash_script_files/commits.ts
# Check if there are any changes to stash
stash_needed=$(git diff-index --quiet HEAD -- || echo "yes")
# Store any uncommitted changes
if [ "$stash_needed" == "yes" ]; then
git stash
fi
# Iterate through each commit hash
for commit in $commits; do
# Check if the folder for this commit already exists
if [ ! -d "versions/$commit" ]; then
echo "Checking out commit: $commit"
# Checkout the commit
git checkout --force $commit
# Install dependencies specific to this commit
if [ -f "package.json" ]; then
# Install dependencies specific to this commit
yarn install
fi
# Replace the condition in App.tsx file to include all dev features in production build
perl -i -pe 's/const environment = process\.env\.NODE_ENV/const environment = "development"/g' src/components/App.tsx
# This is needed for the oldest versions of the site that didn't have a separate environment variable
perl -i -pe 's/\{process.env.NODE_ENV === "development" && <VersionDrawer windowSize=\{windowSize\} \/>\}/\{true && <VersionDrawer windowSize=\{windowSize\} \/>\}/g' src/components/App.tsx
# Replace the entire VersionDrawer component in src/components/VersionDrawer.tsx with the one stored in the temporary file
# only if the file exists in the current branch (as it doesn't for the oldest versions)
if [ -f "src/components/VersionDrawer.tsx" ]; then
cp tmp_bash_script_files/VersionDrawer.tsx src/components/VersionDrawer.tsx
# Change the redirection URL so that it doesn't duplicate the commit path
perl -i -p0e 's/\n\s*const handleClick = \(\) => \{\n\s*router.push\(`\/\${commit}\${router.asPath}`\)\n\s*\}//s' src/components/VersionDrawer.tsx
sed "/const router = useRouter()/a \\
const handleClick = () => {\\
const pathAfterCommit = router.asPath.substring(8);\\
router.push(\`/\${commit}\${pathAfterCommit}\`);\\
};" src/components/VersionDrawer.tsx > tmpfile && mv tmpfile src/components/VersionDrawer.tsx
fi
# Replace the entire commits api in src/pages/api/commits.ts with the one stored in the temporary file
# only if the file exists in the current branch (as it doesn't for the oldest versions)
if [ -f "src/pages/api/commits.ts" ]; then
cp tmp_bash_script_files/commits.ts src/pages/api/commits.ts
# Remove the condition that checks if the NODE_ENV is development
perl -i -p0e 's/\n\s*if \(process.env.NODE_ENV !== "development"\) \{\n\s*res.status\(403\).json\(\{ error: "This API is only available in development mode."\} \)\n\s*return\n\s*\}//s' src/pages/api/commits.ts
fi
# Build the static site
yarn build && yarn next export
# Move the generated static site to a folder named with the commit hash
mkdir -p "versions/$commit"
mv out/* "versions/$commit"
# Clean up the 'out' directory
rm -rf out
# Update static asset URLs
# It's easier to have this an an embedded Node.js script
# rather than a separate file, otherwise, that other file might be a different version
# in the branch that it's reading from
node -e "$(cat << 'EOF'
const fs = require("fs")
const path = require("path")
const commit = process.argv[1]
const updateAssets = (commit) => {
const basePath = path.join(process.cwd(), "versions", commit)
const files = fs.readdirSync(basePath)
files.forEach((file) => {
if (file.endsWith(".html")) {
const filePath = path.join(basePath, file)
let content = fs.readFileSync(filePath, "utf-8")
content = content.replace(/\/_next\//g, `/${commit}/_next/`)
fs.writeFileSync(filePath, content)
}
})
}
updateAssets(commit)
EOF
)" $commit
fi
done
# Return to the original branch
git checkout --force $current_branch
# Re-install dependencies for the original branch
if [ -f "package.json" ]; then
yarn install
fi
# Clean up the tmp_bash_script_files directory
rm -rf tmp_bash_script_files
# Pop the stash if there were changes to stash
if [ "$stash_needed" == "yes" ]; then
git stash pop > /dev/null 2>&1
fi