@@ -5,6 +5,7 @@ const paramCase = require('param-case');
5
5
6
6
const templates = require ( './templates' ) ;
7
7
const { hasPrefix, createFile, createFolder } = require ( './utils' ) ;
8
+ const { execSync } = require ( 'child_process' ) ;
8
9
9
10
const DEFAULT_NAME = 'Library' ;
10
11
const DEFAULT_PREFIX = 'RN' ;
@@ -16,6 +17,7 @@ const DEFAULT_GITHUB_ACCOUNT = 'github_account'
16
17
const DEFAULT_AUTHOR_NAME = 'Your Name'
17
18
const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com'
18
19
const DEFAULT_LICENSE = 'Apache-2.0'
20
+ const DEFAULT_GENERATE_EXAMPLE = false ;
19
21
20
22
module . exports = ( {
21
23
namespace,
@@ -29,6 +31,7 @@ module.exports = ({
29
31
authorName = DEFAULT_AUTHOR_NAME ,
30
32
authorEmail = DEFAULT_AUTHOR_EMAIL ,
31
33
license = DEFAULT_LICENSE ,
34
+ generateExample = DEFAULT_GENERATE_EXAMPLE ,
32
35
} ) => {
33
36
if ( ! overridePrefix ) {
34
37
if ( hasPrefix ( name ) ) {
@@ -55,34 +58,59 @@ module.exports = ({
55
58
identifier, it is recommended to customize the package identifier.` ) ;
56
59
}
57
60
58
- return Promise . all ( templates . filter ( ( template ) => {
59
- if ( template . platform ) {
60
- return ( platforms . indexOf ( template . platform ) >= 0 ) ;
61
- }
62
-
63
- return true ;
64
- } ) . map ( ( template ) => {
65
- if ( ! template . name ) {
66
- return Promise . resolve ( ) ;
67
- }
68
-
69
- const args = {
70
- name : `${ prefix } ${ pascalCase ( name ) } ` ,
71
- moduleName : `${ modulePrefix } -${ paramCase ( name ) } ` ,
72
- packageIdentifier,
73
- namespace : namespace || pascalCase ( name ) . split ( / (? = [ A - Z ] ) / ) . join ( '.' ) ,
74
- platforms,
75
- githubAccount,
76
- authorName,
77
- authorEmail,
78
- license,
79
- } ;
61
+ return createFolder ( name )
62
+ . then ( ( ) => {
63
+ if ( ! generateExample ) {
64
+ return Promise . resolve ( )
65
+ }
66
+ // Note: The example has to be created first because it will fail if there
67
+ // is already a package.json in the folder in which the command is executed.
68
+ return execSync ( 'react-native init example' , { cwd : './' + name , stdio :'inherit' } ) ;
69
+ } )
70
+ . then ( ( ) => {
71
+ return Promise . all ( templates . filter ( ( template ) => {
72
+ if ( template . platform ) {
73
+ return ( platforms . indexOf ( template . platform ) >= 0 ) ;
74
+ }
80
75
81
- const filename = path . join ( name , template . name ( args ) ) ;
82
- const baseDir = filename . split ( path . basename ( filename ) ) [ 0 ] ;
76
+ return true ;
77
+ } ) . map ( ( template ) => {
78
+ if ( ! template . name ) {
79
+ return Promise . resolve ( ) ;
80
+ }
81
+ const args = {
82
+ name : `${ prefix } ${ pascalCase ( name ) } ` ,
83
+ moduleName : `${ modulePrefix } -${ paramCase ( name ) } ` ,
84
+ packageIdentifier,
85
+ namespace : namespace || pascalCase ( name ) . split ( / (? = [ A - Z ] ) / ) . join ( '.' ) ,
86
+ platforms,
87
+ githubAccount,
88
+ authorName,
89
+ authorEmail,
90
+ license,
91
+ } ;
83
92
84
- return createFolder ( baseDir ) . then ( ( ) =>
85
- createFile ( filename , template . content ( args ) )
86
- ) ;
87
- } ) ) ;
93
+ const filename = path . join ( name , template . name ( args ) ) ;
94
+ var baseDir = filename . split ( path . basename ( filename ) ) [ 0 ] ;
95
+
96
+ return createFolder ( baseDir ) . then ( ( ) =>
97
+ createFile ( filename , template . content ( args ) )
98
+ ) ;
99
+ } ) ) ;
100
+ } )
101
+ . then ( ( ) => {
102
+ if ( ! generateExample ) {
103
+ return Promise . resolve ( ) ;
104
+ }
105
+ // Adds and links the created library project
106
+ const pathExampleApp = `./${ name } /example` ;
107
+ const options = { cwd : pathExampleApp , stdio :'inherit' } ;
108
+ try {
109
+ execSync ( 'yarn add file:../' , options ) ;
110
+ } catch ( e ) {
111
+ execSync ( 'npm install ../' , options ) ;
112
+ execSync ( 'npm install' , options ) ;
113
+ }
114
+ execSync ( 'react-native link' , options ) ;
115
+ } ) ;
88
116
} ;
0 commit comments