Skip to content

Commit 1a331e1

Browse files
committed
Fix ejecting from a scoped fork (#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 b210d7a commit 1a331e1

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
@@ -114,14 +114,17 @@ prompt(
114114
console.log(cyan('Updating the scripts'));
115115
delete appPackage.scripts['eject'];
116116
Object.keys(appPackage.scripts).forEach(function (key) {
117-
appPackage.scripts[key] = appPackage.scripts[key]
118-
.replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
119-
console.log(
120-
' Replacing ' +
121-
cyan('"react-scripts ' + key + '"') +
122-
' with ' +
123-
cyan('"node scripts/' + key + '.js"')
124-
);
117+
Object.keys(ownPackage.bin).forEach(function (binKey) {
118+
var regex = new RegExp(binKey + ' (\\w+)', 'g');
119+
appPackage.scripts[key] = appPackage.scripts[key]
120+
.replace(regex, 'node scripts/$1.js');
121+
console.log(
122+
' Replacing ' +
123+
cyan('"' + binKey + ' ' + key + '"') +
124+
' with ' +
125+
cyan('"node scripts/' + key + '.js"')
126+
);
127+
});
125128
});
126129

127130
console.log();

0 commit comments

Comments
 (0)