Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Add support for templates fetched from npm #12548

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Link dependencies
  • Loading branch information
Martin Konicek committed Feb 27, 2017
commit 865e55ced5a9cb6a0a284fea4ebab81a42b6dc99
99 changes: 53 additions & 46 deletions local-cli/generator/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,57 +60,64 @@ function createProjectFromTemplate(destPath, newProjectName, templateKey, yarnVe
newProjectName
);

if (templateKey !== undefined) {
// Keep the files from the 'HelloWorld' template, and overwrite some of them
// with the specified project template.
// The 'HelloWorld' template contains the native files (these are used by
// all templates) and every other template only contains additional JS code.
// Reason:
// This way we don't have to duplicate the native files in every template.
// If we duplicated them we'd make RN larger and risk that people would
// forget to maintain all the copies so they would go out of sync.
const templateName = availableTemplates[templateKey];
if (templateName) {
copyProjectTemplateAndReplace(
path.resolve(
'node_modules', 'react-native', 'local-cli', 'templates', templateName
),
destPath,
newProjectName
);
} else {
throw new Error('Uknown template: ' + templateKey);
}
if (templateKey === undefined) {
// No specific template, use just the HelloWorld template above
return;
}

// Keep the files from the 'HelloWorld' template, and overwrite some of them
// with the specified project template.
// The 'HelloWorld' template contains the native files (these are used by
// all templates) and every other template only contains additional JS code.
// Reason:
// This way we don't have to duplicate the native files in every template.
// If we duplicated them we'd make RN larger and risk that people would
// forget to maintain all the copies so they would go out of sync.
const templateName = availableTemplates[templateKey];
if (templateName) {
copyProjectTemplateAndReplace(
path.resolve(
'node_modules', 'react-native', 'local-cli', 'templates', templateName
),
destPath,
newProjectName
);
} else {
throw new Error('Uknown template: ' + templateKey);
}

// Add dependencies:
installTemplateDependencies(templateName, yarnVersion);
}

// dependencies.json is a special file that lists additional dependencies
// that are required by this template
const dependenciesJsonPath = path.resolve(
'node_modules', 'react-native', 'local-cli', 'templates', templateName, 'dependencies.json'
function installTemplateDependencies(templateName, yarnVersion) {
// dependencies.json is a special file that lists additional dependencies
// that are required by this template
const dependenciesJsonPath = path.resolve(
'node_modules', 'react-native', 'local-cli', 'templates', templateName, 'dependencies.json'
);
if (!fs.existsSync(dependenciesJsonPath)) {
return;
}
console.log('Adding dependencies for the project...');
let dependencies;
try {
dependencies = JSON.parse(fs.readFileSync(dependenciesJsonPath));
} catch (err) {
throw new Error(
'Could not parse the template\'s dependencies.json: ' + err.message
);
if (fs.existsSync(dependenciesJsonPath)) {
console.log('Adding dependencies for the project...');
let dependencies;
try {
dependencies = JSON.parse(fs.readFileSync(dependenciesJsonPath));
} catch (err) {
throw new Error(
'Could not parse the template\'s dependencies.json: ' + err.message
);
}
for (let depName in dependencies) {
const depVersion = dependencies[depName];
const depToInstall = depName + '@' + depVersion;
console.log('Adding ' + depToInstall + '...');
if (yarnVersion) {
execSync(`yarn add ${depToInstall}`, {stdio: 'inherit'});
} else {
execSync(`npm install ${depToInstall} --save --save-exact`, {stdio: 'inherit'});
}
}
}
for (let depName in dependencies) {
const depVersion = dependencies[depName];
const depToInstall = depName + '@' + depVersion;
console.log('Adding ' + depToInstall + '...');
if (yarnVersion) {
execSync(`yarn add ${depToInstall}`, {stdio: 'inherit'});
} else {
execSync(`npm install ${depToInstall} --save --save-exact`, {stdio: 'inherit'});
}
}
execSync('react-native link');
}

module.exports = {
Expand Down
20 changes: 13 additions & 7 deletions local-cli/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

const log = require('npmlog');
Expand All @@ -29,6 +31,8 @@ const pollParams = require('./pollParams');
const commandStub = require('./commandStub');
const promisify = require('./promisify');

import type {ConfigT} from '../core';

log.heading = 'rnpm-link';

const dedupeAssets = (assets) => uniq(assets, asset => path.basename(asset));
Expand Down Expand Up @@ -125,18 +129,20 @@ const linkAssets = (project, assets) => {
};

/**
* Updates project and links all dependencies to it
* Updates project and links all dependencies to it.
*
* If optional argument [packageName] is provided, it's the only one that's checked
* @param args If optional argument [packageName] is provided,
* only that package is processed.
* @param config CLI config, see local-cli/core/index.js
*/
function link(args, config) {
function link(args: Array<string>, config: ConfigT) {
var project;
try {
project = config.getProjectConfig();
} catch (err) {
log.error(
'ERRPACKAGEJSON',
'No package found. Are you sure it\'s a React Native project?'
'No package found. Are you sure this is a React Native project?'
);
return Promise.reject(err);
}
Expand Down Expand Up @@ -169,15 +175,15 @@ function link(args, config) {

return promiseWaterfall(tasks).catch(err => {
log.error(
`It seems something went wrong while linking. Error: ${err.message} \n`
+ 'Please file an issue here: https://github.com/facebook/react-native/issues'
`Something went wrong while linking. Error: ${err.message} \n` +
'Please file an issue here: https://github.com/facebook/react-native/issues'
);
throw err;
});
}

module.exports = {
func: link,
description: 'links all native dependencies',
description: 'links all native dependencies (updates native build files)',
name: 'link [packageName]',
};
3 changes: 2 additions & 1 deletion local-cli/templates/HelloNavigation/dependencies.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"react-navigation": "1.0.0-beta.5"
"react-navigation": "1.0.0-beta.5",
"react-native-vector-icons": "4.0.0"
}