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

Dan Abramov's avatar
Dan Abramov committed
17
18
  var hostPackage = require(path.join(hostPath, 'package.json'));
  var selfPackage = require(path.join(selfPath, '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
  hostPackage.dependencies = hostPackage.dependencies || {};
Dan Abramov's avatar
Dan Abramov committed
22
  ['react', 'react-dom'].forEach(function (key) {
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
23
    hostPackage.dependencies[key] = selfPackage.devDependencies[key];
Dan Abramov's avatar
Dan Abramov committed
24
  });
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
25
26
27

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

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

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

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

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

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
    // Make sure to display the right way to cd
    var cdpath;
65
    if (path.join(process.cwd(), appName) === hostPath) {
Kevin Lacker's avatar
Kevin Lacker committed
66
67
68
69
70
      cdpath = appName;
    } else {
      cdpath = hostPath;
    }

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