1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | process.on('unhandledRejection', err => {
|
14 | throw err;
|
15 | });
|
16 |
|
17 | const fs = require('fs-extra');
|
18 | const path = require('path');
|
19 | const execSync = require('child_process').execSync;
|
20 | const chalk = require('chalk');
|
21 | const paths = require('../config/paths');
|
22 | const createJestConfig = require('./utils/createJestConfig');
|
23 | const inquirer = require('react-dev-utils/inquirer');
|
24 | const spawnSync = require('react-dev-utils/crossSpawn').sync;
|
25 | const os = require('os');
|
26 |
|
27 | const green = chalk.green;
|
28 | const cyan = chalk.cyan;
|
29 |
|
30 | function getGitStatus() {
|
31 | try {
|
32 | let stdout = execSync(`git status --porcelain`, {
|
33 | stdio: ['pipe', 'pipe', 'ignore'],
|
34 | }).toString();
|
35 | return stdout.trim();
|
36 | } catch (e) {
|
37 | return '';
|
38 | }
|
39 | }
|
40 |
|
41 | inquirer
|
42 | .prompt({
|
43 | type: 'confirm',
|
44 | name: 'shouldEject',
|
45 | message: 'Are you sure you want to eject? This action is permanent.',
|
46 | default: false,
|
47 | })
|
48 | .then(answer => {
|
49 | if (!answer.shouldEject) {
|
50 | console.log(cyan('Close one! Eject aborted.'));
|
51 | return;
|
52 | }
|
53 |
|
54 | const gitStatus = getGitStatus();
|
55 | if (gitStatus) {
|
56 | console.error(
|
57 | chalk.red(
|
58 | 'This git repository has untracked files or uncommitted changes:'
|
59 | ) +
|
60 | '\n\n' +
|
61 | gitStatus
|
62 | .split('\n')
|
63 | .map(line => line.match(/ .*/g)[0].trim())
|
64 | .join('\n') +
|
65 | '\n\n' +
|
66 | chalk.red(
|
67 | 'Remove untracked files, stash or commit any changes, and try again.'
|
68 | )
|
69 | );
|
70 | process.exit(1);
|
71 | }
|
72 |
|
73 | console.log('Ejecting...');
|
74 |
|
75 | const ownPath = paths.ownPath;
|
76 | const appPath = paths.appPath;
|
77 |
|
78 | function verifyAbsent(file) {
|
79 | if (fs.existsSync(path.join(appPath, file))) {
|
80 | console.error(
|
81 | `\`${file}\` already exists in your app folder. We cannot ` +
|
82 | 'continue as you would lose all the changes in that file or directory. ' +
|
83 | 'Please move or delete it (maybe make a copy for backup) and run this ' +
|
84 | 'command again.'
|
85 | );
|
86 | process.exit(1);
|
87 | }
|
88 | }
|
89 |
|
90 | const folders = ['config', 'config/jest', 'scripts'];
|
91 |
|
92 |
|
93 | const files = folders.reduce((files, folder) => {
|
94 | return files.concat(
|
95 | fs
|
96 | .readdirSync(path.join(ownPath, folder))
|
97 |
|
98 | .map(file => path.join(ownPath, folder, file))
|
99 |
|
100 | .filter(file => fs.lstatSync(file).isFile())
|
101 | );
|
102 | }, []);
|
103 |
|
104 |
|
105 | folders.forEach(verifyAbsent);
|
106 | files.forEach(verifyAbsent);
|
107 |
|
108 |
|
109 | const jestConfig = createJestConfig(
|
110 | filePath => path.posix.join('<rootDir>', filePath),
|
111 | null,
|
112 | true
|
113 | );
|
114 |
|
115 | console.log();
|
116 | console.log(cyan(`Copying files into ${appPath}`));
|
117 |
|
118 | folders.forEach(folder => {
|
119 | fs.mkdirSync(path.join(appPath, folder));
|
120 | });
|
121 |
|
122 | files.forEach(file => {
|
123 | let content = fs.readFileSync(file, 'utf8');
|
124 |
|
125 |
|
126 | if (content.match(/\/\/ @remove-file-on-eject/)) {
|
127 | return;
|
128 | }
|
129 | content =
|
130 | content
|
131 |
|
132 | .replace(
|
133 | /\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/gm,
|
134 | ''
|
135 | )
|
136 |
|
137 | .replace(
|
138 | /-- @remove-on-eject-begin([\s\S]*?)-- @remove-on-eject-end/gm,
|
139 | ''
|
140 | )
|
141 | .trim() + '\n';
|
142 | console.log(` Adding ${cyan(file.replace(ownPath, ''))} to the project`);
|
143 | fs.writeFileSync(file.replace(ownPath, appPath), content);
|
144 | });
|
145 | console.log();
|
146 |
|
147 | const ownPackage = require(path.join(ownPath, 'package.json'));
|
148 | const appPackage = require(path.join(appPath, 'package.json'));
|
149 |
|
150 | console.log(cyan('Updating the dependencies'));
|
151 | const ownPackageName = ownPackage.name;
|
152 | if (appPackage.devDependencies) {
|
153 |
|
154 | if (appPackage.devDependencies[ownPackageName]) {
|
155 | console.log(` Removing ${cyan(ownPackageName)} from devDependencies`);
|
156 | delete appPackage.devDependencies[ownPackageName];
|
157 | }
|
158 | }
|
159 | appPackage.dependencies = appPackage.dependencies || {};
|
160 | if (appPackage.dependencies[ownPackageName]) {
|
161 | console.log(` Removing ${cyan(ownPackageName)} from dependencies`);
|
162 | delete appPackage.dependencies[ownPackageName];
|
163 | }
|
164 | Object.keys(ownPackage.dependencies).forEach(key => {
|
165 |
|
166 | if (ownPackage.optionalDependencies[key]) {
|
167 | return;
|
168 | }
|
169 | console.log(` Adding ${cyan(key)} to dependencies`);
|
170 | appPackage.dependencies[key] = ownPackage.dependencies[key];
|
171 | });
|
172 |
|
173 | const unsortedDependencies = appPackage.dependencies;
|
174 | appPackage.dependencies = {};
|
175 | Object.keys(unsortedDependencies)
|
176 | .sort()
|
177 | .forEach(key => {
|
178 | appPackage.dependencies[key] = unsortedDependencies[key];
|
179 | });
|
180 | console.log();
|
181 |
|
182 | console.log(cyan('Updating the scripts'));
|
183 | delete appPackage.scripts['eject'];
|
184 | Object.keys(appPackage.scripts).forEach(key => {
|
185 | Object.keys(ownPackage.bin).forEach(binKey => {
|
186 | const regex = new RegExp(binKey + ' (\\w+)', 'g');
|
187 | if (!regex.test(appPackage.scripts[key])) {
|
188 | return;
|
189 | }
|
190 | appPackage.scripts[key] = appPackage.scripts[key].replace(
|
191 | regex,
|
192 | 'node scripts/$1.js'
|
193 | );
|
194 | console.log(
|
195 | ` Replacing ${cyan(`"${binKey} ${key}"`)} with ${cyan(
|
196 | `"node scripts/${key}.js"`
|
197 | )}`
|
198 | );
|
199 | });
|
200 | });
|
201 |
|
202 | console.log();
|
203 | console.log(cyan('Configuring package.json'));
|
204 |
|
205 | console.log(` Adding ${cyan('Jest')} configuration`);
|
206 | appPackage.jest = jestConfig;
|
207 |
|
208 |
|
209 | console.log(` Adding ${cyan('Babel')} preset`);
|
210 | appPackage.babel = {
|
211 | presets: ['react-app'],
|
212 | };
|
213 |
|
214 |
|
215 | console.log(` Adding ${cyan('ESLint')} configuration`);
|
216 | appPackage.eslintConfig = {
|
217 | extends: 'react-app',
|
218 | };
|
219 |
|
220 | fs.writeFileSync(
|
221 | path.join(appPath, 'package.json'),
|
222 | JSON.stringify(appPackage, null, 2) + os.EOL
|
223 | );
|
224 | console.log();
|
225 |
|
226 |
|
227 | if (ownPath.indexOf(appPath) === 0) {
|
228 | try {
|
229 |
|
230 | Object.keys(ownPackage.bin).forEach(binKey => {
|
231 | fs.removeSync(path.join(appPath, 'node_modules', '.bin', binKey));
|
232 | });
|
233 | fs.removeSync(ownPath);
|
234 | } catch (e) {
|
235 |
|
236 | }
|
237 | }
|
238 |
|
239 | if (fs.existsSync(paths.yarnLockFile)) {
|
240 | const windowsCmdFilePath = path.join(
|
241 | appPath,
|
242 | 'node_modules',
|
243 | '.bin',
|
244 | 'react-scripts.cmd'
|
245 | );
|
246 | let windowsCmdFileContent;
|
247 | if (process.platform === 'win32') {
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | try {
|
253 | windowsCmdFileContent = fs.readFileSync(windowsCmdFilePath);
|
254 | } catch (err) {
|
255 |
|
256 | }
|
257 | }
|
258 |
|
259 | console.log(cyan('Running yarn...'));
|
260 | spawnSync('yarnpkg', ['--cwd', process.cwd()], { stdio: 'inherit' });
|
261 |
|
262 | if (windowsCmdFileContent && !fs.existsSync(windowsCmdFilePath)) {
|
263 | try {
|
264 | fs.writeFileSync(windowsCmdFilePath, windowsCmdFileContent);
|
265 | } catch (err) {
|
266 |
|
267 | }
|
268 | }
|
269 | } else {
|
270 | console.log(cyan('Running npm install...'));
|
271 | spawnSync('npm', ['install', '--loglevel', 'error'], {
|
272 | stdio: 'inherit',
|
273 | });
|
274 | }
|
275 | console.log(green('Ejected successfully!'));
|
276 | console.log();
|
277 |
|
278 | console.log(
|
279 | green('Please consider sharing why you ejected in this survey:')
|
280 | );
|
281 | console.log(green(' http://goo.gl/forms/Bi6CZjk1EqsdelXk1'));
|
282 | console.log();
|
283 | });
|