Skip to content

Commit 57ac6a7

Browse files
committed
fix: support specifying --pm yarn and --skip-install together
1 parent c4a04a9 commit 57ac6a7

5 files changed

Lines changed: 55 additions & 11 deletions

File tree

.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/

__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: 1 addition & 1 deletion
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",

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

Lines changed: 7 additions & 4 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,7 +324,7 @@ async function createFromTemplate({
321324
}
322325
} catch (error) {
323326
logger.error(
324-
`\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`,
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',
325328
);
326329
logger.error((error as Error).message as string);
327330
}

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
}

0 commit comments

Comments
 (0)