Skip to content

Commit e54ba6a

Browse files
authored
feat(storybook): incorporate the new chromatic plugin (#2376)
1 parent 8172ec9 commit e54ba6a

File tree

9 files changed

+359
-9
lines changed

9 files changed

+359
-9
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
CHROMATIC_PROJECT_TOKEN=1234abcd
2+
CHROMATIC_PROJECT_ID="Project:64762974a45b8bc5ca1705a2"
3+
CHROMATIC_BUILD_SCRIPT_NAME="ci:storybook"
24

35
# NX settings
46
NX_PREFER_TS_NODE=true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ build-storybook.log
2525
chromatic.log
2626
chromatic-build-*.xml
2727
chromatic-diagnostics*.json
28+
chromatic.config.json
2829

2930
# Custom diff tool
3031
.diff-output

.storybook/.env.example

Lines changed: 0 additions & 1 deletion
This file was deleted.

.storybook/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ module.exports = {
3232
"@whitespace/storybook-addon-html",
3333
// https://storybook.js.org/addons/@etchteam/storybook-addon-status
3434
"@etchteam/storybook-addon-status",
35-
"storybook-addon-pseudo-states",
35+
"storybook-addon-pseudo-states",
3636
// https://github.com/storybookjs/storybook/tree/next/code/addons/interactions
37-
"@storybook/addon-interactions"
37+
"@storybook/addon-interactions",
38+
// https://www.chromatic.com/docs/visual-testing-addon/
39+
"@chromaui/addon-visual-tests",
3840
],
3941
core: {
4042
disableTelemetry: true,

.storybook/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"devDependencies": {
2121
"@babel/core": "^7.22.1",
22+
"@chromaui/addon-visual-tests": "^0.0.124",
2223
"@etchteam/storybook-addon-status": "^4.2.4",
2324
"@spectrum-css/component-builder": "^4.0.19",
2425
"@storybook/addon-a11y": "^7.5.1",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"preinstall": "command -v nvm >/dev/null 2>&1 && nvm use || exit 0",
3434
"new": "nx run @spectrum-css/generator:new",
3535
"precommit": "lint-staged",
36-
"prepare": "husky install && run-p refresh:*",
36+
"prepare": "husky install && run-p refresh:directory refresh:env && yarn refresh:config",
37+
"refresh:config": "test -n $BASH_VERSION && bash ./tasks/chromatic-config-creation.sh || exit 0",
3738
"refresh:directory": "test -n $BASH_VERSION && bash ./tasks/clean-up-after-migration.sh || exit 0",
3839
"refresh:env": "test -n $BASH_VERSION && bash ./tasks/copy-env-from-root.sh || exit 0",
3940
"prerelease": "nx affected --target build",

tasks/chromatic-config-creation.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/bash
2+
3+
# Copyright 2023 Adobe. All rights reserved.
4+
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License. You may obtain a copy
6+
# of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
8+
# Unless required by applicable law or agreed to in writing, software distributed under
9+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10+
# OF ANY KIND, either express or implied. See the License for the specific language
11+
# governing permissions and limitations under the License.
12+
13+
env="$(pwd)/.env"
14+
config_path="$(pwd)/.storybook/chromatic.config.json"
15+
16+
create_config() {
17+
[[ ! -f "$1" || -f "$2" ]] && return
18+
19+
echo "🪄 Using .env to generate a local chromatic.config.json"
20+
21+
# Initialize the chromatic config file
22+
echo "{" > $2
23+
24+
# Read in the local env example, line-by-line and look for variable names
25+
while IFS= read -r line; do
26+
[[ "$line" != "CHROMATIC"* ]] && continue
27+
28+
# Split the line into an array with 0=key, 1=value
29+
while IFS='=' read -ra parts; do
30+
if [[ "${parts[0]}" = "" || "${parts[1]}" = "" ]]; then
31+
continue
32+
fi
33+
34+
# Remove the CHROMATIC_ prefix and convert to lower case
35+
while IFS='_' read -ra split; do
36+
for i in "${!split[@]}"; do
37+
[[ "${split[$i]}" = "CHROMATIC" ]] && unset "split[$i]"
38+
split[$i]=$(tr '[:upper:]' '[:lower:]' <<< ${split[$i]})
39+
# If the part is the first in the array, capitalize it
40+
[[ $i > 1 ]] && split[$i]=$(tr '[:lower:]' '[:upper:]' <<< ${split[$i]:0:1})${split[$i]:1}
41+
done
42+
43+
key=$(printf "%s" "${split[@]}")
44+
done <<< "${parts[0]}"
45+
46+
# Write the key/value pair to the chromatic config file
47+
echo " \"${key}\": \"${parts[1]//\"/}\"," >> $2
48+
49+
done <<< "$line"
50+
done < $1
51+
52+
# Remove the last comma from the chromatic config file
53+
sed '$ s/,$//' $2 > $2.tmp && mv $2.tmp $2
54+
55+
# Close the chromatic config file
56+
echo "}" >> $2
57+
echo "" >> $2
58+
}
59+
60+
create_config $env $config_path

tasks/copy-env-from-root.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ copy_env_file() {
4040
continue
4141
fi
4242

43-
# If the variable does not exist in the home .env file, write it to the local .env file as-is
44-
if [[ -z $(grep "${example_var[0]}=" $HOME/.env) ]]; then
43+
if ! test -f $HOME/.env; then
4544
echo "${example_var[0]}=${example_var[1]}" >> .env
4645
continue
4746
fi
4847

49-
if ! test -f $HOME/.env; then
48+
# If the variable does not exist in the home .env file, write it to the local .env file as-is
49+
if [[ -z $(grep "${example_var[0]}=" $HOME/.env) ]]; then
50+
echo "${example_var[0]}=${example_var[1]}" >> .env
5051
continue
5152
fi
5253

0 commit comments

Comments
 (0)