eject.js 5.62 KB
Newer Older
Daniel Grant's avatar
Daniel Grant committed
1
// @remove-file-on-eject
eanplatter's avatar
eanplatter committed
2
3
4
5
6
7
8
9
10
/**
 * 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.
 */

11
var fs = require('fs-extra');
eanplatter's avatar
eanplatter committed
12
var path = require('path');
Dan Abramov's avatar
Dan Abramov committed
13
var spawnSync = require('cross-spawn').sync;
14
var chalk = require('chalk');
Daniel Grant's avatar
Daniel Grant committed
15
16
17
18
var prompt = require('react-dev-utils/prompt');
var paths = require('../config/paths');
var createJestConfig = require('./utils/createJestConfig');

19
20
var green = chalk.green;
var cyan = chalk.cyan;
eanplatter's avatar
eanplatter committed
21

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

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

33
34
  var ownPath = paths.ownPath;
  var appPath = paths.appPath;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

  function verifyAbsent(file) {
    if (fs.existsSync(path.join(appPath, file))) {
      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 move or delete it (maybe make a copy for backup) and run this ' +
        'command again.'
      );
      process.exit(1);
    }
  }

  var folders = [
    'config',
Daniel Grant's avatar
Daniel Grant committed
50
51
52
    'config/jest',
    'scripts',
    'scripts/utils',
53
54
  ];

Daniel Grant's avatar
Daniel Grant committed
55
56
57
58
59
60
61
62
63
64
  // Make shallow array of files paths
  var files = folders.reduce(function (files, folder) {
    return files.concat(
      fs.readdirSync(path.join(ownPath, folder))
        // set full path
        .map(file => path.join(ownPath, folder, file))
        // omit dirs from file list
        .filter(file => fs.lstatSync(file).isFile())
    );
  }, []);
eanplatter's avatar
eanplatter committed
65

66
  // Ensure that the app folder is clean and we won't override any files
67
68
  folders.forEach(verifyAbsent);
  files.forEach(verifyAbsent);
eanplatter's avatar
eanplatter committed
69

Daniel Grant's avatar
Daniel Grant committed
70
71
72
  console.log();
  console.log(cyan('Copying files into ' + appPath));

73
74
75
  folders.forEach(function(folder) {
    fs.mkdirSync(path.join(appPath, folder))
  });
76

eanplatter's avatar
eanplatter committed
77
  files.forEach(function(file) {
Daniel Grant's avatar
Daniel Grant committed
78
79
80
81
82
83
84
    var content = fs.readFileSync(file, 'utf8');

    // Skip flagged files
    if (content.match(/\/\/ @remove-file-on-eject/)) {
      return;
    }
    content = content
85
86
87
88
      // 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, '')
89
      .trim() + '\n';
Daniel Grant's avatar
Daniel Grant committed
90
91
    console.log('  Adding ' + cyan(file.replace(ownPath, '')) + ' to the project');
    fs.writeFileSync(file.replace(ownPath, appPath), content);
eanplatter's avatar
eanplatter committed
92
93
94
  });
  console.log();

95
96
  var ownPackage = require(path.join(ownPath, 'package.json'));
  var appPackage = require(path.join(appPath, 'package.json'));
97
98
  var babelConfig = JSON.parse(fs.readFileSync(path.join(ownPath, '.babelrc'), 'utf8'));
  var eslintConfig = JSON.parse(fs.readFileSync(path.join(ownPath, '.eslintrc'), 'utf8'));
eanplatter's avatar
eanplatter committed
99

Dan Abramov's avatar
Dan Abramov committed
100
  console.log(cyan('Updating the dependencies'));
101
  var ownPackageName = ownPackage.name;
102
103
104
105
106
107
108
109
  if (appPackage.devDependencies[ownPackageName]) {
    console.log('  Removing ' + cyan(ownPackageName) + ' from devDependencies');
    delete appPackage.devDependencies[ownPackageName];
  }
  if (appPackage.dependencies[ownPackageName]) {
    console.log('  Removing ' + cyan(ownPackageName) + ' from dependencies');
    delete appPackage.dependencies[ownPackageName];
  }
eanplatter's avatar
eanplatter committed
110

111
  Object.keys(ownPackage.dependencies).forEach(function (key) {
Dan Abramov's avatar
Dan Abramov committed
112
    // For some reason optionalDependencies end up in dependencies after install
113
    if (ownPackage.optionalDependencies[key]) {
Dan Abramov's avatar
Dan Abramov committed
114
115
      return;
    }
Dan Abramov's avatar
Dan Abramov committed
116
    console.log('  Adding ' + cyan(key) + ' to devDependencies');
117
    appPackage.devDependencies[key] = ownPackage.dependencies[key];
eanplatter's avatar
eanplatter committed
118
  });
119
  console.log();
Dan Abramov's avatar
Dan Abramov committed
120
  console.log(cyan('Updating the scripts'));
Dan Abramov's avatar
Dan Abramov committed
121
  delete appPackage.scripts['eject'];
122
  Object.keys(appPackage.scripts).forEach(function (key) {
Dan Abramov's avatar
Dan Abramov committed
123
    appPackage.scripts[key] = appPackage.scripts[key]
124
      .replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
125
126
    console.log(
      '  Replacing ' +
Dan Abramov's avatar
Dan Abramov committed
127
      cyan('"react-scripts ' + key + '"') +
128
      ' with ' +
Dan Abramov's avatar
Dan Abramov committed
129
      cyan('"node scripts/' + key + '.js"')
130
    );
eanplatter's avatar
eanplatter committed
131
132
  });

133
  console.log();
Dan Abramov's avatar
Dan Abramov committed
134
  console.log(cyan('Configuring package.json'));
Dan Abramov's avatar
Dan Abramov committed
135
  // Add Jest config
136
  console.log('  Adding ' + cyan('Jest') + ' configuration');
Christoph Pojer's avatar
Christoph Pojer committed
137
  appPackage.jest = createJestConfig(
138
    filePath => path.posix.join('<rootDir>', filePath),
139
140
    null,
    true
Christoph Pojer's avatar
Christoph Pojer committed
141
142
  );

143
  // Add Babel config
144
  console.log('  Adding ' + cyan('Babel') + ' preset');
145
146
147
  appPackage.babel = babelConfig;

  // Add ESlint config
148
  console.log('  Adding ' + cyan('ESLint') +' configuration');
149
150
  appPackage.eslintConfig = eslintConfig;

eanplatter's avatar
eanplatter committed
151
  fs.writeFileSync(
152
    path.join(appPath, 'package.json'),
153
    JSON.stringify(appPackage, null, 2) + '\n'
eanplatter's avatar
eanplatter committed
154
155
156
  );
  console.log();

157
158
159
160
161
162
163
164
  try {
    // remove react-scripts and react-scripts binaries from app node_modules
    Object.keys(ownPackage.bin).forEach(function(binKey) {
      fs.removeSync(path.join(appPath, 'node_modules', '.bin', binKey));
    });
    fs.removeSync(ownPath);
  } catch(e) {}

165
  if (fs.existsSync(paths.yarnLockFile)) {
Ville Immonen's avatar
Ville Immonen committed
166
    console.log(cyan('Running yarn...'));
167
    spawnSync('yarnpkg', [], {stdio: 'inherit'});
Ville Immonen's avatar
Ville Immonen committed
168
169
170
171
  } else {
    console.log(cyan('Running npm install...'));
    spawnSync('npm', ['install'], {stdio: 'inherit'});
  }
172
  console.log(green('Ejected successfully!'));
eanplatter's avatar
eanplatter committed
173
174
  console.log();

175
176
177
178
  console.log(green('Please consider sharing why you ejected in this survey:'));
  console.log(green('  http://goo.gl/forms/Bi6CZjk1EqsdelXk1'));
  console.log()
})