init.js 3.83 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

33
  // explicitly specify ESLint config path for editor plugins
34
  appPackage.eslintConfig = {
35
36
37
    extends: './node_modules/react-scripts/config/eslint.js',
  };

Dan Abramov's avatar
Dan Abramov committed
38
  fs.writeFileSync(
39
40
    path.join(appPath, 'package.json'),
    JSON.stringify(appPackage, null, 2)
Dan Abramov's avatar
Dan Abramov committed
41
  );
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
42

43
44
45
46
47
  var readmeExists = pathExists.sync(path.join(appPath, 'README.md'));
  if (readmeExists) {
    fs.renameSync(path.join(appPath, 'README.md'), path.join(appPath, 'README.old.md'));
  }

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

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

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

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