Skip to content

Commit 0427380

Browse files
committed
Detect yarn usage from execpath env variable as signal to use yarn
as the package manager. Otherwise it will use npm.
1 parent 64ff23d commit 0427380

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

packages/create-react-app/createReactApp.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ const validateProjectName = require('validate-npm-package-name');
4949

5050
const packageJson = require('./package.json');
5151

52+
function isUsingYarn() {
53+
// Yarn 1 and 2 set npm_execpath to a path ending with 'yarn' so we can detect
54+
// that to determine if the script was run using yarn
55+
// https://github.com/yarnpkg/yarn/blob/v1.22.11/src/util/execute-lifecycle-script.js#L81
56+
// https://github.com/yarnpkg/berry/blob/%40yarnpkg/core%2F3.1.0-rc.2/packages/yarnpkg-core/sources/scriptUtils.ts#L113
57+
return path.basename(process.env.npm_execpath || '', '.js') === 'yarn';
58+
}
59+
5260
let projectName;
5361

5462
function init() {
@@ -69,7 +77,7 @@ function init() {
6977
'--template <path-to-template>',
7078
'specify a template for the created project'
7179
)
72-
.option('--use-npm')
80+
.option('--use-yarn', 'use yarn as the package manager', isUsingYarn())
7381
.option('--use-pnp')
7482
.allowUnknownOption()
7583
.on('--help', () => {
@@ -228,14 +236,14 @@ function init() {
228236
program.verbose,
229237
program.scriptsVersion,
230238
program.template,
231-
program.useNpm,
239+
program.useYarn,
232240
program.usePnp
233241
);
234242
}
235243
});
236244
}
237245

238-
function createApp(name, verbose, version, template, useNpm, usePnp) {
246+
function createApp(name, verbose, version, template, useYarn, usePnp) {
239247
const unsupportedNodeVersion = !semver.satisfies(
240248
// Coerce strings with metadata (i.e. `15.0.0-nightly`).
241249
semver.coerce(process.version),
@@ -276,7 +284,6 @@ function createApp(name, verbose, version, template, useNpm, usePnp) {
276284
JSON.stringify(packageJson, null, 2) + os.EOL
277285
);
278286

279-
const useYarn = useNpm ? false : shouldUseYarn();
280287
const originalDirectory = process.cwd();
281288
process.chdir(root);
282289
if (!useYarn && !checkThatNpmCanReadCwd()) {
@@ -351,15 +358,6 @@ function createApp(name, verbose, version, template, useNpm, usePnp) {
351358
);
352359
}
353360

354-
function shouldUseYarn() {
355-
try {
356-
execSync('yarnpkg --version', { stdio: 'ignore' });
357-
return true;
358-
} catch (e) {
359-
return false;
360-
}
361-
}
362-
363361
function install(root, useYarn, usePnp, dependencies, verbose, isOnline) {
364362
return new Promise((resolve, reject) => {
365363
let command;

tasks/e2e-installs.sh

+7-6
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ cd "$temp_app_path"
112112
npx create-react-app test-app-dist-tag --scripts-version=@latest
113113
cd test-app-dist-tag
114114

115-
# Check corresponding scripts version is installed and no TypeScript is present.
115+
# Check corresponding scripts version is installed and no TypeScript or yarn is present by default
116116
exists node_modules/react-scripts
117117
! exists node_modules/typescript
118118
! exists src/index.tsx
119+
! exists yarn.lock
119120
exists src/index.js
120121
checkDependencies
121122

@@ -133,16 +134,16 @@ grep '"version": "1.0.17"' node_modules/react-scripts/package.json
133134
checkDependencies
134135

135136
# ******************************************************************************
136-
# Test --use-npm flag
137+
# Test --use-yarn flag
137138
# ******************************************************************************
138139

139140
cd "$temp_app_path"
140-
npx create-react-app test-use-npm-flag --use-npm --scripts-version=1.0.17
141-
cd test-use-npm-flag
141+
npx create-react-app test-use-yarn-flag --use-yarn --scripts-version=1.0.17
142+
cd test-use-yarn-flag
142143

143144
# Check corresponding scripts version is installed.
144145
exists node_modules/react-scripts
145-
[ ! -e "yarn.lock" ] && echo "yarn.lock correctly does not exist"
146+
exists yarn.lock
146147
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
147148
checkDependencies
148149

@@ -277,7 +278,7 @@ npm start -- --smoke-test
277278
# Test when PnP is enabled
278279
# ******************************************************************************
279280
cd "$temp_app_path"
280-
npx create-react-app test-app-pnp --use-pnp
281+
npx create-react-app test-app-pnp --use-yarn --use-pnp
281282
cd test-app-pnp
282283
! exists node_modules
283284
exists .pnp.js

tasks/e2e-kitchensink-eject.sh

-6
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ cd "$temp_app_path/test-kitchensink"
110110
# In kitchensink, we want to test all transforms
111111
export BROWSERSLIST='ie 9'
112112

113-
# Link to test module
114-
npm link "$temp_module_path/node_modules/test-integrity"
115-
116113
# ******************************************************************************
117114
# Finally, let's check that everything still works after ejecting.
118115
# ******************************************************************************
@@ -124,9 +121,6 @@ echo yes | npm run eject
124121
rm yarn.lock
125122
yarn add @babel/plugin-transform-react-jsx-source @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx @babel/plugin-transform-react-jsx-self
126123

127-
# Link to test module
128-
npm link "$temp_module_path/node_modules/test-integrity"
129-
130124
# Test the build
131125
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
132126
NODE_PATH=src \

0 commit comments

Comments
 (0)