Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Options:
--author-name <name> The author's name (Default: `Your Name`)
--author-email <email> The author's email (Default: `yourname@email.com`)
--license <license> The license type of this library (Default: `Apache-2.0`)
--generate-example <shouldGenerate> Will generate a RN example project and link the new library to it (Default: `false`)
```

## Programmatic usage
Expand All @@ -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`) */
}
```

Expand Down
7 changes: 6 additions & 1 deletion command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -31,7 +32,8 @@ module.exports = {
githubAccount,
authorName,
authorEmail,
license
license,
generateExample,
}).then(() => {
console.log(`
${emoji.get('books')} Created library ${name} in \`./${name}\`.
Expand Down Expand Up @@ -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',
}]
};
84 changes: 56 additions & 28 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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)) {
Expand All @@ -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);
});
};