Skip to content

Commit

Permalink
Add devDependencies support for templates
Browse files Browse the repository at this point in the history
Summary:
Add devDependencies support to React Native templates.
Template can have a devDependencies.json file with devDependencies inside.
Using separate files to dependencies and devDependencies, it maintains compatibility with the current version.

Allows React Native templates to have devDependencies, which can help applications to have better organization, quality and testability. It's possible start a new app with some dependencies for dev support like prettier, reactotron, eslint packages and others.

Add a devDependencies.json file with at least one dependency (like prettier)

[CLI] [FEATURE] [local-cli/generator/templates.js] - Add support to devDependencies for react native templates
Closes #18164

Differential Revision: D7660744

Pulled By: hramos

fbshipit-source-id: 6fbb13832d2d1bd0c06bada0842c890dd99cf331
  • Loading branch information
lucianomlima authored and facebook-github-bot committed Apr 17, 2018
1 parent c10c6db commit c4ab03a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions local-cli/generator/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function createFromBuiltInTemplate(templateName, destPath, newProjectName, yarnV
newProjectName,
);
installTemplateDependencies(templatePath, yarnVersion);
installTemplateDevDependencies(templatePath, yarnVersion);
}

/**
Expand Down Expand Up @@ -143,6 +144,7 @@ function createFromRemoteTemplate(template, destPath, newProjectName, yarnVersio
}
);
installTemplateDependencies(templatePath, yarnVersion);
installTemplateDevDependencies(templatePath, yarnVersion);
} finally {
// Clean up the temp files
try {
Expand Down Expand Up @@ -196,6 +198,38 @@ function installTemplateDependencies(templatePath, yarnVersion) {
execSync('react-native link', {stdio: 'inherit'});
}

function installTemplateDevDependencies(templatePath, yarnVersion) {
// devDependencies.json is a special file that lists additional develop dependencies
// that are required by this template
const devDependenciesJsonPath = path.resolve(
templatePath, 'devDependencies.json'
);
console.log('Adding develop dependencies for the project...');
if (!fs.existsSync(devDependenciesJsonPath)) {
console.log('No additional develop dependencies.');
return;
}

let dependencies;
try {
dependencies = JSON.parse(fs.readFileSync(devDependenciesJsonPath));
} catch (err) {
throw new Error(
'Could not parse the template\'s devDependencies.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} -D`, {stdio: 'inherit'});
} else {
execSync(`npm install ${depToInstall} --save-dev --save-exact`, {stdio: 'inherit'});
}
}
}

module.exports = {
listTemplatesAndExit,
createProjectFromTemplate,
Expand Down

0 comments on commit c4ab03a

Please sign in to comment.