init.js 3.96 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
  var appPackage = require(path.join(appPath, 'package.json'));
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
20

Dan Abramov's avatar
Dan Abramov committed
21
  // Copy over some of the devDependencies
22
  appPackage.dependencies = appPackage.dependencies || {};
Christoph Pojer's avatar
Christoph Pojer committed
23
  appPackage.devDependencies = appPackage.devDependencies || {};
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
24
25

  // Setup the script rules
Dan Abramov's avatar
Dan Abramov committed
26
27
28
  appPackage.scripts = {
    'start': 'react-scripts start',
    'build': 'react-scripts build',
29
    'test': 'react-scripts test --env=jsdom',
Dan Abramov's avatar
Dan Abramov committed
30
31
    'eject': 'react-scripts eject'
  };
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
32

Dan Abramov's avatar
Dan Abramov committed
33
  fs.writeFileSync(
34
35
    path.join(appPath, 'package.json'),
    JSON.stringify(appPackage, null, 2)
Dan Abramov's avatar
Dan Abramov committed
36
  );
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
37

38
39
40
41
42
  var readmeExists = pathExists.sync(path.join(appPath, 'README.md'));
  if (readmeExists) {
    fs.renameSync(path.join(appPath, 'README.md'), path.join(appPath, 'README.old.md'));
  }

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

46
47
  // Rename gitignore after the fact to prevent npm from renaming it to .npmignore
  // See: https://github.com/npm/npm/issues/1862
48
49
50
51
52
53
54
55
56
57
58
59
  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;
      }
    }
  });
60

61
  // Run another npm install for react and react-dom
Kevin Lacker's avatar
Kevin Lacker committed
62
  console.log('Installing react and react-dom from npm...');
63
  console.log();
64
65
66
  // TODO: having to do two npm installs is bad, can we avoid it?
  var args = [
    'install',
67
68
69
    'react',
    'react-dom',
    '--save',
70
71
72
73
74
75
76
77
78
    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
79
80
81
    // 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
82
    var cdpath;
Kevin Lacker's avatar
Kevin Lacker committed
83
    if (originalDirectory &&
84
        path.join(originalDirectory, appName) === appPath) {
Kevin Lacker's avatar
Kevin Lacker committed
85
86
      cdpath = appName;
    } else {
87
      cdpath = appPath;
Kevin Lacker's avatar
Kevin Lacker committed
88
89
    }

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