11const { default : axios } = require ( 'axios' )
22const fs = require ( 'fs' ) ;
33const { sendDoM } = require ( './utils/dom' )
4- const { validateStorybookUrl } = require ( './utils/validate' )
4+ const { validateStorybookUrl, validateStorybookDir } = require ( './utils/validate' )
55const { defaultSmartUIConfig } = require ( './utils/config' )
66const { skipStory } = require ( './utils/story' )
7+ const { createStaticServer } = require ( './utils/server' )
78
8- async function storybook ( url , options ) {
9- // Check connection with storybook url
10- await validateStorybookUrl ( url ) ;
9+ async function storybook ( serve , options ) {
10+ let server , url ;
11+ let type = / ^ h t t p s ? : \/ \/ / . test ( serve ) ? 'url' : 'dir'
12+ if ( type === 'url' ) {
13+ await validateStorybookUrl ( serve ) ;
14+ url = serve ;
15+ } else {
16+ await validateStorybookDir ( serve ) ;
17+ server = await createStaticServer ( serve ) ;
18+ url = `http://localhost:${ server . address ( ) . port } ` ;
19+ }
1120
1221 // TODO: modularize this and separate check for file exists
1322 let storybookConfig = defaultSmartUIConfig . storybook ;
@@ -37,8 +46,8 @@ async function storybook(url, options) {
3746 storybookConfig . browsers = ( ! storybookConfig . browsers . length ) ? 'all' : storybookConfig . browsers . toString ( ) ;
3847
3948 // Get stories object from stories.json and add url corresponding to every story ID
40- axios . get ( new URL ( 'stories.json' , url ) . href )
41- . then ( function ( response ) {
49+ await axios . get ( new URL ( 'stories.json' , url ) . href )
50+ . then ( async function ( response ) {
4251 let stories = { }
4352 for ( const [ storyId , storyInfo ] of Object . entries ( response . data . stories ) ) {
4453 if ( ! skipStory ( storyInfo . name , storybookConfig ) ) {
@@ -59,7 +68,7 @@ async function storybook(url, options) {
5968 }
6069 }
6170 // Capture DoM of every story and send it to renderer API
62- sendDoM ( url , stories , storybookConfig , options ) ;
71+ await sendDoM ( url , stories , storybookConfig , options ) ;
6372 } )
6473 . catch ( function ( error ) {
6574 if ( error . response ) {
@@ -69,8 +78,11 @@ async function storybook(url, options) {
6978 } else {
7079 console . log ( '[smartui] Error: Cannot fetch Storybook stories' ) ;
7180 }
72- process . exit ( 0 ) ;
7381 } ) ;
82+
83+ // Close static server
84+ if ( server ) server ?. close ( ) ;
85+
7486} ;
7587
7688module . exports = { storybook } ;
0 commit comments