Skip to content

Commit 23dc422

Browse files
authored
Fix ejecting from a scoped fork (facebook#1727)
* Read script names from own bin instead of guessing This fixes ejecting from a fork that uses a different bin script name. * Fix ejecting for a scoped react-scripts fork We shouldn't hardcode react-scripts because fork name might differ. We also shouldn't rely on it being an immediate child because scoped packages are a level deeper. * Clarify that own* properties only exist before ejecting
1 parent 2f7e3a2 commit 23dc422

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

packages/react-scripts/config/paths.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ module.exports = {
8383
yarnLockFile: resolveApp('yarn.lock'),
8484
testsSetup: resolveApp('src/setupTests.js'),
8585
appNodeModules: resolveApp('node_modules'),
86-
ownNodeModules: resolveApp('node_modules'),
8786
nodePaths: nodePaths,
8887
publicUrl: getPublicUrl(resolveApp('package.json')),
8988
servedPath: getServedPath(resolveApp('package.json'))
@@ -97,7 +96,6 @@ function resolveOwn(relativePath) {
9796
// config before eject: we're in ./node_modules/react-scripts/config/
9897
module.exports = {
9998
appPath: resolveApp('.'),
100-
ownPath: resolveApp('node_modules/react-scripts'),
10199
appBuild: resolveApp('build'),
102100
appPublic: resolveApp('public'),
103101
appHtml: resolveApp('public/index.html'),
@@ -107,11 +105,12 @@ module.exports = {
107105
yarnLockFile: resolveApp('yarn.lock'),
108106
testsSetup: resolveApp('src/setupTests.js'),
109107
appNodeModules: resolveApp('node_modules'),
110-
// this is empty with npm3 but node resolution searches higher anyway:
111-
ownNodeModules: resolveOwn('node_modules'),
112108
nodePaths: nodePaths,
113109
publicUrl: getPublicUrl(resolveApp('package.json')),
114-
servedPath: getServedPath(resolveApp('package.json'))
110+
servedPath: getServedPath(resolveApp('package.json')),
111+
// These properties only exist before ejecting:
112+
ownPath: resolveOwn('.'),
113+
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
115114
};
116115

117116
var reactScriptsPath = path.resolve('node_modules/react-scripts');
@@ -121,7 +120,6 @@ var reactScriptsLinked = fs.existsSync(reactScriptsPath) && fs.lstatSync(reactSc
121120
if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
122121
module.exports = {
123122
appPath: resolveApp('.'),
124-
ownPath: resolveOwn('.'),
125123
appBuild: resolveOwn('../../build'),
126124
appPublic: resolveOwn('template/public'),
127125
appHtml: resolveOwn('template/public/index.html'),
@@ -131,10 +129,12 @@ if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-script
131129
yarnLockFile: resolveOwn('template/yarn.lock'),
132130
testsSetup: resolveOwn('template/src/setupTests.js'),
133131
appNodeModules: resolveOwn('node_modules'),
134-
ownNodeModules: resolveOwn('node_modules'),
135132
nodePaths: nodePaths,
136133
publicUrl: getPublicUrl(resolveOwn('package.json')),
137-
servedPath: getServedPath(resolveOwn('package.json'))
134+
servedPath: getServedPath(resolveOwn('package.json')),
135+
// These properties only exist before ejecting:
136+
ownPath: resolveOwn('.'),
137+
ownNodeModules: resolveOwn('node_modules'),
138138
};
139139
}
140140
// @remove-on-eject-end

packages/react-scripts/scripts/eject.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,17 @@ prompt(
120120
console.log(cyan('Updating the scripts'));
121121
delete appPackage.scripts['eject'];
122122
Object.keys(appPackage.scripts).forEach(function (key) {
123-
appPackage.scripts[key] = appPackage.scripts[key]
124-
.replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
125-
console.log(
126-
' Replacing ' +
127-
cyan('"react-scripts ' + key + '"') +
128-
' with ' +
129-
cyan('"node scripts/' + key + '.js"')
130-
);
123+
Object.keys(ownPackage.bin).forEach(function (binKey) {
124+
var regex = new RegExp(binKey + ' (\\w+)', 'g');
125+
appPackage.scripts[key] = appPackage.scripts[key]
126+
.replace(regex, 'node scripts/$1.js');
127+
console.log(
128+
' Replacing ' +
129+
cyan('"' + binKey + ' ' + key + '"') +
130+
' with ' +
131+
cyan('"node scripts/' + key + '.js"')
132+
);
133+
});
131134
});
132135

133136
console.log();

0 commit comments

Comments
 (0)