-
Notifications
You must be signed in to change notification settings - Fork 156
/
update-scss-paths.sh
executable file
·42 lines (33 loc) · 1.24 KB
/
update-scss-paths.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
#!/bin/bash
declare -a component_names
if ! command -v fswatch &>/dev/null; then
echo "fswatch required (https://github.com/emcrisostomo/fswatch)"
exit 1
fi
# Function to search and replace the string with proper escaping
search_and_replace_string() {
directory="$1"
search_string="$2"
replace_string="$3"
# search for scss files under directory
find "$directory" -type f -name "*.scss" ! -path "*/__stories__/*" | while read -r scss_file; do
# search and replace the string in the file
sed -i "" "s#$search_string#$replace_string#g" "$scss_file"
echo "Replaced imports in $scss_file"
done
}
# monitor for file changes under the `styles` directory
fswatch -0 ../styles/ | while read -d "" changed_file
do
component=$(basename "$(dirname "$changed_file")")
target_directory="src/components/$component"
if [[ ! " ${component_names[@]} " =~ " ${component} " ]]; then
component_names+=("$component")
search_and_replace_string "$target_directory" "@carbon/ibmdotcom-styles/scss/components" "../../../../styles/scss/components"
fi
done
cleanup() {
echo "Reverting styles imports"
search_and_replace_string "src/components" "../../../../styles/scss/components" "@carbon/ibmdotcom-styles/scss/components"
}
trap cleanup EXIT