-
-
Notifications
You must be signed in to change notification settings - Fork 27k
/
Copy patheject.js
176 lines (155 loc) · 5.74 KB
/
eject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* 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.
*/
var createJestConfig = require('./utils/createJestConfig');
var fs = require('fs');
var path = require('path');
var prompt = require('./utils/prompt');
var rimrafSync = require('rimraf').sync;
var spawnSync = require('cross-spawn').sync;
var babelDevConfig = require('../config/babel.dev.js')(false);
var babelProdConfig = require('../config/babel.prod.js')(false);
prompt(
'Are you sure you want to eject? This action is permanent.',
false
).then(shouldEject => {
if (!shouldEject) {
console.log('Close one! Eject aborted.');
process.exit(1);
}
console.log('Ejecting...');
console.log();
var ownPath = path.join(__dirname, '..');
var appPath = path.join(ownPath, '..', '..');
var files = [
path.join('config', 'flow', 'css.js.flow'),
path.join('config', 'flow', 'file.js.flow'),
path.join('config', 'eslint.js'),
path.join('config', 'paths.js'),
path.join('config', 'env.js'),
path.join('config', 'polyfills.js'),
path.join('config', 'webpack.config.dev.js'),
path.join('config', 'webpack.config.prod.js'),
path.join('config', 'jest', 'CSSStub.js'),
path.join('config', 'jest', 'FileStub.js'),
path.join('config', 'jest', 'transform.js'),
path.join('scripts', 'build.js'),
path.join('scripts', 'start.js'),
path.join('scripts', 'utils', 'chrome.applescript'),
path.join('scripts', 'utils', 'prompt.js'),
path.join('scripts', 'utils', 'WatchMissingNodeModulesPlugin.js')
];
// Ensure that the app folder is clean and we won't override any files
files.forEach(function(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 delete it (maybe make a copy for backup) and run this ' +
'command again.'
);
process.exit(1);
}
});
// Ensure a .babelrc file doesn't exist in the app config folder
if (fs.existsSync(path.join(appPath, 'config', '.babelrc'))) {
console.error(
'config/.babelrc already exists in your app folder. We cannot ' +
'continue as you would lose all the changes in that file ' +
'Please delete it (maybe make a copy for backup) and run this ' +
'command again. You might have to manually merge your configs.'
);
process.exit(1);
}
// Copy the files over
fs.mkdirSync(path.join(appPath, 'config'));
fs.mkdirSync(path.join(appPath, 'config', 'flow'));
fs.mkdirSync(path.join(appPath, 'config', 'jest'));
fs.mkdirSync(path.join(appPath, 'scripts'));
fs.mkdirSync(path.join(appPath, 'scripts', 'utils'));
files.forEach(function(file) {
console.log('Copying ' + file + ' to ' + appPath);
var content = fs
.readFileSync(path.join(ownPath, file), 'utf8')
// 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, '')
.trim() + '\n';
fs.writeFileSync(path.join(appPath, file), content);
});
console.log();
// Create .babelrc from dev and prod configs
var babelrc = {
env: {
test: {
presets: babelDevConfig.presets,
plugins: babelDevConfig.plugins
},
development: {
presets: babelDevConfig.presets,
plugins: babelDevConfig.plugins
},
production: {
presets: babelProdConfig.presets,
plugins: babelProdConfig.plugins
}
}
};
console.log('Writing config/.babelrc to ' + appPath );
fs.writeFileSync(
path.join(appPath, 'config', '.babelrc'),
JSON.stringify(babelrc, null, 2)
);
console.log();
var ownPackage = require(path.join(ownPath, 'package.json'));
var appPackage = require(path.join(appPath, 'package.json'));
console.log('Removing dependency: react-scripts');
delete appPackage.devDependencies['react-scripts'];
Object.keys(ownPackage.dependencies).forEach(function (key) {
// For some reason optionalDependencies end up in dependencies after install
if (ownPackage.optionalDependencies[key]) {
return;
}
console.log('Adding dependency: ' + key);
appPackage.devDependencies[key] = ownPackage.dependencies[key];
});
console.log('Updating scripts');
delete appPackage.scripts['eject'];
Object.keys(appPackage.scripts).forEach(function (key) {
appPackage.scripts[key] = appPackage.scripts[key]
.replace(/react-scripts test/g, 'jest --watch')
.replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
});
// Add Jest config
appPackage.jest = createJestConfig(
filePath => path.join('<rootDir>', filePath)
);
// Explicitly specify ESLint config path for editor plugins
appPackage.eslintConfig = {
extends: './config/eslint.js',
};
// Explicitly specify .babelrc config path
appPackage.babel = {
extends: './config/.babelrc'
};
console.log('Writing package.json');
fs.writeFileSync(
path.join(appPath, 'package.json'),
JSON.stringify(appPackage, null, 2)
);
console.log();
console.log('Running npm install...');
rimrafSync(ownPath);
spawnSync('npm', ['install'], {stdio: 'inherit'});
console.log('Ejected successfully!');
console.log();
console.log('Please consider sharing why you ejected in this survey:');
console.log(' http://goo.gl/forms/Bi6CZjk1EqsdelXk1');
console.log();
});