init.js 3.06 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');
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
38
39
40
41
  // Copy the files for the user
  function copySync(src, dest) {
    return fs.writeFileSync(dest, fs.readFileSync(src));
  }
  fs.mkdirSync(path.join(hostPath, 'src'));
42
  fs.readdirSync(path.join(selfPath, 'template/src')).forEach(function(filename) {
43
    copySync(
44
      path.join(selfPath, 'template/src', filename),
45
46
47
      path.join(hostPath, 'src', filename)
    );
  });
48
49
50
51
52
53
54
  fs.readdirSync(path.join(selfPath, 'template')).forEach(function(filename) {
    if (fs.lstatSync(path.join(selfPath, 'template', filename)).isDirectory()) {
      return
    }
    copySync(
      path.join(selfPath, 'template', filename),
      path.join(hostPath, filename)
Dan Abramov's avatar
Dan Abramov committed
55
    );
56
  });
Christopher Chedeau's avatar
.    
Christopher Chedeau committed
57

58
  // Run another npm install for react and react-dom
Kevin Lacker's avatar
Kevin Lacker committed
59
  console.log('Installing react and react-dom from npm...');
60
61
62
63
64
65
66
67
68
69
70
71
  // 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
72
73
74
75
76
77
78
79
    // Make sure to display the right way to cd
    var cdpath;
    if (path.join(process.cwd(), appName) == hostPath) {
      cdpath = appName;
    } else {
      cdpath = hostPath;
    }

80
81
82
83
    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
84
    console.log('  * npm run build: Bundles the app into static files for production.');
eanplatter's avatar
eanplatter committed
85
    console.log('  * npm run eject: Removes this tool. If you do this, you can’t go back!');
86
87
    console.log();
    console.log('We suggest that you begin by typing:');
Kevin Lacker's avatar
Kevin Lacker committed
88
    console.log('  cd', cdpath);
89
90
91
92
    console.log('  npm start');
    console.log();
    console.log('Happy hacking!');
  });
93
};