init.js 3.03 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
15
module.exports = function(appPath, appName, verbose, originalDirectory) {
  var ownPath = path.join(appPath, 'node_modules', 'react-scripts');
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
16

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

Dan Abramov's avatar
Dan Abramov committed
20
  // Copy over some of the devDependencies
21
  appPackage.dependencies = appPackage.dependencies || {};
Dan Abramov's avatar
Dan Abramov committed
22
  ['react', 'react-dom'].forEach(function (key) {
23
    appPackage.dependencies[key] = ownPackage.devDependencies[key];
Dan Abramov's avatar
Dan Abramov committed
24
  });
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
25
26

  // Setup the script rules
27
  appPackage.scripts = {};
eanplatter's avatar
eanplatter committed
28
  ['start', 'build', 'eject'].forEach(function(command) {
29
    appPackage.scripts[command] = 'react-scripts ' + command;
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
30
31
  });

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

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

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

45
46
  // Rename gitignore after the fact to prevent npm from renaming it to .npmignore
  // See: https://github.com/npm/npm/issues/1862
47
  fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), []);
48

49
  // Run another npm install for react and react-dom
Kevin Lacker's avatar
Kevin Lacker committed
50
  console.log('Installing react and react-dom from npm...');
51
52
53
54
55
56
57
58
59
60
61
62
  // 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
63
64
65
    // 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
66
    var cdpath;
Kevin Lacker's avatar
Kevin Lacker committed
67
    if (originalDirectory &&
68
        path.join(originalDirectory, appName) === appPath) {
Kevin Lacker's avatar
Kevin Lacker committed
69
70
      cdpath = appName;
    } else {
71
      cdpath = appPath;
Kevin Lacker's avatar
Kevin Lacker committed
72
73
    }

74
    console.log('Success! Created ' + appName + ' at ' + appPath + '.');
75
    console.log('Inside that directory, you can run several commands:');
76
    console.log();
77
    console.log('  * npm start: Starts the development server.');
Kevin Lacker's avatar
Kevin Lacker committed
78
    console.log('  * npm run build: Bundles the app into static files for production.');
eanplatter's avatar
eanplatter committed
79
    console.log('  * npm run eject: Removes this tool. If you do this, you can’t go back!');
80
81
    console.log();
    console.log('We suggest that you begin by typing:');
82
    console.log();
Kevin Lacker's avatar
Kevin Lacker committed
83
    console.log('  cd', cdpath);
84
85
86
87
    console.log('  npm start');
    console.log();
    console.log('Happy hacking!');
  });
88
};