@@ -18,6 +18,56 @@ const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
1818const CPP_FILES = path . resolve ( __dirname , '../templates/cpp-library' ) ;
1919const OBJC_FILES = path . resolve ( __dirname , '../templates/objc-library' ) ;
2020
21+ type ArgName =
22+ | 'slug'
23+ | 'description'
24+ | 'author-name'
25+ | 'author-email'
26+ | 'author-url'
27+ | 'repo-url'
28+ | 'type' ;
29+
30+ type Answers = {
31+ slug : string ;
32+ description : string ;
33+ authorName : string ;
34+ authorEmail : string ;
35+ authorUrl : string ;
36+ repoUrl : string ;
37+ type : 'native' | 'cpp' | 'expo' ;
38+ } ;
39+
40+ export const args : Record < ArgName , yargs . Options > = {
41+ 'slug' : {
42+ description : 'Name of the npm package' ,
43+ type : 'string' ,
44+ } ,
45+ 'description' : {
46+ description : 'Description of the npm package' ,
47+ type : 'string' ,
48+ } ,
49+ 'author-name' : {
50+ description : 'Name of the package author' ,
51+ type : 'string' ,
52+ } ,
53+ 'author-email' : {
54+ description : 'Email address of the package author' ,
55+ type : 'string' ,
56+ } ,
57+ 'author-url' : {
58+ description : 'URL for the package author' ,
59+ type : 'string' ,
60+ } ,
61+ 'repo-url' : {
62+ description : 'URL for the repository' ,
63+ type : 'string' ,
64+ } ,
65+ 'type' : {
66+ description : 'Type package do you want to develop' ,
67+ choices : [ 'native' , 'cpp' , 'expo' ] ,
68+ } ,
69+ } ;
70+
2171export default async function create ( argv : yargs . Arguments < any > ) {
2272 const folder = path . join ( process . cwd ( ) , argv . name ) ;
2373
@@ -49,18 +99,9 @@ export default async function create(argv: yargs.Arguments<any>) {
4999
50100 const basename = path . basename ( argv . name ) ;
51101
52- const {
53- slug,
54- description,
55- authorName,
56- authorEmail,
57- authorUrl,
58- githubUrl : repo ,
59- type,
60- } = ( await inquirer . prompt ( [
61- {
102+ const questions : Record < ArgName , inquirer . Question > = {
103+ 'slug' : {
62104 type : 'input' ,
63- name : 'slug' ,
64105 message : 'What is the name of the npm package?' ,
65106 default : validateNpmPackage ( basename ) . validForNewPackages
66107 ? / ^ ( @ | r e a c t - n a t i v e ) / . test ( basename )
@@ -71,30 +112,26 @@ export default async function create(argv: yargs.Arguments<any>) {
71112 validateNpmPackage ( input ) . validForNewPackages ||
72113 'Must be a valid npm package name' ,
73114 } ,
74- {
115+ 'description' : {
75116 type : 'input' ,
76- name : 'description' ,
77117 message : 'What is the description for the package?' ,
78118 validate : ( input ) => Boolean ( input ) ,
79119 } ,
80- {
120+ 'author-name' : {
81121 type : 'input' ,
82- name : 'authorName' ,
83122 message : 'What is the name of package author?' ,
84123 default : name ,
85124 validate : ( input ) => Boolean ( input ) ,
86125 } ,
87- {
126+ 'author-email' : {
88127 type : 'input' ,
89- name : 'authorEmail' ,
90128 message : 'What is the email address for the package author?' ,
91129 default : email ,
92130 validate : ( input ) =>
93131 / ^ \S + @ \S + $ / . test ( input ) || 'Must be a valid email address' ,
94132 } ,
95- {
133+ 'author-url' : {
96134 type : 'input' ,
97- name : 'authorUrl' ,
98135 message : 'What is the URL for the package author?' ,
99136 default : async ( answers : any ) => {
100137 try {
@@ -109,9 +146,8 @@ export default async function create(argv: yargs.Arguments<any>) {
109146 } ,
110147 validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
111148 } ,
112- {
149+ 'repo-url' : {
113150 type : 'input' ,
114- name : 'githubUrl' ,
115151 message : 'What is the URL for the repository?' ,
116152 default : ( answers : any ) => {
117153 if ( / ^ h t t p s ? : \/ \/ g i t h u b .c o m \/ [ ^ / ] + / . test ( answers . authorUrl ) ) {
@@ -124,10 +160,10 @@ export default async function create(argv: yargs.Arguments<any>) {
124160 } ,
125161 validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
126162 } ,
127- {
163+ 'type' : {
128164 type : 'list' ,
129- name : 'type' ,
130165 message : 'What type of package do you want to develop?' ,
166+ // @ts -ignore - seems types are wrong for inquirer
131167 choices : [
132168 { name : 'Native module in Kotlin and Objective-C' , value : 'native' } ,
133169 { name : 'Native module with C++ code' , value : 'cpp' } ,
@@ -138,16 +174,34 @@ export default async function create(argv: yargs.Arguments<any>) {
138174 ] ,
139175 default : 'native' ,
140176 } ,
141- ] ) ) as {
142- slug : string ;
143- description : string ;
144- authorName : string ;
145- authorEmail : string ;
146- authorUrl : string ;
147- githubUrl : string ;
148- type : 'native' | 'cpp' | 'expo' ;
149177 } ;
150178
179+ const {
180+ slug,
181+ description,
182+ authorName,
183+ authorEmail,
184+ authorUrl,
185+ repoUrl,
186+ type,
187+ } = {
188+ ...argv ,
189+ ...( await inquirer . prompt (
190+ Object . entries ( questions ) . map ( ( [ key , value ] ) => ( {
191+ ...value ,
192+ name : key . replace ( / \b - ( [ a - z ] ) / g, ( _ , char ) => char . toUpperCase ( ) ) ,
193+ when : ! ( argv [ key ] && value . validate
194+ ? value . validate ( argv [ key ] ) === true
195+ : Boolean ( argv [ key ] ) ) ,
196+ default :
197+ typeof value . default === 'function'
198+ ? ( answers : Partial < Answers > ) =>
199+ value . default ( { ...argv , ...answers } )
200+ : value . default ,
201+ } ) )
202+ ) ) ,
203+ } as Answers ;
204+
151205 const project = slug . replace ( / ^ ( r e a c t - n a t i v e - | @ [ ^ / ] + \/ ) / , '' ) ;
152206
153207 const options = {
@@ -172,7 +226,7 @@ export default async function create(argv: yargs.Arguments<any>) {
172226 email : authorEmail ,
173227 url : authorUrl ,
174228 } ,
175- repo,
229+ repo : repoUrl ,
176230 } ;
177231
178232 const copyDir = async ( source : string , dest : string ) => {
0 commit comments