init.js 2.5 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
  });

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

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

40
  // Run another npm install for react and react-dom
Kevin Lacker's avatar
Kevin Lacker committed
41
  console.log('Installing react and react-dom from npm...');
42
43
44
45
46
47
48
49
50
51
52
53
  // 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
54
55
    // Make sure to display the right way to cd
    var cdpath;
56
    if (path.join(process.cwd(), appName) === hostPath) {
Kevin Lacker's avatar
Kevin Lacker committed
57
58
59
60
61
      cdpath = appName;
    } else {
      cdpath = hostPath;
    }

62
63
64
65
    console.log('Success! Created ' + appName + ' at ' + hostPath + '.');
    console.log();
    console.log('Inside that directory, you can run several commands:');
    console.log('  * npm start: Starts the development server.');
Kevin Lacker's avatar
Kevin Lacker committed
66
    console.log('  * npm run build: Bundles the app into static files for production.');
eanplatter's avatar
eanplatter committed
67
    console.log('  * npm run eject: Removes this tool. If you do this, you can’t go back!');
68
69
    console.log();
    console.log('We suggest that you begin by typing:');
Kevin Lacker's avatar
Kevin Lacker committed
70
    console.log('  cd', cdpath);
71
72
73
74
    console.log('  npm start');
    console.log();
    console.log('Happy hacking!');
  });
75
};