Skip to content

Commit cc19409

Browse files
authored
fix(init): Fix yarn + skip-install combo causing template copy failure (#2748)
* chore: bump husky * fix: add leading newline to Cocoapods installation error message * fix: separate Cocoapods installation error message to avoid conflict * fix: support specifying --pm yarn and --skip-install together
1 parent d20b58c commit cc19409

File tree

7 files changed

+61
-21
lines changed

7 files changed

+61
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ build/
1313
coverage
1414
*.env
1515
.parcel-cache
16+
.yarn/

.husky/pre-commit

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/usr/bin/env sh
2-
. "$(dirname -- "$0")/_/husky.sh"
3-
41
yarn lint-staged

__e2e__/init.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import execa from 'execa';
34
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
45
import slash from 'slash';
56

@@ -43,6 +44,15 @@ if (process.platform === 'win32') {
4344
templatePath = `file://${templatePath}`;
4445
}
4546

47+
function isYarnAvailable() {
48+
try {
49+
execa.sync('yarn', ['--version'], {stdio: 'pipe'});
50+
return true;
51+
} catch (error) {
52+
return false;
53+
}
54+
}
55+
4656
test('init fails if the directory already exists and --replace-directory false', () => {
4757
fs.mkdirSync(path.join(DIR, PROJECT_NAME));
4858

@@ -152,6 +162,34 @@ test('init skips installation of dependencies with --skip-install', () => {
152162
);
153163
});
154164

165+
test('init supports --pm yarn together with --skip-install', () => {
166+
if (!isYarnAvailable()) {
167+
return;
168+
}
169+
170+
createCustomTemplateFiles();
171+
172+
const {stdout, stderr} = runCLI(DIR, [
173+
'init',
174+
'--template',
175+
templatePath,
176+
PROJECT_NAME,
177+
'--pm',
178+
'yarn',
179+
'--skip-install',
180+
]);
181+
182+
expect(stderr).not.toContain(`Couldn't find the "`);
183+
expect(stdout).toContain('Run instructions');
184+
185+
const dirFiles = fs.readdirSync(path.join(DIR, PROJECT_NAME)).sort();
186+
const expectedFiles = customTemplateCopiedFiles
187+
.filter((file) => !['node_modules', 'package-lock.json'].includes(file))
188+
.concat(['.yarn', '.yarnrc.yml'])
189+
.sort();
190+
expect(dirFiles).toEqual(expectedFiles);
191+
});
192+
155193
// 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.
156194
test.skip('init --platform-name should work for out of tree platform', () => {
157195
createCustomTemplateFiles();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"link-packages": "node ./scripts/linkPackages.js",
1919
"publish": "yarn build-clean-all && yarn install && lerna publish --force-publish",
2020
"publish:next": "yarn build-clean-all && yarn install && lerna publish --force-publish --dist-tag next",
21-
"prepare": "husky install"
21+
"prepare": "husky"
2222
},
2323
"devDependencies": {
2424
"@babel/core": "^7.0.0",
@@ -41,7 +41,7 @@
4141
"eslint-plugin-import": "^2.25.3",
4242
"execa": "^5.0.0",
4343
"fast-glob": "^3.3.2",
44-
"husky": "^8.0.2",
44+
"husky": "^9.1.7",
4545
"jest": "^26.6.2",
4646
"jest-circus": "^26.6.2",
4747
"jest-snapshot-serializer-raw": "^1.1.0",

packages/cli/src/commands/init/init.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ async function createFromTemplate({
281281

282282
if (process.platform === 'darwin') {
283283
const installPodsValue = String(installCocoaPods);
284-
const reactNativePath = path.dirname(
285-
require.resolve('react-native', {paths: [projectDirectory]}),
286-
);
287284

288285
try {
289286
if (installPodsValue === 'true') {
287+
const reactNativePath = path.dirname(
288+
require.resolve('react-native', {paths: [projectDirectory]}),
289+
);
290290
didInstallPods = true;
291291
await runCodegen({
292292
root: projectDirectory,
@@ -308,6 +308,9 @@ async function createFromTemplate({
308308
didInstallPods = installCocoapods;
309309

310310
if (installCocoapods) {
311+
const reactNativePath = path.dirname(
312+
require.resolve('react-native', {paths: [projectDirectory]}),
313+
);
311314
await runCodegen({
312315
root: projectDirectory,
313316
platform: 'ios',
@@ -321,10 +324,9 @@ async function createFromTemplate({
321324
}
322325
} catch (error) {
323326
logger.error(
324-
`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: ${
325-
(error as Error).message as string
326-
}\n`,
327+
'\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',
327328
);
329+
logger.error((error as Error).message as string);
328330
}
329331
}
330332
} else {

packages/cli/src/commands/init/template.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ export async function installTemplatePackage(
3737
};
3838

3939
// 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
40-
executeCommand(
40+
await executeCommand(
4141
'yarn',
4242
['config', 'set', 'nodeLinker', 'node-modules'],
4343
options,
4444
);
4545

46-
executeCommand(
46+
await executeCommand(
4747
'yarn',
4848
['config', 'set', 'nmHoistingLimits', 'workspaces'],
4949
options,
5050
);
5151

52-
for (let key in yarnConfigOptions) {
53-
if (yarnConfigOptions.hasOwnProperty(key)) {
54-
let value = yarnConfigOptions[key];
55-
executeCommand('yarn', ['config', 'set', key, value], options);
52+
if (yarnConfigOptions) {
53+
for (let key in yarnConfigOptions) {
54+
if (Object.prototype.hasOwnProperty.call(yarnConfigOptions, key)) {
55+
const value = yarnConfigOptions[key];
56+
await executeCommand('yarn', ['config', 'set', key, value], options);
57+
}
5658
}
5759
}
5860
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,10 +5609,10 @@ humanize-ms@^1.2.1:
56095609
dependencies:
56105610
ms "^2.0.0"
56115611

5612-
husky@^8.0.2:
5613-
version "8.0.2"
5614-
resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.2.tgz#5816a60db02650f1f22c8b69b928fd6bcd77a236"
5615-
integrity sha512-Tkv80jtvbnkK3mYWxPZePGFpQ/tT3HNSs/sasF9P2YfkMezDl3ON37YN6jUUI4eTg5LcyVynlb6r4eyvOmspvg==
5612+
husky@^9.1.7:
5613+
version "9.1.7"
5614+
resolved "https://registry.yarnpkg.com/husky/-/husky-9.1.7.tgz#d46a38035d101b46a70456a850ff4201344c0b2d"
5615+
integrity sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==
56165616

56175617
iconv-lite@0.4.24, iconv-lite@^0.4.24:
56185618
version "0.4.24"

0 commit comments

Comments
 (0)