Skip to content

Commit 2b04cfc

Browse files
Adds Option To Generate ReactNative Example project (#57)
* Adds Option To Generate ReactNative Example project * updated readme * Changed path to use template, Added try/catch to use npm when yarn is not avaialble * Moved createFolder out, changed empty return to Promise.resolve()
1 parent db4549a commit 2b04cfc

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Options:
5151
--author-name <name> The author's name (Default: `Your Name`)
5252
--author-email <email> The author's email (Default: `yourname@email.com`)
5353
--license <license> The license type of this library (Default: `Apache-2.0`)
54+
--generate-example <shouldGenerate> Will generate a RN example project and link the new library to it (Default: `false`)
5455
```
5556

5657
## Programmatic usage
@@ -77,6 +78,7 @@ createLibrary({
7778
authorName: String, /* The author's name (Default: `Your Name`) */
7879
authorEmail: String, /* The author's email (Default: `yourname@email.com`) */
7980
license: String, /* The license type of this library (Default: `Apache-2.0`) */
81+
generateExample: Boolean, /* Will generate a RN example project and link the new library to it (Default: `false`) */
8082
}
8183
```
8284

command.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
const authorName = options.authorName;
1919
const authorEmail = options.authorEmail;
2020
const license = options.license;
21+
const generateExample = options.generateExample;
2122

2223
const beforeCreation = Date.now();
2324
createLibrary({
@@ -31,7 +32,8 @@ module.exports = {
3132
githubAccount,
3233
authorName,
3334
authorEmail,
34-
license
35+
license,
36+
generateExample,
3537
}).then(() => {
3638
console.log(`
3739
${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
8486
command: '--license [license]',
8587
description: 'The license type (Default: `Apache-2.0`)',
8688
default: 'Apache-2.0',
89+
}, {
90+
command: '--generate-example',
91+
description: 'Generates an example project for iOS and Android and links the library to it',
8792
}]
8893
};

lib.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const paramCase = require('param-case');
55

66
const templates = require('./templates');
77
const { hasPrefix, createFile, createFolder } = require('./utils');
8+
const { execSync } = require('child_process');
89

910
const DEFAULT_NAME = 'Library';
1011
const DEFAULT_PREFIX = 'RN';
@@ -16,6 +17,7 @@ const DEFAULT_GITHUB_ACCOUNT = 'github_account'
1617
const DEFAULT_AUTHOR_NAME = 'Your Name'
1718
const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com'
1819
const DEFAULT_LICENSE = 'Apache-2.0'
20+
const DEFAULT_GENERATE_EXAMPLE = false;
1921

2022
module.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

Comments
 (0)