@@ -18,6 +18,56 @@ const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
18
18
const CPP_FILES = path . resolve ( __dirname , '../templates/cpp-library' ) ;
19
19
const OBJC_FILES = path . resolve ( __dirname , '../templates/objc-library' ) ;
20
20
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
+
21
71
export default async function create ( argv : yargs . Arguments < any > ) {
22
72
const folder = path . join ( process . cwd ( ) , argv . name ) ;
23
73
@@ -49,18 +99,9 @@ export default async function create(argv: yargs.Arguments<any>) {
49
99
50
100
const basename = path . basename ( argv . name ) ;
51
101
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' : {
62
104
type : 'input' ,
63
- name : 'slug' ,
64
105
message : 'What is the name of the npm package?' ,
65
106
default : validateNpmPackage ( basename ) . validForNewPackages
66
107
? / ^ ( @ | 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>) {
71
112
validateNpmPackage ( input ) . validForNewPackages ||
72
113
'Must be a valid npm package name' ,
73
114
} ,
74
- {
115
+ 'description' : {
75
116
type : 'input' ,
76
- name : 'description' ,
77
117
message : 'What is the description for the package?' ,
78
118
validate : ( input ) => Boolean ( input ) ,
79
119
} ,
80
- {
120
+ 'author-name' : {
81
121
type : 'input' ,
82
- name : 'authorName' ,
83
122
message : 'What is the name of package author?' ,
84
123
default : name ,
85
124
validate : ( input ) => Boolean ( input ) ,
86
125
} ,
87
- {
126
+ 'author-email' : {
88
127
type : 'input' ,
89
- name : 'authorEmail' ,
90
128
message : 'What is the email address for the package author?' ,
91
129
default : email ,
92
130
validate : ( input ) =>
93
131
/ ^ \S + @ \S + $ / . test ( input ) || 'Must be a valid email address' ,
94
132
} ,
95
- {
133
+ 'author-url' : {
96
134
type : 'input' ,
97
- name : 'authorUrl' ,
98
135
message : 'What is the URL for the package author?' ,
99
136
default : async ( answers : any ) => {
100
137
try {
@@ -109,9 +146,8 @@ export default async function create(argv: yargs.Arguments<any>) {
109
146
} ,
110
147
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
111
148
} ,
112
- {
149
+ 'repo-url' : {
113
150
type : 'input' ,
114
- name : 'githubUrl' ,
115
151
message : 'What is the URL for the repository?' ,
116
152
default : ( answers : any ) => {
117
153
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>) {
124
160
} ,
125
161
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
126
162
} ,
127
- {
163
+ 'type' : {
128
164
type : 'list' ,
129
- name : 'type' ,
130
165
message : 'What type of package do you want to develop?' ,
166
+ // @ts -ignore - seems types are wrong for inquirer
131
167
choices : [
132
168
{ name : 'Native module in Kotlin and Objective-C' , value : 'native' } ,
133
169
{ name : 'Native module with C++ code' , value : 'cpp' } ,
@@ -138,16 +174,34 @@ export default async function create(argv: yargs.Arguments<any>) {
138
174
] ,
139
175
default : 'native' ,
140
176
} ,
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' ;
149
177
} ;
150
178
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
+
151
205
const project = slug . replace ( / ^ ( r e a c t - n a t i v e - | @ [ ^ / ] + \/ ) / , '' ) ;
152
206
153
207
const options = {
@@ -172,7 +226,7 @@ export default async function create(argv: yargs.Arguments<any>) {
172
226
email : authorEmail ,
173
227
url : authorUrl ,
174
228
} ,
175
- repo,
229
+ repo : repoUrl ,
176
230
} ;
177
231
178
232
const copyDir = async ( source : string , dest : string ) => {
0 commit comments