Skip to content

Commit

Permalink
Use env var to signal yarn usage to main script
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Aug 23, 2021
1 parent a666acc commit 9a1cb99
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
24 changes: 11 additions & 13 deletions packages/create-react-app/createReactApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ const validateProjectName = require('validate-npm-package-name');

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

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

let projectName;

function init() {
Expand All @@ -69,7 +77,7 @@ function init() {
'--template <path-to-template>',
'specify a template for the created project'
)
.option('--use-npm')
.option('--use-yarn', 'use yarn as the package manager', isUsingYarn())
.option('--use-pnp')
.allowUnknownOption()
.on('--help', () => {
Expand Down Expand Up @@ -228,14 +236,14 @@ function init() {
program.verbose,
program.scriptsVersion,
program.template,
program.useNpm,
program.useYarn,
program.usePnp
);
}
});
}

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

const useYarn = useNpm ? false : shouldUseYarn();
const originalDirectory = process.cwd();
process.chdir(root);
if (!useYarn && !checkThatNpmCanReadCwd()) {
Expand Down Expand Up @@ -351,15 +358,6 @@ function createApp(name, verbose, version, template, useNpm, usePnp) {
);
}

function shouldUseYarn() {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}

function install(root, useYarn, usePnp, dependencies, verbose, isOnline) {
return new Promise((resolve, reject) => {
let command;
Expand Down
37 changes: 23 additions & 14 deletions tasks/e2e-installs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ cd "$temp_app_path"
npx create-react-app test-app-dist-tag --scripts-version=@latest
cd test-app-dist-tag

# Check corresponding scripts version is installed and no TypeScript is present.
# Check corresponding scripts version is installed and no TypeScript or yarn is present by default
exists node_modules/react-scripts
! exists node_modules/typescript
! exists src/index.tsx
! exists yarn.lock
exists src/index.js
checkDependencies

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

# ******************************************************************************
# Test --use-npm flag
# Test --use-yarn flag
# ******************************************************************************

cd "$temp_app_path"
npx create-react-app test-use-npm-flag --use-npm --scripts-version=1.0.17
cd test-use-npm-flag
npx create-react-app test-use-yarn-flag --use-yarn --scripts-version=1.0.17
cd test-use-yarn-flag

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

# ******************************************************************************
# Test yarn create
# ******************************************************************************

cd "$temp_app_path"
yarn create react-app test-use-yarn-create --scripts-version=1.0.17
cd test-use-yarn-create

# Check corresponding scripts version is installed.
exists node_modules/react-scripts
exists yarn.lock
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
checkDependencies

Expand Down Expand Up @@ -172,10 +187,6 @@ CI=true npm test
# Eject...
echo yes | npm run eject

# Temporary workaround for https://github.com/facebook/create-react-app/issues/6099
rm yarn.lock
yarn add @babel/plugin-transform-react-jsx-source @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx @babel/plugin-transform-react-jsx-self

# Ensure env file still exists
exists src/react-app-env.d.ts

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

Expand Down Expand Up @@ -277,12 +288,10 @@ npm start -- --smoke-test
# Test when PnP is enabled
# ******************************************************************************
cd "$temp_app_path"
npx create-react-app test-app-pnp --use-pnp
npx create-react-app test-app-pnp --use-yarn --use-pnp
cd test-app-pnp
! exists node_modules
exists .pnp.js
npm start -- --smoke-test
npm run build

# Cleanup
cleanup

0 comments on commit 9a1cb99

Please sign in to comment.