Skip to content

Commit 09be700

Browse files
committed
Add config validation
* Add checks for browsers in config * Also, send lower case browser names in payload so that it doesn't cause mismatch across services
1 parent 2827b7a commit 09be700

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

commands/storybook.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function storybook(serve, options) {
2323
resolutions.push(element.join('x'));
2424
});
2525
storybookConfig.resolutions = (!resolutions.length) ? 'all' : resolutions.toString();
26-
storybookConfig.browsers = (!storybookConfig.browsers.length) ? 'all' : storybookConfig.browsers.toString();
26+
storybookConfig.browsers = (!storybookConfig.browsers.length) ? 'all' : storybookConfig.browsers.map(x => x.toLowerCase()).toString();
2727

2828
// Get stories object from stories.json and add url corresponding to every story ID
2929
await axios.get(new URL('stories.json', url).href)
@@ -38,7 +38,7 @@ async function storybook(serve, options) {
3838
}
3939
}
4040
}
41-
41+
4242
if (Object.keys(stories).length === 0) {
4343
console.log('[smartui] Error: No stories found');
4444
process.exit(0);
@@ -60,7 +60,7 @@ async function storybook(serve, options) {
6060
} else {
6161
let dirPath = serve;
6262
await validateStorybookDir(dirPath);
63-
63+
6464
// Get storyIds to be rendered
6565
let storyIds = static.filterStories(dirPath, storybookConfig)
6666

@@ -78,7 +78,7 @@ async function storybook(serve, options) {
7878
console.log(`[smartui] Cannot compress ${dirPath}. Error: ${err.message}`);
7979
process.exit(0);
8080
});
81-
81+
8282
// Upload to S3
8383
const zipData = fs.readFileSync('storybook-static.zip');
8484
console.log('[smartui] Upload in progress...')
@@ -97,18 +97,22 @@ async function storybook(serve, options) {
9797
process.exit(0);
9898
});
9999

100-
// Prepare payload data
100+
// Prepare payload data
101+
let browsers = []
101102
let resolutions = []
103+
storybookConfig.browsers.forEach(element => {
104+
browsers.push(element.toLowerCase());
105+
});
102106
storybookConfig.resolutions.forEach(element => {
103-
resolutions.push({width: element[0], height: element[1]});
107+
resolutions.push({ width: element[0], height: element[1] });
104108
});
105109
let commit = await getLastCommit();
106110
let payload = {
107111
downloadURL: url.substring(url.search(/.com/)+5, url.search(/.zip/)+4),
108112
uploadId: uploadId,
109113
projectToken: process.env.PROJECT_TOKEN,
110114
storybookConfig: {
111-
browsers: storybookConfig.browsers,
115+
browsers: browsers,
112116
resolutions: resolutions,
113117
storyIds: storyIds
114118
},
@@ -132,7 +136,7 @@ async function storybook(serve, options) {
132136
console.log('[smartui] Build failed: Error: ', error.response.data.error?.message);
133137
} else {
134138
console.log('[smartui] Build failed: Error: ', error.message);
135-
}
139+
}
136140
});
137141
})
138142
.catch(function (error) {

commands/utils/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ constants.prod = {
2020
GET_SIGNED_URL_PATH: "/storybook/url",
2121
STATIC_RENDER_PATH: "/storybook/staticrender"
2222
};
23+
constants.VALID_BROWSERS = ['chrome', 'safari', 'firefox'];
2324

2425
module.exports = { constants };

commands/utils/validate.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,33 @@ function validateConfig(configFile) {
112112

113113
// Sanity check browsers
114114
if (storybookConfig.browsers.length == 0) {
115-
console.log('[smartui] Error: Empty browsers list in config.')
115+
console.log('[smartui] Error: Empty browsers list in config.');
116+
process.exit(0);
117+
}
118+
if (storybookConfig.browsers.length > constants.VALID_BROWSERS.length) {
119+
console.log('[smartui] Error: Invalid or duplicate browsers in config.');
120+
process.exit(0);
116121
}
117122
storybookConfig.browsers.forEach(element => {
118-
if (!(['chrome', 'safari', 'firefox'].includes(element.toLowerCase()))) {
119-
console.log('[smartui] Error: Invalid value for browser. Accepted browsers are chrome, safari and firefox');
123+
if (!(constants.VALID_BROWSERS.includes(element.toLowerCase()))) {
124+
console.log(`[smartui] Error: Invalid value for browser. Accepted browsers are ${constants.VALID_BROWSERS.join(',')}`);
120125
process.exit(0);
121126
}
122127
});
123128

124129
// Sanity check resolutions
125130
if (storybookConfig.resolutions.length == 0) {
126-
console.log('[smartui] Error: Invalid number of resolutions. Min. required - 1')
131+
console.log('[smartui] Error: Invalid number of resolutions. Min. required - 1');
132+
process.exit(0);
127133
}
128134
if (storybookConfig.resolutions.length > 5) {
129-
console.log('[smartui] Error: Invalid number of resolutions. Max allowed - 5')
135+
console.log('[smartui] Error: Invalid number of resolutions. Max allowed - 5');
136+
process.exit(0);
130137
}
131138
storybookConfig.resolutions.forEach(element => {
132139
if (element.length != 2 || element[0] <= 0 || element[1] <= 0) {
133140
console.log('[smartui] Error: Invalid resolutions.')
141+
process.exit(0);
134142
}
135143
});
136144

0 commit comments

Comments
 (0)