Skip to content

Commit b023fdd

Browse files
authored
feat: exit on outdated create-react-app version (facebook#9359)
1 parent 4bfcc2b commit b023fdd

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

packages/create-react-app/createReactApp.js

+72-14
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
// /!\ DO NOT MODIFY THIS FILE /!\
1010
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1111
//
12-
// create-react-app is installed globally on people's computers. This means
13-
// that it is extremely difficult to have them upgrade the version and
14-
// because there's only one global version installed, it is very prone to
15-
// breaking changes.
16-
//
1712
// The only job of create-react-app is to init the repository and then
1813
// forward all the commands to the local version of create-react-app.
1914
//
@@ -26,14 +21,15 @@
2621
// tell people to update their global version of create-react-app.
2722
//
2823
// Also be careful with new language features.
29-
// This file must work on Node 6+.
24+
// This file must work on Node 10+.
3025
//
3126
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3227
// /!\ DO NOT MODIFY THIS FILE /!\
3328
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3429

3530
'use strict';
3631

32+
const https = require('https');
3733
const chalk = require('chalk');
3834
const commander = require('commander');
3935
const dns = require('dns');
@@ -179,14 +175,53 @@ if (typeof projectName === 'undefined') {
179175
process.exit(1);
180176
}
181177

182-
createApp(
183-
projectName,
184-
program.verbose,
185-
program.scriptsVersion,
186-
program.template,
187-
program.useNpm,
188-
program.usePnp
189-
);
178+
// We first check the registry directly via the API, and if that fails, we try
179+
// the slower `npm view [package] version` command.
180+
//
181+
// This is important for users in environments where direct access to npm is
182+
// blocked by a firewall, and packages are provided exclusively via a private
183+
// registry.
184+
checkForLatestVersion()
185+
.catch(() => {
186+
try {
187+
return execSync('npm view create-react-app version').toString().trim();
188+
} catch (e) {
189+
return null;
190+
}
191+
})
192+
.then(latest => {
193+
if (latest && semver.lt(packageJson.version, latest)) {
194+
console.log();
195+
console.error(
196+
chalk.yellow(
197+
`You are running \`create-react-app\` ${packageJson.version}, which is behind the latest release (${latest}).\n\n` +
198+
'We no longer support global installation of Create React App.'
199+
)
200+
);
201+
console.log();
202+
console.log(
203+
'Please remove any global installs with one of the following commands:\n' +
204+
'- npm uninstall -g create-react-app\n' +
205+
'- yarn global remove create-react-app'
206+
);
207+
console.log();
208+
console.log(
209+
'The latest instructions for creating a new app can be found here:\n' +
210+
'https://create-react-app.dev/docs/getting-started/'
211+
);
212+
console.log();
213+
process.exit(1);
214+
} else {
215+
createApp(
216+
projectName,
217+
program.verbose,
218+
program.scriptsVersion,
219+
program.template,
220+
program.useNpm,
221+
program.usePnp
222+
);
223+
}
224+
});
190225

191226
function createApp(name, verbose, version, template, useNpm, usePnp) {
192227
const unsupportedNodeVersion = !semver.satisfies(process.version, '>=10');
@@ -1075,3 +1110,26 @@ function executeNodeScript({ cwd, args }, data, source) {
10751110
});
10761111
});
10771112
}
1113+
1114+
function checkForLatestVersion() {
1115+
return new Promise((resolve, reject) => {
1116+
https
1117+
.get(
1118+
'https://registry.npmjs.org/-/package/create-react-app/dist-tags',
1119+
res => {
1120+
if (res.statusCode === 200) {
1121+
let body = '';
1122+
res.on('data', data => (body += data));
1123+
res.on('end', () => {
1124+
resolve(JSON.parse(body).latest);
1125+
});
1126+
} else {
1127+
reject();
1128+
}
1129+
}
1130+
)
1131+
.on('error', () => {
1132+
reject();
1133+
});
1134+
});
1135+
}

0 commit comments

Comments
 (0)