Skip to content

Commit c4ab03a

Browse files
lucianomlimafacebook-github-bot
authored andcommitted
Add devDependencies support for templates
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
1 parent c10c6db commit c4ab03a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

local-cli/generator/templates.js

+34
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function createFromBuiltInTemplate(templateName, destPath, newProjectName, yarnV
9999
newProjectName,
100100
);
101101
installTemplateDependencies(templatePath, yarnVersion);
102+
installTemplateDevDependencies(templatePath, yarnVersion);
102103
}
103104

104105
/**
@@ -143,6 +144,7 @@ function createFromRemoteTemplate(template, destPath, newProjectName, yarnVersio
143144
}
144145
);
145146
installTemplateDependencies(templatePath, yarnVersion);
147+
installTemplateDevDependencies(templatePath, yarnVersion);
146148
} finally {
147149
// Clean up the temp files
148150
try {
@@ -196,6 +198,38 @@ function installTemplateDependencies(templatePath, yarnVersion) {
196198
execSync('react-native link', {stdio: 'inherit'});
197199
}
198200

201+
function installTemplateDevDependencies(templatePath, yarnVersion) {
202+
// devDependencies.json is a special file that lists additional develop dependencies
203+
// that are required by this template
204+
const devDependenciesJsonPath = path.resolve(
205+
templatePath, 'devDependencies.json'
206+
);
207+
console.log('Adding develop dependencies for the project...');
208+
if (!fs.existsSync(devDependenciesJsonPath)) {
209+
console.log('No additional develop dependencies.');
210+
return;
211+
}
212+
213+
let dependencies;
214+
try {
215+
dependencies = JSON.parse(fs.readFileSync(devDependenciesJsonPath));
216+
} catch (err) {
217+
throw new Error(
218+
'Could not parse the template\'s devDependencies.json: ' + err.message
219+
);
220+
}
221+
for (let depName in dependencies) {
222+
const depVersion = dependencies[depName];
223+
const depToInstall = depName + '@' + depVersion;
224+
console.log('Adding ' + depToInstall + '...');
225+
if (yarnVersion) {
226+
execSync(`yarn add ${depToInstall} -D`, {stdio: 'inherit'});
227+
} else {
228+
execSync(`npm install ${depToInstall} --save-dev --save-exact`, {stdio: 'inherit'});
229+
}
230+
}
231+
}
232+
199233
module.exports = {
200234
listTemplatesAndExit,
201235
createProjectFromTemplate,

0 commit comments

Comments
 (0)