Skip to content

Commit

Permalink
chore(storybook): script to use local styles in dev (carbon-design-sy…
Browse files Browse the repository at this point in the history
…stem#10915)

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

* chore(yarn): using yarn start instead

* chore(yarn): require fswatch for script
  • Loading branch information
IgnacioBecerra authored Sep 22, 2023
1 parent 75175c0 commit 36b824f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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

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

0 comments on commit 36b824f

Please sign in to comment.