Skip to content

Commit d606bcc

Browse files
djgrantTimer
authored andcommitted
Replace eject file whitelist with a "remove-file-on-eject" flag
1 parent 073cb55 commit d606bcc

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

packages/react-scripts/scripts/eject.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @remove-file-on-eject
12
/**
23
* Copyright (c) 2015-present, Facebook, Inc.
34
* All rights reserved.
@@ -7,13 +8,14 @@
78
* of patent rights can be found in the PATENTS file in the same directory.
89
*/
910

10-
var createJestConfig = require('../utils/createJestConfig');
1111
var fs = require('fs-extra');
1212
var path = require('path');
13-
var paths = require('../config/paths');
14-
var prompt = require('react-dev-utils/prompt');
1513
var spawnSync = require('cross-spawn').sync;
1614
var chalk = require('chalk');
15+
var prompt = require('react-dev-utils/prompt');
16+
var paths = require('../config/paths');
17+
var createJestConfig = require('../utils/createJestConfig');
18+
1719
var green = chalk.green;
1820
var cyan = chalk.cyan;
1921

@@ -45,45 +47,51 @@ prompt(
4547

4648
var folders = [
4749
'config',
48-
path.join('config', 'jest'),
50+
'config/jest',
4951
'scripts'
5052
];
5153

52-
var files = [
53-
path.join('config', 'env.js'),
54-
path.join('config', 'paths.js'),
55-
path.join('config', 'polyfills.js'),
56-
path.join('config', 'webpack.config.dev.js'),
57-
path.join('config', 'webpack.config.prod.js'),
58-
path.join('config', 'jest', 'cssTransform.js'),
59-
path.join('config', 'jest', 'fileTransform.js'),
60-
path.join('scripts', 'build.js'),
61-
path.join('scripts', 'start.js'),
62-
path.join('scripts', 'test.js')
63-
];
54+
// Make shallow array of files paths
55+
var files = folders.reduce(function (files, folder) {
56+
return files.concat(
57+
fs.readdirSync(path.join(ownPath, folder))
58+
// set full path
59+
.map(file => path.join(ownPath, folder, file))
60+
// omit dirs from file list
61+
.filter(file => fs.lstatSync(file).isFile())
62+
);
63+
}, []);
6464

6565
// Ensure that the app folder is clean and we won't override any files
6666
folders.forEach(verifyAbsent);
6767
files.forEach(verifyAbsent);
6868

69-
// Copy the files over
69+
console.log();
70+
console.log(cyan('Copying files into ' + appPath));
71+
7072
folders.forEach(function(folder) {
7173
fs.mkdirSync(path.join(appPath, folder))
7274
});
7375

74-
console.log();
75-
console.log(cyan('Copying files into ' + appPath));
7676
files.forEach(function(file) {
77-
console.log(' Adding ' + cyan(file) + ' to the project');
78-
var content = fs
79-
.readFileSync(path.join(ownPath, file), 'utf8')
77+
var content = fs.readFileSync(file, 'utf8')
78+
79+
// Skip flagged files
80+
if (content.match(/\/\/ @remove-file-on-eject/)) {
81+
return;
82+
}
83+
84+
content = content
8085
// Remove dead code from .js files on eject
8186
.replace(/\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/mg, '')
8287
// Remove dead code from .applescript files on eject
8388
.replace(/-- @remove-on-eject-begin([\s\S]*?)-- @remove-on-eject-end/mg, '')
8489
.trim() + '\n';
85-
fs.writeFileSync(path.join(appPath, file), content);
90+
91+
console.log(' Adding ' + cyan(file.replace(ownPath, '')) + ' to the project');
92+
fs.writeFileSync(file.replace(ownPath, appPath), content);
8693
});
94+
8795
console.log();
8896

8997
var ownPackage = require(path.join(ownPath, 'package.json'));

packages/react-scripts/scripts/init.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @remove-file-on-eject
12
/**
23
* Copyright (c) 2015-present, Facebook, Inc.
34
* All rights reserved.

packages/react-scripts/utils/createJestConfig.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @remove-file-on-eject
12
/**
23
* Copyright (c) 2015-present, Facebook, Inc.
34
* All rights reserved.
@@ -7,8 +8,6 @@
78
* of patent rights can be found in the PATENTS file in the same directory.
89
*/
910

10-
// Note: this file does not exist after ejecting.
11-
1211
const fs = require('fs');
1312
const paths = require('../config/paths');
1413

0 commit comments

Comments
 (0)