Skip to content

Commit 23140a3

Browse files
committed
Fix compat with older scripts
1 parent 258462b commit 23140a3

File tree

5 files changed

+85
-29
lines changed

5 files changed

+85
-29
lines changed

packages/create-react-app/createReactApp.js

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -426,32 +426,62 @@ function run(
426426
getInstallPackage(version, originalDirectory),
427427
getTemplateInstallPackage(template, originalDirectory),
428428
]).then(([packageToInstall, templateToInstall]) => {
429-
const allDependencies = [
430-
'react',
431-
'react-dom',
432-
packageToInstall,
433-
templateToInstall,
434-
];
429+
const allDependencies = ['react', 'react-dom', packageToInstall];
435430

436431
console.log('Installing packages. This might take a couple of minutes.');
432+
437433
Promise.all([
438-
getPackageName(packageToInstall),
439-
getPackageName(templateToInstall),
434+
getPackageInfo(packageToInstall),
435+
getPackageInfo(templateToInstall),
440436
])
441-
.then(([packageName, templateName]) =>
437+
.then(([packageInfo, templateInfo]) =>
442438
checkIfOnline(useYarn).then(isOnline => ({
443439
isOnline,
444-
packageName,
445-
templateName,
440+
packageInfo,
441+
templateInfo,
446442
}))
447443
)
448-
.then(({ isOnline, packageName, templateName }) => {
444+
.then(({ isOnline, packageInfo, templateInfo }) => {
445+
let packageVersion = packageInfo.version;
446+
447+
// Assume compatibility if we can't test the version.
448+
if (!semver.valid(packageVersion)) {
449+
packageVersion = '3.2.0';
450+
}
451+
452+
// Only support templates when used alongside new react-scripts versions.
453+
const supportsTemplates = semver.gte(packageVersion, '3.2.0');
454+
if (supportsTemplates) {
455+
allDependencies.push(templateToInstall);
456+
} else if (template) {
457+
console.log('');
458+
console.log(
459+
`The ${chalk.cyan(
460+
packageInfo.name
461+
)} version you're using is not compatible with the ${chalk.cyan(
462+
'--template'
463+
)} option.`
464+
);
465+
console.log('');
466+
}
467+
468+
// TODO: Remove with next major release.
469+
if (!supportsTemplates && template.includes('typescript')) {
470+
allDependencies.push(
471+
'@types/node',
472+
'@types/react',
473+
'@types/react-dom',
474+
'@types/jest',
475+
'typescript'
476+
);
477+
}
478+
449479
console.log(
450480
`Installing ${chalk.cyan('react')}, ${chalk.cyan(
451481
'react-dom'
452-
)}, and ${chalk.cyan(packageName)} with ${chalk.cyan(
453-
templateName
454-
)}...`
482+
)}, and ${chalk.cyan(packageInfo.name)}${
483+
supportsTemplates ? ` with ${chalk.cyan(templateInfo.name)}` : ''
484+
}...`
455485
);
456486
console.log();
457487

@@ -463,11 +493,14 @@ function run(
463493
verbose,
464494
isOnline
465495
).then(() => ({
466-
packageName,
467-
templateName,
496+
packageInfo,
497+
supportsTemplates,
498+
templateInfo,
468499
}));
469500
})
470-
.then(async ({ packageName, templateName }) => {
501+
.then(async ({ packageInfo, supportsTemplates, templateInfo }) => {
502+
const packageName = packageInfo.name;
503+
const templateName = supportsTemplates ? templateInfo.name : undefined;
471504
checkNodeVersion(packageName);
472505
setCaretRangeForRuntimeDeps(packageName);
473506

@@ -656,7 +689,7 @@ function extractStream(stream, dest) {
656689
}
657690

658691
// Extract package name from tarball url or path.
659-
function getPackageName(installPackage) {
692+
function getPackageInfo(installPackage) {
660693
if (installPackage.match(/^.+\.(tgz|tar\.gz)$/)) {
661694
return getTemporaryDirectory()
662695
.then(obj => {
@@ -669,9 +702,12 @@ function getPackageName(installPackage) {
669702
return extractStream(stream, obj.tmpdir).then(() => obj);
670703
})
671704
.then(obj => {
672-
const packageName = require(path.join(obj.tmpdir, 'package.json')).name;
705+
const { name, version } = require(path.join(
706+
obj.tmpdir,
707+
'package.json'
708+
));
673709
obj.cleanup();
674-
return packageName;
710+
return { name, version };
675711
})
676712
.catch(err => {
677713
// The package name could be with or without semver version, e.g. react-scripts-0.2.0-alpha.1.tgz
@@ -687,27 +723,30 @@ function getPackageName(installPackage) {
687723
assumedProjectName
688724
)}"`
689725
);
690-
return Promise.resolve(assumedProjectName);
726+
return Promise.resolve({ name: assumedProjectName });
691727
});
692728
} else if (installPackage.indexOf('git+') === 0) {
693729
// Pull package name out of git urls e.g:
694730
// git+https://github.com/mycompany/react-scripts.git
695731
// git+ssh://github.com/mycompany/react-scripts.git#v1.2.3
696-
return Promise.resolve(installPackage.match(/([^/]+)\.git(#.*)?$/)[1]);
732+
return Promise.resolve({
733+
name: installPackage.match(/([^/]+)\.git(#.*)?$/)[1],
734+
});
697735
} else if (installPackage.match(/.+@/)) {
698736
// Do not match @scope/ when stripping off @version or @tag
699-
return Promise.resolve(
700-
installPackage.charAt(0) + installPackage.substr(1).split('@')[0]
701-
);
737+
return Promise.resolve({
738+
name: installPackage.charAt(0) + installPackage.substr(1).split('@')[0],
739+
version: installPackage.split('@')[1],
740+
});
702741
} else if (installPackage.match(/^file:/)) {
703742
const installPackagePath = installPackage.match(/^file:(.*)?$/)[1];
704-
const installPackageJson = require(path.join(
743+
const { name, version } = require(path.join(
705744
installPackagePath,
706745
'package.json'
707746
));
708-
return Promise.resolve(installPackageJson.name);
747+
return Promise.resolve({ name, version });
709748
}
710-
return Promise.resolve(installPackage);
749+
return Promise.resolve({ name: installPackage });
711750
}
712751

713752
function checkNpmVersion() {

packages/react-scripts/fixtures/kitchensink/template/.gitignore renamed to packages/react-scripts/fixtures/kitchensink/template/gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ build
1111

1212
# misc
1313
.DS_Store
14+
.env
1415
npm-debug.log

packages/react-scripts/scripts/init.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ module.exports = function(
8484
) {
8585
const appPackage = require(path.join(appPath, 'package.json'));
8686
const useYarn = fs.existsSync(path.join(appPath, 'yarn.lock'));
87+
88+
if (!templateName) {
89+
console.log('');
90+
console.error(
91+
`A template was not provided. This is likely because you're using an outdated version of ${chalk.cyan(
92+
'create-react-app'
93+
)}.`
94+
);
95+
console.error(
96+
`Please note that global installs of ${chalk.cyan(
97+
'create-react-app'
98+
)} are no longer supported.`
99+
);
100+
return;
101+
}
102+
87103
const templatePath = path.join(appPath, 'node_modules', templateName);
88104

89105
// Copy over some of the devDependencies

0 commit comments

Comments
 (0)