Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(storybook): script to use local styles in dev #10915

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/web-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"doctoc": "doctoc --title '## Table of contents' docs && doctoc --title '## Table of contents' README.md",
"postinstall": "carbon-telemetry collect --install",
"start": "cross-env NODE_OPTIONS=--openssl-legacy-provider yarn storybook",
"start-dev": "./update-scss-paths.sh & yarn start",
"storybook": "NODE_OPTIONS=--openssl-legacy-provider start-storybook -p 9000",
"storybook:react": "node --openssl-legacy-provider node_modules/@storybook/react/bin/index.js -p 9002 -c .storybook/react",
"test": "yarn test:unit && yarn test:integration",
Expand Down
42 changes: 42 additions & 0 deletions packages/web-components/update-scss-paths.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

declare -a component_names

IgnacioBecerra marked this conversation as resolved.
Show resolved Hide resolved
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
IgnacioBecerra marked this conversation as resolved.
Show resolved Hide resolved
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