Skip to content

Commit

Permalink
fix react dependency versions during initial install (#1669)
Browse files Browse the repository at this point in the history
* fix react dependency versions during initial install

* add review remarks
  • Loading branch information
johann-sonntagbauer authored and gaearon committed Feb 28, 2017
1 parent c5d8cfd commit 649c346
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);

Expand All @@ -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));
}

Expand Down

0 comments on commit 649c346

Please sign in to comment.