From 323d7ae1119f1c9de3c90cf58e81fc4b1f83e218 Mon Sep 17 00:00:00 2001 From: Dominic Saadi Date: Thu, 25 Jan 2024 00:30:55 +0000 Subject: [PATCH] fix(crwa): remove yarn-install option for yarn 1 (#9881) Follow up to https://github.com/redwoodjs/redwood/pull/9861. --- .../src/create-redwood-app.js | 44 +++++++++++++++---- packages/create-redwood-app/tests/e2e.test.ts | 4 +- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/packages/create-redwood-app/src/create-redwood-app.js b/packages/create-redwood-app/src/create-redwood-app.js index bd3d75d9596d..8e9e535a7634 100644 --- a/packages/create-redwood-app/src/create-redwood-app.js +++ b/packages/create-redwood-app/src/create-redwood-app.js @@ -38,6 +38,20 @@ const { telemetry } = Parser(hideBin(process.argv), { const tui = new RedwoodTUI() +function isYarnBerryOrNewer() { + const { npm_config_user_agent: npmConfigUserAgent } = process.env + + if (npmConfigUserAgent) { + const match = npmConfigUserAgent.match(/yarn\/(\d+)/) + + if (match && match[1]) { + return parseInt(match[1], 10) >= 2 + } + } + + return false +} + const USE_GITPOD_TEXT = [ ` As an alternative solution, you can launch a Redwood project using GitPod instead. GitPod is a an online IDE.`, ` See: ${terminalLink( @@ -669,11 +683,6 @@ async function createRedwoodApp() { type: 'string', describe: 'Commit message for the initial commit', }) - .option('yarn-install', { - default: null, - type: 'boolean', - describe: 'Install node modules. Skip via --no-yarn-install.', - }) .option('telemetry', { default: true, type: 'boolean', @@ -681,6 +690,17 @@ async function createRedwoodApp() { 'Enables sending telemetry events for this create command and all Redwood CLI commands https://telemetry.redwoodjs.com', }) + const _isYarnBerryOrNewer = isYarnBerryOrNewer() + + // Only add the yarn-install flag if the yarn version is >= 2 + if (_isYarnBerryOrNewer) { + cli.option('yarn-install', { + default: null, + type: 'boolean', + describe: 'Install node modules. Skip via --no-yarn-install.', + }) + } + const parsedFlags = cli.parse() tui.drawText( @@ -696,7 +716,9 @@ async function createRedwoodApp() { // Extract the args as provided by the user in the command line // TODO: Make all flags have the 'flag' suffix const args = parsedFlags._ - const yarnInstallFlag = parsedFlags['yarn-install'] ?? parsedFlags.yes + const yarnInstallFlag = + parsedFlags['yarn-install'] ?? + (_isYarnBerryOrNewer ? parsedFlags.yes : null) const typescriptFlag = parsedFlags.typescript ?? parsedFlags.yes const overwrite = parsedFlags.overwrite const gitInitFlag = parsedFlags['git-init'] ?? parsedFlags.yes @@ -734,7 +756,11 @@ async function createRedwoodApp() { commitMessage = await handleCommitMessagePreference(commitMessageFlag) } - const yarnInstall = await handleYarnInstallPreference(yarnInstallFlag) + let yarnInstall = false + + if (_isYarnBerryOrNewer) { + yarnInstall = await handleYarnInstallPreference(yarnInstallFlag) + } let newAppDir = path.resolve(process.cwd(), targetDir) @@ -750,7 +776,9 @@ async function createRedwoodApp() { .getActiveSpan() ?.setAttribute('yarn-install-time', Date.now() - yarnInstallStart) } else { - tui.drawText(`${RedwoodStyling.info('ℹ')} Skipped yarn install step`) + if (_isYarnBerryOrNewer) { + tui.drawText(`${RedwoodStyling.info('ℹ')} Skipped yarn install step`) + } } // Generate types diff --git a/packages/create-redwood-app/tests/e2e.test.ts b/packages/create-redwood-app/tests/e2e.test.ts index b0343c02446e..875d4f3ec834 100644 --- a/packages/create-redwood-app/tests/e2e.test.ts +++ b/packages/create-redwood-app/tests/e2e.test.ts @@ -28,12 +28,12 @@ describe('create-redwood-app', () => { --git-init, --git Initialize a git repository [boolean] [default: null] -m, --commit-message Commit message for the initial commit [string] [default: null] - --yarn-install Install node modules. Skip via --no-yarn-install. - [boolean] [default: null] --telemetry Enables sending telemetry events for this create command and all Redwood CLI commands https://telemetry.redwoodjs.com [boolean] [default: true] + --yarn-install Install node modules. Skip via --no-yarn-install. + [boolean] [default: null] Examples: create-redwood-app my-redwood-app