eject.js 4.29 KB
Newer Older
eanplatter's avatar
eanplatter 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.
 */

Dan Abramov's avatar
Dan Abramov committed
10
var createJestConfig = require('./utils/createJestConfig');
eanplatter's avatar
eanplatter committed
11
12
var fs = require('fs');
var path = require('path');
Christoph Pojer's avatar
Christoph Pojer committed
13
var prompt = require('./utils/prompt');
eanplatter's avatar
eanplatter committed
14
var rimrafSync = require('rimraf').sync;
Dan Abramov's avatar
Dan Abramov committed
15
var spawnSync = require('cross-spawn').sync;
eanplatter's avatar
eanplatter committed
16

17
18
19
20
prompt(
  'Are you sure you want to eject? This action is permanent.',
  false
).then(shouldEject => {
Dan Abramov's avatar
Dan Abramov committed
21
  if (!shouldEject) {
Tyler McGinnis's avatar
Tyler McGinnis committed
22
    console.log('Close one! Eject aborted.');
eanplatter's avatar
eanplatter committed
23
24
25
26
27
    process.exit(1);
  }

  console.log('Ejecting...');
  console.log();
Dan Abramov's avatar
Dan Abramov committed
28

29
30
  var ownPath = path.join(__dirname, '..');
  var appPath = path.join(ownPath, '..', '..');
eanplatter's avatar
eanplatter committed
31
  var files = [
32
    '.babelrc',
33
    '.eslintrc',
34
    path.join('config', 'paths.js'),
35
    path.join('config', 'polyfills.js'),
36
37
    path.join('config', 'webpack.config.dev.js'),
    path.join('config', 'webpack.config.prod.js'),
Christoph Pojer's avatar
Christoph Pojer committed
38
39
    path.join('config', 'jest', 'CSSStub.js'),
    path.join('config', 'jest', 'FileStub.js'),
40
    path.join('scripts', 'build.js'),
41
    path.join('scripts', 'start.js'),
42
    path.join('scripts', 'test.js'),
43
    path.join('scripts', 'utils', 'checkRequiredFiles.js'),
44
    path.join('scripts', 'utils', 'chrome.applescript'),
45
46
    path.join('scripts', 'utils', 'getClientEnvironment.js'),
    path.join('scripts', 'utils', 'InterpolateHtmlPlugin.js'),
47
48
    path.join('scripts', 'utils', 'prompt.js'),
    path.join('scripts', 'utils', 'WatchMissingNodeModulesPlugin.js')
eanplatter's avatar
eanplatter committed
49
50
  ];

51
  // Ensure that the app folder is clean and we won't override any files
eanplatter's avatar
eanplatter committed
52
  files.forEach(function(file) {
53
    if (fs.existsSync(path.join(appPath, file))) {
Dan Abramov's avatar
Dan Abramov committed
54
55
56
57
58
59
60
61
      console.error(
        '`' + file + '` already exists in your app folder. We cannot ' +
        'continue as you would lose all the changes in that file or directory. ' +
        'Please delete it (maybe make a copy for backup) and run this ' +
        'command again.'
      );
      process.exit(1);
    }
eanplatter's avatar
eanplatter committed
62
63
  });

64
  // Copy the files over
65
  fs.mkdirSync(path.join(appPath, 'config'));
Christoph Pojer's avatar
Christoph Pojer committed
66
  fs.mkdirSync(path.join(appPath, 'config', 'jest'));
67
  fs.mkdirSync(path.join(appPath, 'scripts'));
68
  fs.mkdirSync(path.join(appPath, 'scripts', 'utils'));
69

eanplatter's avatar
eanplatter committed
70
  files.forEach(function(file) {
71
    console.log('Copying ' + file + ' to ' + appPath);
72
    var content = fs
73
      .readFileSync(path.join(ownPath, file), 'utf8')
74
75
76
77
      // Remove dead code from .js files on eject
      .replace(/\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/mg, '')
      // Remove dead code from .applescript files on eject
      .replace(/-- @remove-on-eject-begin([\s\S]*?)-- @remove-on-eject-end/mg, '')
78
      .trim() + '\n';
79
    fs.writeFileSync(path.join(appPath, file), content);
eanplatter's avatar
eanplatter committed
80
81
82
  });
  console.log();

83
84
  var ownPackage = require(path.join(ownPath, 'package.json'));
  var appPackage = require(path.join(appPath, 'package.json'));
eanplatter's avatar
eanplatter committed
85

86
  console.log('Removing dependency: react-scripts');
87
  delete appPackage.devDependencies['react-scripts'];
eanplatter's avatar
eanplatter committed
88

89
  Object.keys(ownPackage.dependencies).forEach(function (key) {
Dan Abramov's avatar
Dan Abramov committed
90
    // For some reason optionalDependencies end up in dependencies after install
91
    if (ownPackage.optionalDependencies[key]) {
Dan Abramov's avatar
Dan Abramov committed
92
93
94
      return;
    }
    console.log('Adding dependency: ' + key);
95
    appPackage.devDependencies[key] = ownPackage.dependencies[key];
eanplatter's avatar
eanplatter committed
96
97
98
  });

  console.log('Updating scripts');
Dan Abramov's avatar
Dan Abramov committed
99
  delete appPackage.scripts['eject'];
100
  Object.keys(appPackage.scripts).forEach(function (key) {
Dan Abramov's avatar
Dan Abramov committed
101
102
    appPackage.scripts[key] = appPackage.scripts[key]
      .replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
eanplatter's avatar
eanplatter committed
103
104
  });

Dan Abramov's avatar
Dan Abramov committed
105
  // Add Jest config
Christoph Pojer's avatar
Christoph Pojer committed
106
  appPackage.jest = createJestConfig(
107
108
109
    filePath => path.join('<rootDir>', filePath),
    null,
    true
Christoph Pojer's avatar
Christoph Pojer committed
110
111
  );

eanplatter's avatar
eanplatter committed
112
113
  console.log('Writing package.json');
  fs.writeFileSync(
114
115
    path.join(appPath, 'package.json'),
    JSON.stringify(appPackage, null, 2)
eanplatter's avatar
eanplatter committed
116
117
118
119
  );
  console.log();

  console.log('Running npm install...');
120
  rimrafSync(ownPath);
eanplatter's avatar
eanplatter committed
121
  spawnSync('npm', ['install'], {stdio: 'inherit'});
Dan Abramov's avatar
Dan Abramov committed
122
  console.log('Ejected successfully!');
eanplatter's avatar
eanplatter committed
123
124
  console.log();

Dan Abramov's avatar
Dan Abramov committed
125
126
127
  console.log('Please consider sharing why you ejected in this survey:');
  console.log('  http://goo.gl/forms/Bi6CZjk1EqsdelXk1');
  console.log();
eanplatter's avatar
eanplatter committed
128
});