Commit 2d93ae12 authored by Johann Hubert Sonntagbauer's avatar Johann Hubert Sonntagbauer Committed by Dan Abramov
Browse files

fix react dependency versions during initial install (#1669)

* fix react dependency versions during initial install

* add review remarks
parent a3a223aa
4 merge requests!12191Lim.Pisey.168:/Identified - We are currently investigating reports of missing build logs. The issue has been identified and a resolution is in progress. We will provide a further update when available.Mar 21, 09:02 UTC,!12853brikk,!5717Automatically extract project file structure from build bundle file,!1933Add note about installing watchman
Showing with 29 additions and 3 deletions
+29 -3
......@@ -212,8 +212,9 @@ function run(root, appName, version, verbose, originalDirectory, template) {
checkNodeVersion(packageName);
// Since react-scripts has been installed with --save
// We need to move it into devDependencies and rewrite package.json
moveReactScriptsToDev(packageName);
// we need to move it into devDependencies and rewrite package.json
// also ensure react dependencies have caret version range
fixDependencies(packageName);
var scriptsPath = path.resolve(
process.cwd(),
......@@ -325,7 +326,29 @@ function checkAppName(appName) {
}
}
function moveReactScriptsToDev(packageName) {
function makeCaretRange(dependencies, name) {
var version = dependencies[name];
if (typeof version === 'undefined') {
console.error(
chalk.red('Missing ' + name + ' dependency in package.json')
);
process.exit(1);
}
var patchedVersion = '^' + version;
if (!semver.validRange(patchedVersion)) {
console.error(
'Unable to patch ' + name + ' dependency version because version ' + chalk.red(version) + ' will become invalid ' + chalk.red(patchedVersion)
);
patchedVersion = version;
}
dependencies[name] = patchedVersion;
}
function fixDependencies(packageName) {
var packagePath = path.join(process.cwd(), 'package.json');
var packageJson = require(packagePath);
......@@ -349,6 +372,9 @@ function moveReactScriptsToDev(packageName) {
packageJson.devDependencies[packageName] = packageVersion;
delete packageJson.dependencies[packageName];
makeCaretRange(packageJson.dependencies, 'react');
makeCaretRange(packageJson.dependencies, 'react-dom');
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment