Skip to content

Commit

Permalink
Fix ejecting from a scoped fork (facebook#1727)
Browse files Browse the repository at this point in the history
* 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

# Conflicts:
#   packages/react-cy-scripts/scripts/eject.js
  • Loading branch information
gaearon authored and SpaceK33z committed Mar 7, 2017
1 parent 357941a commit 168f514
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
16 changes: 8 additions & 8 deletions packages/react-cy-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ module.exports = {
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveApp('src/setupTests.js'),
appNodeModules: resolveApp('node_modules'),
ownNodeModules: resolveApp('node_modules'),
nodePaths: nodePaths,
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json'))
Expand All @@ -98,7 +97,6 @@ function resolveOwn(relativePath) {
// config before eject: we're in ./node_modules/react-cy-scripts/config/
module.exports = {
appPath: resolveApp('.'),
ownPath: resolveApp('node_modules/react-scripts'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
Expand All @@ -108,11 +106,12 @@ module.exports = {
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveApp('src/setupTests.js'),
appNodeModules: resolveApp('node_modules'),
// this is empty with npm3 but node resolution searches higher anyway:
ownNodeModules: resolveOwn('node_modules'),
nodePaths: nodePaths,
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json'))
servedPath: getServedPath(resolveApp('package.json')),
// These properties only exist before ejecting:
ownPath: resolveOwn('.'),
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
};

var reactScriptsPath = path.resolve('node_modules/react-cy-scripts');
Expand All @@ -122,7 +121,6 @@ var reactScriptsLinked = fs.existsSync(reactScriptsPath) && fs.lstatSync(reactSc
if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-cy-scripts', 'config')) !== -1) {
module.exports = {
appPath: resolveApp('.'),
ownPath: resolveOwn('.'),
appBuild: resolveOwn('../../build'),
appPublic: resolveOwn('template/public'),
appHtml: resolveOwn('template/public/index.html'),
Expand All @@ -132,10 +130,12 @@ if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-cy-scr
yarnLockFile: resolveOwn('template/yarn.lock'),
testsSetup: resolveOwn('template/src/setupTests.js'),
appNodeModules: resolveOwn('node_modules'),
ownNodeModules: resolveOwn('node_modules'),
nodePaths: nodePaths,
publicUrl: getPublicUrl(resolveOwn('package.json')),
servedPath: getServedPath(resolveOwn('package.json'))
servedPath: getServedPath(resolveOwn('package.json')),
// These properties only exist before ejecting:
ownPath: resolveOwn('.'),
ownNodeModules: resolveOwn('node_modules'),
};
}
// @remove-on-eject-end
19 changes: 11 additions & 8 deletions packages/react-cy-scripts/scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ prompt(
console.log(cyan('Updating the scripts'));
delete appPackage.scripts['eject'];
Object.keys(appPackage.scripts).forEach(function (key) {
appPackage.scripts[key] = appPackage.scripts[key]
.replace(/react-cy-scripts (\w+)/g, 'node scripts/$1.js');
console.log(
' Replacing ' +
cyan('"react-cy-scripts ' + key + '"') +
' with ' +
cyan('"node scripts/' + key + '.js"')
);
Object.keys(ownPackage.bin).forEach(function (binKey) {
var regex = new RegExp(binKey + ' (\\w+)', 'g');
appPackage.scripts[key] = appPackage.scripts[key]
.replace(regex, 'node scripts/$1.js');
console.log(
' Replacing ' +
cyan('"' + binKey + ' ' + key + '"') +
' with ' +
cyan('"node scripts/' + key + '.js"')
);
});
});

console.log();
Expand Down

0 comments on commit 168f514

Please sign in to comment.