@@ -6,6 +6,14 @@ const chalk = require('chalk');
66const { upperFirst } = require ( 'lodash' ) ;
77
88const root = path . resolve ( process . cwd ( ) ) ;
9+ const checkPkgManager = pathExistsSync (
10+ path . resolve ( process . cwd ( ) , 'yarn.lock' ) ,
11+ ) ;
12+
13+ const usePkgManager = checkPkgManager ? 'yarn' : 'npm' ;
14+
15+ // yarn.cmd/npm.cmd run in windows
16+ const isWin = process . platform === 'win32' ;
917
1018function getOptions ( opts ) {
1119 const { create, all } = opts ;
@@ -59,23 +67,50 @@ function setInit() {
5967 const storyPath = path . normalize ( `${ __dirname } /..//.storybook` ) ;
6068 prompt ( initial ) . then ( ( res ) => {
6169 if ( ! res . init ) return ;
62- copy ( storyPath , `${ root } \\src\\.storybook` , ( err ) => {
63- if ( err ) console . log ( err ) ;
64- console . log ( 'success create dir /.storybook' ) ;
65- } ) ;
70+
71+ const isYarn = [
72+ 'add' ,
73+ '--dev' ,
74+ '@storybook/react' ,
75+ '@babel/core' ,
76+ '@storybook/addon-actions' ,
77+ '@storybook/addon-essentials' ,
78+ 'babel-loader' ,
79+ ] ;
80+
81+ const isNpm = [
82+ 'install' ,
83+ '@storybook/react' ,
84+ '@babel/core' ,
85+ '@storybook/addon-actions' ,
86+ '@storybook/addon-essentials' ,
87+ 'babel-loader' ,
88+ '--save-dev' ,
89+ ] ;
90+
91+ copy ( storyPath , `${ root } \\src\\.storybook` )
92+ . then ( ( ) => {
93+ console . log ( 'success build directory ./.storybook' ) ;
94+ console . log ( 'waiting install dependencies...' ) ;
95+
96+ const getPkg = spawn (
97+ isWin ? `${ usePkgManager } .cmd` : `${ usePkgManager } ` ,
98+ checkPkgManager ? isYarn : isNpm ,
99+ { stdio : 'inherit' } ,
100+ ) ;
101+
102+ getPkg . on ( 'error' , ( err ) => {
103+ console . error ( err ) ;
104+ process . exit ( 1 ) ;
105+ } ) ;
106+ } )
107+ . catch ( ( err ) => {
108+ console . log ( err ) ;
109+ } ) ;
66110 } ) ;
67111}
68112
69113function runStorybook ( ) {
70- const checkPkgManager = pathExistsSync (
71- path . resolve ( process . cwd ( ) , 'yarn.lock' ) ,
72- ) ;
73-
74- const usePkgManager = checkPkgManager ? 'yarn' : 'npm' ;
75-
76- // yarn.cmd/npm.cmd run in windows
77- const isWin = process . platform === 'win32' ;
78-
79114 const sb = spawn (
80115 isWin ? `${ usePkgManager } .cmd` : `${ usePkgManager } ` ,
81116 checkPkgManager ? [ 'start-storybook' , '-p' , '9001' ] : [ 'run' , 'storybook' ] ,
0 commit comments