mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
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 https://github.com/facebook/react-native/pull/18164 Differential Revision: D7660744 Pulled By: hramos fbshipit-source-id: 6fbb13832d2d1bd0c06bada0842c890dd99cf331
This commit is contained in:
committed by
Facebook Github Bot
parent
c10c6dbf7c
commit
c4ab03a18e
@@ -99,6 +99,7 @@ function createFromBuiltInTemplate(templateName, destPath, newProjectName, yarnV
|
||||
newProjectName,
|
||||
);
|
||||
installTemplateDependencies(templatePath, yarnVersion);
|
||||
installTemplateDevDependencies(templatePath, yarnVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,6 +144,7 @@ function createFromRemoteTemplate(template, destPath, newProjectName, yarnVersio
|
||||
}
|
||||
);
|
||||
installTemplateDependencies(templatePath, yarnVersion);
|
||||
installTemplateDevDependencies(templatePath, yarnVersion);
|
||||
} finally {
|
||||
// Clean up the temp files
|
||||
try {
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user