Skip to content

Commit e18e4ff

Browse files
gaearonrandycoulman
authored andcommitted
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 51b6886 commit e18e4ff

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
@@ -86,7 +86,6 @@ module.exports = {
8686
yarnLockFile: resolveApp('yarn.lock'),
8787
testsSetup: resolveApp('client/setupTests.js'),
8888
appNodeModules: resolveApp('node_modules'),
89-
ownNodeModules: resolveApp('node_modules'),
9089
nodePaths: nodePaths,
9190
publicUrl: getPublicUrl(resolveApp('package.json')),
9291
servedPath: getServedPath(resolveApp('package.json'))
@@ -100,7 +99,6 @@ function resolveOwn(relativePath) {
10099
// config before eject: we're in ./node_modules/react-scripts/config/
101100
module.exports = {
102101
appPath: resolveApp('.'),
103-
ownPath: resolveApp('node_modules/react-scripts'),
104102
appBuild: resolveApp(buildPath),
105103
appPublic: resolveApp('client/public'),
106104
appHtml: resolveApp('client/public/index.html'),
@@ -110,11 +108,12 @@ module.exports = {
110108
yarnLockFile: resolveApp('yarn.lock'),
111109
testsSetup: resolveApp('client/setupTests.js'),
112110
appNodeModules: resolveApp('node_modules'),
113-
// this is empty with npm3 but node resolution searches higher anyway:
114-
ownNodeModules: resolveOwn('node_modules'),
115111
nodePaths: nodePaths,
116112
publicUrl: getPublicUrl(resolveApp('package.json')),
117-
servedPath: getServedPath(resolveApp('package.json'))
113+
servedPath: getServedPath(resolveApp('package.json')),
114+
// These properties only exist before ejecting:
115+
ownPath: resolveOwn('.'),
116+
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
118117
};
119118

120119
var reactScriptsPath = path.resolve('node_modules/react-scripts');
@@ -124,7 +123,6 @@ var reactScriptsLinked = fs.existsSync(reactScriptsPath) && fs.lstatSync(reactSc
124123
if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
125124
module.exports = {
126125
appPath: resolveApp('.'),
127-
ownPath: resolveOwn('.'),
128126
appBuild: resolveOwn('../../' + buildPath),
129127
appPublic: resolveOwn('template/client/public'),
130128
appHtml: resolveOwn('template/client/public/index.html'),
@@ -134,10 +132,12 @@ if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-script
134132
yarnLockFile: resolveOwn('template/yarn.lock'),
135133
testsSetup: resolveOwn('template/client/setupTests.js'),
136134
appNodeModules: resolveOwn('node_modules'),
137-
ownNodeModules: resolveOwn('node_modules'),
138135
nodePaths: nodePaths,
139136
publicUrl: getPublicUrl(resolveOwn('package.json')),
140-
servedPath: getServedPath(resolveOwn('package.json'))
137+
servedPath: getServedPath(resolveOwn('package.json')),
138+
// These properties only exist before ejecting:
139+
ownPath: resolveOwn('.'),
140+
ownNodeModules: resolveOwn('node_modules'),
141141
};
142142
}
143143
// @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)