Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion __e2e__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import fs from 'fs';
import path from 'path';
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {
runCLI,
getTempDirectory,
cleanup,
writeFiles,
spawnScript,
} from '../jest/helpers';
import slash from 'slash';

const DIR = getTempDirectory('command-init');
Expand Down Expand Up @@ -34,6 +40,7 @@ const customTemplateCopiedFiles = [
beforeEach(() => {
cleanup(DIR);
writeFiles(DIR, {});
spawnScript('corepack', ['enable'], {cwd: DIR});
});
afterEach(() => {
cleanup(DIR);
Expand Down
39 changes: 37 additions & 2 deletions __e2e__/root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,47 @@ beforeAll(() => {
writeFiles(DIR, {});

// Initialise React Native project
runCLI(DIR, ['init', 'TestProject', `--pm`, 'npm', `--install-pods`]);
runCLI(DIR, ['init', 'TestProject', `--install-pods`]);

// Link CLI to the project
spawnScript('yarn', ['link', __dirname, '--all'], {
console.log(__dirname);
const filesOutput = spawnScript('ls', ['-la', path.dirname(__dirname)], {
cwd: path.join(DIR, 'TestProject'),
});
console.log(filesOutput.stdout);

const linkPackagesOutput = spawnScript('yarn', ['link-packages'], {
cwd: path.dirname(__dirname),
});
console.log(linkPackagesOutput.stdout);

const yarnVersionOutput = spawnScript('yarn', ['--version'], {
cwd: path.join(DIR, 'TestProject'),
});
console.log(yarnVersionOutput.stdout);

const linkingOutput = spawnScript(
'yarn',
['link', path.dirname(__dirname), '--all'],
{
cwd: path.join(DIR, 'TestProject'),
},
);
console.log(linkingOutput.stdout);

const yarnWHy = spawnScript('yarn', ['why', '@react-native-community/cli'], {
cwd: path.join(DIR, 'TestProject'),
});
console.log(yarnWHy.stdout);

const configOutput = spawnScript(
'npx',
['@react-native-community/cli', 'config'],
{
cwd: path.join(DIR, 'TestProject'),
},
);
console.log(configOutput.stdout);
});

afterAll(() => {
Expand Down
32 changes: 24 additions & 8 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ const bumpYarnVersion = async (root: string) => {
silent: !logger.isVerbose(),
});

// 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
await executeCommand(
'yarn',
['config', 'set', 'nodeLinker', 'node-modules'],
{root, silent: !logger.isVerbose()},
);
setNodeModulesLinker(root);
}
} catch (e) {
logger.debug(e as string);
Expand Down Expand Up @@ -266,8 +261,12 @@ async function createFromTemplate({
packageName,
});

if (packageManager === 'yarn' && shouldBumpYarnVersion) {
await bumpYarnVersion(projectDirectory);
if (packageManager === 'yarn') {
setNodeModulesLinker(projectDirectory);

if (shouldBumpYarnVersion) {
await bumpYarnVersion(projectDirectory);
}
}

loader.succeed();
Expand Down Expand Up @@ -543,3 +542,20 @@ export default (async function initialize(
showPodsInstructions: !didInstallPods,
});
});

export async function setNodeModulesLinker(root: string) {
const options = {root, silent: !logger.isVerbose()};

// 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
await executeCommand(
'yarn',
['config', 'set', 'nodeLinker', 'node-modules'],
{root, silent: !logger.isVerbose()},
);

await executeCommand(
'yarn',
['config', 'set', 'nmHoistingLimits', 'workspaces'],
options,
);
}
27 changes: 7 additions & 20 deletions packages/cli/src/commands/init/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import copyFiles from '../../tools/copyFiles';
import replacePathSepForRegex from '../../tools/replacePathSepForRegex';
import fs from 'fs';
import chalk from 'chalk';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import {executeCommand} from '../../tools/executeCommand';
import {setNodeModulesLinker} from './init';

export type TemplateConfig = {
placeholderName: string;
Expand All @@ -30,29 +30,16 @@ export async function installTemplatePackage(
root,
});

if (packageManager === 'yarn' && getYarnVersionIfAvailable() !== null) {
const options = {
root,
silent: true,
};

// 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(
'yarn',
['config', 'set', 'nodeLinker', 'node-modules'],
options,
);

executeCommand(
'yarn',
['config', 'set', 'nmHoistingLimits', 'workspaces'],
options,
);
if (packageManager === 'yarn') {
await setNodeModulesLinker(root);

for (let key in yarnConfigOptions) {
if (yarnConfigOptions.hasOwnProperty(key)) {
let value = yarnConfigOptions[key];
executeCommand('yarn', ['config', 'set', key, value], options);
executeCommand('yarn', ['config', 'set', key, value], {
root,
silent: !logger.isVerbose(),
});
}
}
}
Expand Down