@@ -5,6 +5,7 @@ const paramCase = require('param-case');
55
66const templates = require ( './templates' ) ;
77const { hasPrefix, createFile, createFolder } = require ( './utils' ) ;
8+ const { execSync } = require ( 'child_process' ) ;
89
910const DEFAULT_NAME = 'Library' ;
1011const DEFAULT_PREFIX = 'RN' ;
@@ -16,6 +17,7 @@ const DEFAULT_GITHUB_ACCOUNT = 'github_account'
1617const DEFAULT_AUTHOR_NAME = 'Your Name'
1718const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com'
1819const DEFAULT_LICENSE = 'Apache-2.0'
20+ const DEFAULT_GENERATE_EXAMPLE = false ;
1921
2022module . exports = ( {
2123 namespace,
@@ -29,6 +31,7 @@ module.exports = ({
2931 authorName = DEFAULT_AUTHOR_NAME ,
3032 authorEmail = DEFAULT_AUTHOR_EMAIL ,
3133 license = DEFAULT_LICENSE ,
34+ generateExample = DEFAULT_GENERATE_EXAMPLE ,
3235} ) => {
3336 if ( ! overridePrefix ) {
3437 if ( hasPrefix ( name ) ) {
@@ -55,34 +58,59 @@ module.exports = ({
5558 identifier, it is recommended to customize the package identifier.` ) ;
5659 }
5760
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+ }
8075
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+ } ;
8392
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+ } ) ;
88116} ;
0 commit comments