Skip to content

Commit 9a1cb99

Browse files
committed
Use env var to signal yarn usage to main script
1 parent a666acc commit 9a1cb99

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
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

+23-14
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,30 @@ 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
147+
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
148+
checkDependencies
149+
150+
# ******************************************************************************
151+
# Test yarn create
152+
# ******************************************************************************
153+
154+
cd "$temp_app_path"
155+
yarn create react-app test-use-yarn-create --scripts-version=1.0.17
156+
cd test-use-yarn-create
157+
158+
# Check corresponding scripts version is installed.
159+
exists node_modules/react-scripts
160+
exists yarn.lock
146161
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
147162
checkDependencies
148163

@@ -172,10 +187,6 @@ CI=true npm test
172187
# Eject...
173188
echo yes | npm run eject
174189

175-
# Temporary workaround for https://github.com/facebook/create-react-app/issues/6099
176-
rm yarn.lock
177-
yarn add @babel/plugin-transform-react-jsx-source @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx @babel/plugin-transform-react-jsx-self
178-
179190
# Ensure env file still exists
180191
exists src/react-app-env.d.ts
181192

@@ -230,8 +241,8 @@ echo '## Hello' > ./test-app-should-remain/README.md
230241
npx create-react-app test-app-should-remain --scripts-version=`date +%s` || true
231242
# confirm the file exist
232243
test -e test-app-should-remain/README.md
233-
# confirm only README.md and error log are the only files in the directory
234-
if [ "$(ls -1 ./test-app-should-remain | wc -l | tr -d '[:space:]')" != "2" ]; then
244+
# confirm only README.md is the only file in the directory
245+
if [ "$(ls -1 ./test-app-should-remain | wc -l | tr -d '[:space:]')" != "1" ]; then
235246
false
236247
fi
237248

@@ -277,12 +288,10 @@ npm start -- --smoke-test
277288
# Test when PnP is enabled
278289
# ******************************************************************************
279290
cd "$temp_app_path"
280-
npx create-react-app test-app-pnp --use-pnp
291+
npx create-react-app test-app-pnp --use-yarn --use-pnp
281292
cd test-app-pnp
282293
! exists node_modules
283294
exists .pnp.js
284-
npm start -- --smoke-test
285-
npm run build
286295

287296
# Cleanup
288297
cleanup

0 commit comments

Comments
 (0)