Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix react dependency versions during initial install #1669

Merged
merged 2 commits into from
Feb 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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