@@ -6,6 +6,8 @@ const chalk = require('chalk');
66const yosay = require ( 'yosay' ) ;
77const inquirer = require ( 'inquirer' ) ;
88const packageJson = require ( 'package-json' ) ;
9+ const spawn = require ( 'cross-spawn' ) ;
10+ const eos = require ( 'end-of-stream' ) ;
911const globby = require ( 'globby' ) ;
1012const { formatVersionFolder } = require ( '../utility' ) ;
1113
@@ -18,15 +20,22 @@ module.exports = class extends Generator {
1820 )
1921 ) ;
2022
23+ let yarnAdd ;
24+
2125 const prompts = [
2226 {
2327 type : 'input' ,
2428 name : 'libraryName' ,
2529 message : "What is the npm module's name?" ,
2630 validate : ( libraryName , answers ) => {
27- this . spawnCommand ( 'yarn' , [ 'add' , libraryName ] , {
28- stdio : 'ignore' ,
29- } ) ;
31+ yarnAdd = new Promise ( ( resolve , reject ) =>
32+ eos (
33+ spawn ( 'yarn' , [ 'add' , libraryName ] , {
34+ stdio : 'ignore' ,
35+ } ) ,
36+ err => ( err ? reject ( err ) : resolve ( ) )
37+ )
38+ ) ;
3039
3140 return packageJson ( libraryName , {
3241 fullMetadata : true ,
@@ -86,32 +95,36 @@ module.exports = class extends Generator {
8695 type : 'list' ,
8796 name : 'relativePath' ,
8897 message : 'What is the main JavaScript file?' ,
89- choices : answers => {
90- try {
91- return globby
92- . sync ( `node_modules/${ answers . libraryName } /**/*.js` )
93- . map ( file =>
94- file . replace (
95- `node_modules/${ answers . libraryName } /` ,
96- ''
98+ choices : ( { libraryName } ) =>
99+ yarnAdd
100+ . then ( ( ) =>
101+ globby ( [
102+ `node_modules/${ libraryName } /**/*.js` ,
103+ `!node_modules/${ libraryName } /node_modules/**/*.js` ,
104+ ] )
105+ )
106+ . then ( files =>
107+ files
108+ . map ( file =>
109+ file . replace (
110+ `node_modules/${ libraryName } /` ,
111+ ''
112+ )
97113 )
98- )
99- . map ( path . normalize )
100- . map ( file => file . replace ( / \\ / g , '/' ) )
101- . concat ( [ new inquirer . Separator ( ) , 'Other' ] ) ;
102- } catch ( err ) {
103- this . log . error (
104- 'There was an unexpected error retrieving files: \n %O' ,
105- err
106- ) ;
114+ . map ( path . normalize )
115+ . map ( file => file . replace ( / \\ / g , '/' ) )
116+ . concat ( [ new inquirer . Separator ( ) , 'Other' ] )
117+ )
118+ . catch ( err => {
119+ this . log . error (
120+ 'There was an unexpected error retrieving files: \n %O' ,
121+ err
122+ ) ;
107123
108- return [ 'Other' ] ;
109- }
110- } ,
111- default : answers =>
112- path
113- . normalize ( answers . pkg . browser || answers . pkg . main )
114- . replace ( / \\ / g, '/' ) ,
124+ return [ 'Other' ] ;
125+ } ) ,
126+ default : ( { pkg } ) =>
127+ path . normalize ( pkg . browser || pkg . main ) . replace ( / \\ / g, '/' ) ,
115128 filter : relativePath => relativePath . replace ( / \\ / g, '/' ) ,
116129 } ,
117130 {
0 commit comments