@@ -3,11 +3,11 @@ import fs from 'fs-extra';
3
3
import ejs from 'ejs' ;
4
4
import dedent from 'dedent' ;
5
5
import chalk from 'chalk' ;
6
- import inquirer from 'inquirer' ;
7
6
import type yargs from 'yargs' ;
8
7
import spawn from 'cross-spawn' ;
9
8
import validateNpmPackage from 'validate-npm-package-name' ;
10
9
import githubUsername from 'github-username' ;
10
+ import prompts , { PromptObject } from './utils/prompts' ;
11
11
import pack from '../package.json' ;
12
12
13
13
const BINARIES = / ( g r a d l e w | \. ( j a r | k e y s t o r e | p n g | j p g | g i f ) ) $ / ;
@@ -140,11 +140,17 @@ export default async function create(argv: yargs.Arguments<any>) {
140
140
141
141
const basename = path . basename ( argv . name ) ;
142
142
143
- const questions : Record < ArgName , inquirer . Question > = {
143
+ const questions : Record <
144
+ ArgName ,
145
+ Omit < PromptObject < keyof Answers > , 'validate' > & {
146
+ validate ?: ( value : string ) => boolean | string ;
147
+ }
148
+ > = {
144
149
'slug' : {
145
- type : 'input' ,
150
+ type : 'text' ,
151
+ name : 'slug' ,
146
152
message : 'What is the name of the npm package?' ,
147
- default : validateNpmPackage ( basename ) . validForNewPackages
153
+ initial : validateNpmPackage ( basename ) . validForNewPackages
148
154
? / ^ ( @ | r e a c t - n a t i v e ) / . test ( basename )
149
155
? basename
150
156
: `react-native-${ basename } `
@@ -154,29 +160,34 @@ export default async function create(argv: yargs.Arguments<any>) {
154
160
'Must be a valid npm package name' ,
155
161
} ,
156
162
'description' : {
157
- type : 'input' ,
163
+ type : 'text' ,
164
+ name : 'description' ,
158
165
message : 'What is the description for the package?' ,
159
- validate : ( input ) => Boolean ( input ) ,
166
+ validate : ( input ) => Boolean ( input ) || 'Cannot be empty' ,
160
167
} ,
161
168
'author-name' : {
162
- type : 'input' ,
169
+ type : 'text' ,
170
+ name : 'authorName' ,
163
171
message : 'What is the name of package author?' ,
164
- default : name ,
165
- validate : ( input ) => Boolean ( input ) ,
172
+ initial : name ,
173
+ validate : ( input ) => Boolean ( input ) || 'Cannot be empty' ,
166
174
} ,
167
175
'author-email' : {
168
- type : 'input' ,
176
+ type : 'text' ,
177
+ name : 'authorEmail' ,
169
178
message : 'What is the email address for the package author?' ,
170
- default : email ,
179
+ initial : email ,
171
180
validate : ( input ) =>
172
181
/ ^ \S + @ \S + $ / . test ( input ) || 'Must be a valid email address' ,
173
182
} ,
174
183
'author-url' : {
175
- type : 'input' ,
184
+ type : 'text' ,
185
+ name : 'authorUrl' ,
176
186
message : 'What is the URL for the package author?' ,
177
- default : async ( answers : any ) => {
187
+ // @ts -ignore: this is supported, but types are wrong
188
+ initial : async ( previous : string ) => {
178
189
try {
179
- const username = await githubUsername ( answers . authorEmail ) ;
190
+ const username = await githubUsername ( previous ) ;
180
191
181
192
return `https://github.com/${ username } ` ;
182
193
} catch ( e ) {
@@ -188,39 +199,46 @@ export default async function create(argv: yargs.Arguments<any>) {
188
199
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
189
200
} ,
190
201
'repo-url' : {
191
- type : 'input' ,
202
+ type : 'text' ,
203
+ name : 'repoUrl' ,
192
204
message : 'What is the URL for the repository?' ,
193
- default : ( answers : any ) => {
205
+ // @ts -ignore: this is supported, but types are wrong
206
+ initial : ( _ : string , answers : Answers ) => {
194
207
if ( / ^ h t t p s ? : \/ \/ g i t h u b .c o m \/ [ ^ / ] + / . test ( answers . authorUrl ) ) {
195
208
return `${ answers . authorUrl } /${ answers . slug
196
209
. replace ( / ^ @ / , '' )
197
210
. replace ( / \/ / g, '-' ) } `;
198
211
}
199
212
200
- return undefined ;
213
+ return '' ;
201
214
} ,
202
215
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
203
216
} ,
204
217
'type' : {
205
- type : 'list' ,
218
+ type : 'select' ,
219
+ name : 'type' ,
206
220
message : 'What type of package do you want to develop?' ,
207
- // @ts -ignore - seems types are wrong for inquirer
208
221
choices : [
209
- { name : 'Native module in Kotlin and Objective-C' , value : 'native' } ,
210
- { name : 'Native module in Kotlin and Swift' , value : 'native-swift' } ,
211
- { name : 'Native module with C++ code' , value : 'cpp' } ,
212
- { name : 'Native view in Kotlin and Objective-C' , value : 'native-view' } ,
213
- { name : 'Native view in Kotlin and Swift' , value : 'native-view-swift' } ,
222
+ { title : 'Native module in Kotlin and Objective-C' , value : 'native' } ,
223
+ { title : 'Native module in Kotlin and Swift' , value : 'native-swift' } ,
224
+ { title : 'Native module with C++ code' , value : 'cpp' } ,
225
+ {
226
+ title : 'Native view in Kotlin and Objective-C' ,
227
+ value : 'native-view' ,
228
+ } ,
214
229
{
215
- name : 'JavaScript library with native example' ,
230
+ title : 'Native view in Kotlin and Swift' ,
231
+ value : 'native-view-swift' ,
232
+ } ,
233
+ {
234
+ title : 'JavaScript library with native example' ,
216
235
value : 'js' ,
217
236
} ,
218
237
{
219
- name : 'JavaScript library with Expo example and Web support' ,
238
+ title : 'JavaScript library with Expo example and Web support' ,
220
239
value : 'expo' ,
221
240
} ,
222
241
] ,
223
- default : 'native' ,
224
242
} ,
225
243
} ;
226
244
@@ -234,19 +252,15 @@ export default async function create(argv: yargs.Arguments<any>) {
234
252
type,
235
253
} = {
236
254
...argv ,
237
- ...( await inquirer . prompt (
238
- Object . entries ( questions ) . map ( ( [ key , value ] ) => ( {
239
- ...value ,
240
- name : key . replace ( / \b - ( [ a - z ] ) / g, ( _ , char ) => char . toUpperCase ( ) ) ,
241
- when : ! ( argv [ key ] && value . validate
242
- ? value . validate ( argv [ key ] ) === true
243
- : Boolean ( argv [ key ] ) ) ,
244
- default :
245
- typeof value . default === 'function'
246
- ? ( answers : Partial < Answers > ) =>
247
- value . default ( { ...argv , ...answers } )
248
- : value . default ,
249
- } ) )
255
+ ...( await prompts (
256
+ Object . entries ( questions )
257
+ . filter (
258
+ ( [ k , v ] ) =>
259
+ ! ( argv [ k ] && v . validate
260
+ ? v . validate ( argv [ k ] ) === true
261
+ : Boolean ( argv [ k ] ) )
262
+ )
263
+ . map ( ( [ , v ] ) => v )
250
264
) ) ,
251
265
} as Answers ;
252
266
0 commit comments