diff --git a/README.md b/README.md index b2d935c..22b651a 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Options: --author-name The author's name (Default: `Your Name`) --author-email The author's email (Default: `yourname@email.com`) --license The license type of this library (Default: `Apache-2.0`) + --generate-example Will generate a RN example project and link the new library to it (Default: `false`) ``` ## Programmatic usage @@ -77,6 +78,7 @@ createLibrary({ authorName: String, /* The author's name (Default: `Your Name`) */ authorEmail: String, /* The author's email (Default: `yourname@email.com`) */ license: String, /* The license type of this library (Default: `Apache-2.0`) */ + generateExample: Boolean, /* Will generate a RN example project and link the new library to it (Default: `false`) */ } ``` diff --git a/command.js b/command.js index 814c37f..934506e 100644 --- a/command.js +++ b/command.js @@ -18,6 +18,7 @@ module.exports = { const authorName = options.authorName; const authorEmail = options.authorEmail; const license = options.license; + const generateExample = options.generateExample; const beforeCreation = Date.now(); createLibrary({ @@ -31,7 +32,8 @@ module.exports = { githubAccount, authorName, authorEmail, - license + license, + generateExample, }).then(() => { console.log(` ${emoji.get('books')} Created library ${name} in \`./${name}\`. @@ -84,5 +86,8 @@ ${emoji.get('arrow_right')} To get started type \`cd ./${name}\` and run \`npm command: '--license [license]', description: 'The license type (Default: `Apache-2.0`)', default: 'Apache-2.0', + }, { + command: '--generate-example', + description: 'Generates an example project for iOS and Android and links the library to it', }] }; diff --git a/lib.js b/lib.js index e74ebc4..c1ab5b7 100644 --- a/lib.js +++ b/lib.js @@ -5,6 +5,7 @@ const paramCase = require('param-case'); const templates = require('./templates'); const { hasPrefix, createFile, createFolder } = require('./utils'); +const { execSync } = require('child_process'); const DEFAULT_NAME = 'Library'; const DEFAULT_PREFIX = 'RN'; @@ -16,6 +17,7 @@ const DEFAULT_GITHUB_ACCOUNT = 'github_account' const DEFAULT_AUTHOR_NAME = 'Your Name' const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com' const DEFAULT_LICENSE = 'Apache-2.0' +const DEFAULT_GENERATE_EXAMPLE = false; module.exports = ({ namespace, @@ -29,6 +31,7 @@ module.exports = ({ authorName = DEFAULT_AUTHOR_NAME, authorEmail = DEFAULT_AUTHOR_EMAIL, license = DEFAULT_LICENSE, + generateExample = DEFAULT_GENERATE_EXAMPLE, }) => { if (!overridePrefix) { if (hasPrefix(name)) { @@ -55,34 +58,59 @@ module.exports = ({ identifier, it is recommended to customize the package identifier.`); } - return Promise.all(templates.filter((template) => { - if (template.platform) { - return (platforms.indexOf(template.platform) >= 0); - } - - return true; - }).map((template) => { - if (!template.name) { - return Promise.resolve(); - } - - const args = { - name: `${prefix}${pascalCase(name)}`, - moduleName: `${modulePrefix}-${paramCase(name)}`, - packageIdentifier, - namespace: namespace || pascalCase(name).split(/(?=[A-Z])/).join('.'), - platforms, - githubAccount, - authorName, - authorEmail, - license, - }; + return createFolder(name) + .then(() => { + if (!generateExample) { + return Promise.resolve() + } + // Note: The example has to be created first because it will fail if there + // is already a package.json in the folder in which the command is executed. + return execSync('react-native init example', { cwd: './' + name, stdio:'inherit'}); + }) + .then(() => { + return Promise.all(templates.filter((template) => { + if (template.platform) { + return (platforms.indexOf(template.platform) >= 0); + } - const filename = path.join(name, template.name(args)); - const baseDir = filename.split(path.basename(filename))[0]; + return true; + }).map((template) => { + if (!template.name) { + return Promise.resolve(); + } + const args = { + name: `${prefix}${pascalCase(name)}`, + moduleName: `${modulePrefix}-${paramCase(name)}`, + packageIdentifier, + namespace: namespace || pascalCase(name).split(/(?=[A-Z])/).join('.'), + platforms, + githubAccount, + authorName, + authorEmail, + license, + }; - return createFolder(baseDir).then(() => - createFile(filename, template.content(args)) - ); - })); + const filename = path.join(name, template.name(args)); + var baseDir = filename.split(path.basename(filename))[0]; + + return createFolder(baseDir).then(() => + createFile(filename, template.content(args)) + ); + })); + }) + .then(() => { + if (!generateExample) { + return Promise.resolve(); + } + // Adds and links the created library project + const pathExampleApp = `./${name}/example`; + const options = { cwd: pathExampleApp, stdio:'inherit'}; + try { + execSync('yarn add file:../', options); + } catch (e) { + execSync('npm install ../', options); + execSync('npm install', options); + } + execSync('react-native link', options); + }); };