|  | 
| 9 | 9 | //   /!\ DO NOT MODIFY THIS FILE /!\ | 
| 10 | 10 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
| 11 | 11 | // | 
| 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 |  | -// | 
| 17 | 12 | // The only job of create-react-app is to init the repository and then | 
| 18 | 13 | // forward all the commands to the local version of create-react-app. | 
| 19 | 14 | // | 
|  | 
| 26 | 21 | // tell people to update their global version of create-react-app. | 
| 27 | 22 | // | 
| 28 | 23 | // Also be careful with new language features. | 
| 29 |  | -// This file must work on Node 6+. | 
|  | 24 | +// This file must work on Node 10+. | 
| 30 | 25 | // | 
| 31 | 26 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
| 32 | 27 | //   /!\ DO NOT MODIFY THIS FILE /!\ | 
| 33 | 28 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
| 34 | 29 | 
 | 
| 35 | 30 | 'use strict'; | 
| 36 | 31 | 
 | 
|  | 32 | +const https = require('https'); | 
| 37 | 33 | const chalk = require('chalk'); | 
| 38 | 34 | const commander = require('commander'); | 
| 39 | 35 | const dns = require('dns'); | 
| @@ -179,14 +175,53 @@ if (typeof projectName === 'undefined') { | 
| 179 | 175 |   process.exit(1); | 
| 180 | 176 | } | 
| 181 | 177 | 
 | 
| 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 | +  }); | 
| 190 | 225 | 
 | 
| 191 | 226 | function createApp(name, verbose, version, template, useNpm, usePnp) { | 
| 192 | 227 |   const unsupportedNodeVersion = !semver.satisfies(process.version, '>=10'); | 
| @@ -1075,3 +1110,26 @@ function executeNodeScript({ cwd, args }, data, source) { | 
| 1075 | 1110 |     }); | 
| 1076 | 1111 |   }); | 
| 1077 | 1112 | } | 
|  | 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