init.js 4.02 KB
Newer Older
Christopher Chedeau's avatar
Christopher Chedeau committed
1
2
3
4
5
6
7
8
9
/**
 * 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.
 */

10
var fs = require('fs-extra');
Dan Abramov's avatar
Dan Abramov committed
11
var path = require('path');
12
var spawn = require('cross-spawn');
13
14
var pathExists = require('path-exists');
var chalk = require('chalk');
15

16
17
module.exports = function(appPath, appName, verbose, originalDirectory) {
  var ownPath = path.join(appPath, 'node_modules', 'react-scripts');
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
18

19
20
  var appPackage = require(path.join(appPath, 'package.json'));
  var ownPackage = require(path.join(ownPath, 'package.json'));
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
21

Dan Abramov's avatar
Dan Abramov committed
22
  // Copy over some of the devDependencies
23
  appPackage.dependencies = appPackage.dependencies || {};
Christoph Pojer's avatar
Christoph Pojer committed
24
  appPackage.devDependencies = appPackage.devDependencies || {};
Dan Abramov's avatar
Dan Abramov committed
25
  ['react', 'react-dom'].forEach(function (key) {
26
    appPackage.dependencies[key] = ownPackage.devDependencies[key];
Dan Abramov's avatar
Dan Abramov committed
27
  });
Christoph Pojer's avatar
Christoph Pojer committed
28
29
30
  ['react-test-renderer'].forEach(function (key) {
    appPackage.devDependencies[key] = ownPackage.devDependencies[key];
  });
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
31
32

  // Setup the script rules
33
  appPackage.scripts = {};
Christoph Pojer's avatar
Christoph Pojer committed
34
  ['start', 'build', 'eject', 'test'].forEach(function(command) {
35
    appPackage.scripts[command] = 'react-scripts ' + command;
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
36
37
  });

38
  // explicitly specify ESLint config path for editor plugins
39
  appPackage.eslintConfig = {
40
41
42
    extends: './node_modules/react-scripts/config/eslint.js',
  };

Dan Abramov's avatar
Dan Abramov committed
43
  fs.writeFileSync(
44
45
    path.join(appPath, 'package.json'),
    JSON.stringify(appPackage, null, 2)
Dan Abramov's avatar
Dan Abramov committed
46
  );
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
47

48
49
50
51
52
  var readmeExists = pathExists.sync(path.join(appPath, 'README.md'));
  if (readmeExists) {
    fs.renameSync(path.join(appPath, 'README.md'), path.join(appPath, 'README.old.md'));
  }

53
  // Copy the files for the user
54
  fs.copySync(path.join(ownPath, 'template'), appPath);
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
55

56
57
  // Rename gitignore after the fact to prevent npm from renaming it to .npmignore
  // See: https://github.com/npm/npm/issues/1862
58
59
60
61
62
63
64
65
66
67
68
69
  fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), [], function (err) {
    if (err) {
      // Append if there's already a `.gitignore` file there
      if (err.code === 'EEXIST') {
        var data = fs.readFileSync(path.join(appPath, 'gitignore'));
        fs.appendFileSync(path.join(appPath, '.gitignore'), data);
        fs.unlinkSync(path.join(appPath, 'gitignore'));
      } else {
        throw err;
      }
    }
  });
70

71
  // Run another npm install for react and react-dom
Kevin Lacker's avatar
Kevin Lacker committed
72
  console.log('Installing react and react-dom from npm...');
73
  console.log();
74
75
76
77
78
79
80
81
82
83
84
85
  // TODO: having to do two npm installs is bad, can we avoid it?
  var args = [
    'install',
    verbose && '--verbose'
  ].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;
    }

Kevin Lacker's avatar
Kevin Lacker committed
86
87
88
    // Display the most elegant way to cd.
    // This needs to handle an undefined originalDirectory for
    // backward compatibility with old global-cli's.
Kevin Lacker's avatar
Kevin Lacker committed
89
    var cdpath;
Kevin Lacker's avatar
Kevin Lacker committed
90
    if (originalDirectory &&
91
        path.join(originalDirectory, appName) === appPath) {
Kevin Lacker's avatar
Kevin Lacker committed
92
93
      cdpath = appName;
    } else {
94
      cdpath = appPath;
Kevin Lacker's avatar
Kevin Lacker committed
95
96
    }

97
    console.log();
98
    console.log('Success! Created ' + appName + ' at ' + appPath + '.');
99
    console.log('Inside that directory, you can run several commands:');
100
    console.log();
101
    console.log('  * npm start: Starts the development server.');
Kevin Lacker's avatar
Kevin Lacker committed
102
    console.log('  * npm run build: Bundles the app into static files for production.');
eanplatter's avatar
eanplatter committed
103
    console.log('  * npm run eject: Removes this tool. If you do this, you can’t go back!');
104
105
    console.log();
    console.log('We suggest that you begin by typing:');
106
    console.log();
Kevin Lacker's avatar
Kevin Lacker committed
107
    console.log('  cd', cdpath);
108
    console.log('  npm start');
109
110
111
112
    if (readmeExists) {
      console.log();
      console.log(chalk.yellow('You had a `README.md` file, we renamed it to `README.old.md`'));
    }
113
114
115
    console.log();
    console.log('Happy hacking!');
  });
116
};