@@ -4,7 +4,7 @@ const pascalCase = require('pascal-case');
44const paramCase = require ( 'param-case' ) ;
55
66const templates = require ( './templates' ) ;
7- const { hasPrefix, createFile, createFolder } = require ( './utils' ) ;
7+ const { hasPrefix, createFile, createFolder, npmAddScriptSync , exec } = require ( './utils' ) ;
88const { execSync } = require ( 'child_process' ) ;
99
1010const DEFAULT_NAME = 'Library' ;
@@ -13,12 +13,21 @@ const DEFAULT_MODULE_PREFIX = 'react-native';
1313const DEFAULT_PACKAGE_IDENTIFIER = 'com.reactlibrary' ;
1414const DEFAULT_PLATFORMS = [ 'android' , 'ios' , 'windows' ] ;
1515const DEFAULT_OVERRIDE_PREFIX = false ;
16- const DEFAULT_GITHUB_ACCOUNT = 'github_account'
17- const DEFAULT_AUTHOR_NAME = 'Your Name'
18- const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com'
19- const DEFAULT_LICENSE = 'Apache-2.0'
16+ const DEFAULT_GITHUB_ACCOUNT = 'github_account' ;
17+ const DEFAULT_AUTHOR_NAME = 'Your Name' ;
18+ const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com' ;
19+ const DEFAULT_LICENSE = 'Apache-2.0' ;
2020const DEFAULT_GENERATE_EXAMPLE = false ;
2121
22+ const renderTemplate = ( name , template , templateArgs ) => {
23+ const filename = path . join ( name , template . name ( templateArgs ) ) ;
24+ const baseDir = filename . split ( path . basename ( filename ) ) [ 0 ] ;
25+
26+ return createFolder ( baseDir ) . then ( ( ) =>
27+ createFile ( filename , template . content ( templateArgs ) )
28+ ) ;
29+ }
30+
2231module . exports = ( {
2332 namespace,
2433 name = DEFAULT_NAME ,
@@ -58,16 +67,9 @@ module.exports = ({
5867 identifier, it is recommended to customize the package identifier.` ) ;
5968 }
6069
61- const moduleName = rootFolderName = `${ modulePrefix } -${ paramCase ( name ) } ` ;
70+ const moduleName = `${ modulePrefix } -${ paramCase ( name ) } ` ;
71+ const rootFolderName = moduleName ;
6272 return createFolder ( rootFolderName )
63- . then ( ( ) => {
64- if ( ! generateExample ) {
65- return Promise . resolve ( )
66- }
67- // Note: The example has to be created first because it will fail if there
68- // is already a package.json in the folder in which the command is executed.
69- return execSync ( 'react-native init example' , { cwd : './' + rootFolderName , stdio :'inherit' } ) ;
70- } )
7173 . then ( ( ) => {
7274 return Promise . all ( templates . filter ( ( template ) => {
7375 if ( template . platform ) {
@@ -79,7 +81,7 @@ module.exports = ({
7981 if ( ! template . name ) {
8082 return Promise . resolve ( ) ;
8183 }
82- const args = {
84+ const templateArgs = {
8385 name : `${ prefix } ${ pascalCase ( name ) } ` ,
8486 moduleName,
8587 packageIdentifier,
@@ -89,29 +91,52 @@ module.exports = ({
8991 authorName,
9092 authorEmail,
9193 license,
94+ generateExample,
9295 } ;
9396
94- const filename = path . join ( rootFolderName , template . name ( args ) ) ;
95- var baseDir = filename . split ( path . basename ( filename ) ) [ 0 ] ;
96-
97- return createFolder ( baseDir ) . then ( ( ) =>
98- createFile ( filename , template . content ( args ) )
99- ) ;
97+ return renderTemplate ( rootFolderName , template , templateArgs ) ;
10098 } ) ) ;
10199 } )
102100 . then ( ( ) => {
101+ // Generate the example if necessary
103102 if ( ! generateExample ) {
104103 return Promise . resolve ( ) ;
105104 }
106- // Adds and links the created library project
107- const pathExampleApp = `./${ name } /example` ;
108- const options = { cwd : pathExampleApp , stdio :'inherit' } ;
109- try {
110- execSync ( 'yarn add file:../' , options ) ;
111- } catch ( e ) {
112- execSync ( 'npm install ../' , options ) ;
113- execSync ( 'npm install' , options ) ;
114- }
115- execSync ( 'react-native link' , options ) ;
105+
106+ const initExampleOptions = { cwd : `./${ rootFolderName } ` , stdio : 'inherit' } ;
107+ return exec ( 'react-native init example' , initExampleOptions )
108+ . then ( ( ) => {
109+ // Execute the example template
110+ const exampleTemplates = require ( './templates/example' ) ;
111+ return Promise . all (
112+ exampleTemplates . map ( ( template ) => {
113+ return renderTemplate ( rootFolderName , template ) ;
114+ } )
115+ ) ;
116+ } )
117+ . then ( ( ) => {
118+ // Adds and link the new library
119+ return new Promise ( ( resolve , reject ) => {
120+ // Add postinstall script to example package.json
121+ const pathExampleApp = `./${ rootFolderName } /example` ;
122+ const moduleName = `${ modulePrefix } -${ paramCase ( name ) } ` ;
123+ npmAddScriptSync ( `${ pathExampleApp } /package.json` , {
124+ key : 'postinstall' ,
125+ value : `node ../scripts/examples_postinstall.js node_modules/${ moduleName } `
126+ } ) ;
127+
128+ // Add and link the new library
129+ const addLinkLibraryOptions = { cwd : pathExampleApp , stdio : 'inherit' } ;
130+ try {
131+ execSync ( 'yarn add file:../' , addLinkLibraryOptions ) ;
132+ } catch ( e ) {
133+ execSync ( 'npm install ../' , addLinkLibraryOptions ) ;
134+ execSync ( 'npm install' , addLinkLibraryOptions ) ;
135+ }
136+ execSync ( 'react-native link' , addLinkLibraryOptions ) ;
137+
138+ return resolve ( ) ;
139+ } ) ;
140+ } ) ;
116141 } ) ;
117142} ;
0 commit comments