diff --git a/.gitignore b/.gitignore index 86a849d07..a028a7858 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ build/ coverage *.env .parcel-cache +.yarn/ \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit index 5a182ef10..372362317 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - yarn lint-staged diff --git a/__e2e__/init.test.ts b/__e2e__/init.test.ts index 8d8564e13..b337056ad 100644 --- a/__e2e__/init.test.ts +++ b/__e2e__/init.test.ts @@ -1,5 +1,6 @@ import fs from 'fs'; import path from 'path'; +import execa from 'execa'; import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers'; import slash from 'slash'; @@ -43,6 +44,15 @@ if (process.platform === 'win32') { templatePath = `file://${templatePath}`; } +function isYarnAvailable() { + try { + execa.sync('yarn', ['--version'], {stdio: 'pipe'}); + return true; + } catch (error) { + return false; + } +} + test('init fails if the directory already exists and --replace-directory false', () => { fs.mkdirSync(path.join(DIR, PROJECT_NAME)); @@ -152,6 +162,34 @@ test('init skips installation of dependencies with --skip-install', () => { ); }); +test('init supports --pm yarn together with --skip-install', () => { + if (!isYarnAvailable()) { + return; + } + + createCustomTemplateFiles(); + + const {stdout, stderr} = runCLI(DIR, [ + 'init', + '--template', + templatePath, + PROJECT_NAME, + '--pm', + 'yarn', + '--skip-install', + ]); + + expect(stderr).not.toContain(`Couldn't find the "`); + expect(stdout).toContain('Run instructions'); + + const dirFiles = fs.readdirSync(path.join(DIR, PROJECT_NAME)).sort(); + const expectedFiles = customTemplateCopiedFiles + .filter((file) => !['node_modules', 'package-lock.json'].includes(file)) + .concat(['.yarn', '.yarnrc.yml']) + .sort(); + expect(dirFiles).toEqual(expectedFiles); +}); + // react-native-macos stopped shipping `template.config.js` for 0.75, so this test is disabled. in future releases we should re-enable once `template.config.js` will be there again. test.skip('init --platform-name should work for out of tree platform', () => { createCustomTemplateFiles(); diff --git a/package.json b/package.json index 30bfa7a88..9c2769c63 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "link-packages": "node ./scripts/linkPackages.js", "publish": "yarn build-clean-all && yarn install && lerna publish --force-publish", "publish:next": "yarn build-clean-all && yarn install && lerna publish --force-publish --dist-tag next", - "prepare": "husky install" + "prepare": "husky" }, "devDependencies": { "@babel/core": "^7.0.0", @@ -41,7 +41,7 @@ "eslint-plugin-import": "^2.25.3", "execa": "^5.0.0", "fast-glob": "^3.3.2", - "husky": "^8.0.2", + "husky": "^9.1.7", "jest": "^26.6.2", "jest-circus": "^26.6.2", "jest-snapshot-serializer-raw": "^1.1.0", diff --git a/packages/cli/src/commands/init/init.ts b/packages/cli/src/commands/init/init.ts index 954afb0c7..20be2d731 100644 --- a/packages/cli/src/commands/init/init.ts +++ b/packages/cli/src/commands/init/init.ts @@ -281,12 +281,12 @@ async function createFromTemplate({ if (process.platform === 'darwin') { const installPodsValue = String(installCocoaPods); - const reactNativePath = path.dirname( - require.resolve('react-native', {paths: [projectDirectory]}), - ); try { if (installPodsValue === 'true') { + const reactNativePath = path.dirname( + require.resolve('react-native', {paths: [projectDirectory]}), + ); didInstallPods = true; await runCodegen({ root: projectDirectory, @@ -308,6 +308,9 @@ async function createFromTemplate({ didInstallPods = installCocoapods; if (installCocoapods) { + const reactNativePath = path.dirname( + require.resolve('react-native', {paths: [projectDirectory]}), + ); await runCodegen({ root: projectDirectory, platform: 'ios', @@ -321,10 +324,9 @@ async function createFromTemplate({ } } catch (error) { logger.error( - `Installing Cocoapods failed. This doesn't affect project initialization and you can safely proceed. However, you will need to install Cocoapods manually when running iOS, follow additional steps in "Run instructions for iOS" section.\n\nError: ${ - (error as Error).message as string - }\n`, + '\nInstalling Cocoapods failed. This doesn\'t affect project initialization and you can safely proceed. However, you will need to install Cocoapods manually when running iOS, follow additional steps in "Run instructions for iOS" section.\n', ); + logger.error((error as Error).message as string); } } } else { diff --git a/packages/cli/src/commands/init/template.ts b/packages/cli/src/commands/init/template.ts index dcc21fd12..7ec4b9841 100644 --- a/packages/cli/src/commands/init/template.ts +++ b/packages/cli/src/commands/init/template.ts @@ -37,22 +37,24 @@ export async function installTemplatePackage( }; // React Native doesn't support PnP, so we need to set nodeLinker to node-modules. Read more here: https://github.com/react-native-community/cli/issues/27#issuecomment-1772626767 - executeCommand( + await executeCommand( 'yarn', ['config', 'set', 'nodeLinker', 'node-modules'], options, ); - executeCommand( + await executeCommand( 'yarn', ['config', 'set', 'nmHoistingLimits', 'workspaces'], options, ); - for (let key in yarnConfigOptions) { - if (yarnConfigOptions.hasOwnProperty(key)) { - let value = yarnConfigOptions[key]; - executeCommand('yarn', ['config', 'set', key, value], options); + if (yarnConfigOptions) { + for (let key in yarnConfigOptions) { + if (Object.prototype.hasOwnProperty.call(yarnConfigOptions, key)) { + const value = yarnConfigOptions[key]; + await executeCommand('yarn', ['config', 'set', key, value], options); + } } } } diff --git a/yarn.lock b/yarn.lock index 1a746bc1c..09cccd258 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5609,10 +5609,10 @@ humanize-ms@^1.2.1: dependencies: ms "^2.0.0" -husky@^8.0.2: - version "8.0.2" - resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.2.tgz#5816a60db02650f1f22c8b69b928fd6bcd77a236" - integrity sha512-Tkv80jtvbnkK3mYWxPZePGFpQ/tT3HNSs/sasF9P2YfkMezDl3ON37YN6jUUI4eTg5LcyVynlb6r4eyvOmspvg== +husky@^9.1.7: + version "9.1.7" + resolved "https://registry.yarnpkg.com/husky/-/husky-9.1.7.tgz#d46a38035d101b46a70456a850ff4201344c0b2d" + integrity sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA== iconv-lite@0.4.24, iconv-lite@^0.4.24: version "0.4.24"