index.js 4.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env node

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

Christopher Chedeau's avatar
Christopher Chedeau committed
12
13
14
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
16
17
18
19
20
21
22
23
//
// create-react-app is installed globally on people's computers. This means
// that it is extremely difficult to have them upgrade the version and
// because there's only one global version installed, it is very prone to
// breaking changes.
//
// The only job of create-react-app is to init the repository and then
// forward all the commands to the local version of create-react-app.
//
Christopher Chedeau's avatar
Christopher Chedeau committed
24
// If you need to add a new command, please add it to the scripts/ folder.
25
26
//
// The only reason to modify this file is to add more warnings and
Christopher Chedeau's avatar
Christopher Chedeau committed
27
// troubleshooting information for the `create-react-app` command.
28
29
30
31
//
// Do not make breaking changes! We absolutely don't want to have to
// tell people to update their global version of create-react-app.
//
Christopher Chedeau's avatar
Christopher Chedeau committed
32
33
34
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35
36
37
38
39

'use strict';

var fs = require('fs');
var path = require('path');
40
var spawn = require('cross-spawn');
41
42
var chalk = require('chalk');
var semver = require('semver');
Christopher Chedeau's avatar
Christopher Chedeau committed
43
44
var argv = require('minimist')(process.argv.slice(2));

45
/**
Christopher Chedeau's avatar
Christopher Chedeau committed
46
47
 * Arguments:
 *   --version - to print current version
48
 *   --verbose - to print logs while init
Christopher Chedeau's avatar
Christopher Chedeau committed
49
50
51
 *   --scripts-version <alternative package>
 *     Example of valid values:
 *     - a specific npm version: "0.22.0-rc1"
52
53
 *     - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"
 *     - a package prepared with `npm pack`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"
54
55
56
 */
var commands = argv._;
if (commands.length === 0) {
Max Stoiber's avatar
Max Stoiber committed
57
58
59
60
  if (argv.version) {
    console.log('create-react-app version: ' + require('./package.json').version);
    process.exit();
  }
61
  console.error(
Kevin Lacker's avatar
Kevin Lacker committed
62
    'Usage: create-react-app <project-directory> [--verbose]'
63
64
65
66
67
68
69
70
  );
  process.exit(1);
}

createApp(commands[0], argv.verbose, argv['scripts-version']);

function createApp(name, verbose, version) {
  if (fs.existsSync(name)) {
Kevin Lacker's avatar
Kevin Lacker committed
71
    console.log('The directory `' + name + '` already exists. Aborting.');
Christopher Chedeau's avatar
Christopher Chedeau committed
72
    process.exit(1);
73
74
75
76
77
78
  }

  var root = path.resolve(name);
  var appName = path.basename(root);

  console.log(
79
    'Creating a new React app in ' + root + '.'
80
  );
81
  console.log();
82
83
84
85
86
87
88
89
90

  fs.mkdirSync(root);

  var packageJson = {
    name: appName,
    version: '0.0.1',
    private: true,
  };
  fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
Kevin Lacker's avatar
Kevin Lacker committed
91
  var originalDirectory = process.cwd();
92
93
  process.chdir(root);

Dan Abramov's avatar
Dan Abramov committed
94
  console.log('Installing packages. This might take a couple minutes.');
Kevin Lacker's avatar
Kevin Lacker committed
95
  console.log('Installing react-scripts from npm...');
96

Kevin Lacker's avatar
Kevin Lacker committed
97
  run(root, appName, version, verbose, originalDirectory);
98
99
}

Kevin Lacker's avatar
Kevin Lacker committed
100
function run(root, appName, version, verbose, originalDirectory) {
101
102
103
  var args = [
    'install',
    verbose && '--verbose',
104
    '--save-dev',
105
106
107
108
109
110
111
112
113
114
    '--save-exact',
    getInstallPackage(version),
  ].filter(function(e) { return e; });
  var proc = spawn('npm', args, {stdio: 'inherit'});
  proc.on('close', function (code) {
    if (code !== 0) {
      console.error('`npm ' + args.join(' ') + '` failed');
      return;
    }

115
116
    checkNodeVersion();

117
118
119
    var scriptsPath = path.resolve(
      process.cwd(),
      'node_modules',
120
      'react-scripts',
121
      'scripts',
122
123
124
      'init.js'
    );
    var init = require(scriptsPath);
Kevin Lacker's avatar
Kevin Lacker committed
125
    init(root, appName, verbose, originalDirectory);
126
127
128
129
  });
}

function getInstallPackage(version) {
130
  var packageToInstall = 'react-scripts';
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  var validSemver = semver.valid(version);
  if (validSemver) {
    packageToInstall += '@' + validSemver;
  } else if (version) {
    // for tar.gz or alternative paths
    packageToInstall = version;
  }
  return packageToInstall;
}

function checkNodeVersion() {
  var packageJsonPath = path.resolve(
    process.cwd(),
    'node_modules',
145
    'react-scripts',
146
147
148
149
150
151
    'package.json'
  );
  var packageJson = require(packageJsonPath);
  if (!packageJson.engines || !packageJson.engines.node) {
    return;
  }
Christopher Chedeau's avatar
Christopher Chedeau committed
152

153
154
155
  if (!semver.satisfies(process.version, packageJson.engines.node)) {
    console.error(
      chalk.red(
Christopher Chedeau's avatar
Christopher Chedeau committed
156
157
        'You are currently running Node %s but create-react-app requires %s.' +
        ' Please use a supported version of Node.\n'
158
159
160
161
      ),
      process.version,
      packageJson.engines.node
    );
162
    process.exit(1);
163
164
  }
}