Skip to content
Merged
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
26 changes: 26 additions & 0 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,29 @@ test('init uses npm as the package manager with --npm', () => {
expect(fs.existsSync(path.join(initDirPath, file))).toBe(true);
});
});

test('init --platform-name should work for out of tree platform', () => {
createCustomTemplateFiles();
const outOfTreePlatformName = 'react-native-macos';

const {stdout} = runCLI(DIR, [
'init',
PROJECT_NAME,
'--platform-name',
outOfTreePlatformName,
'--skip-install',
'--verbose',
]);

expect(stdout).toContain('Run instructions');
expect(stdout).toContain(
`Installing template from ${outOfTreePlatformName}@latest`,
);

// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toContain('custom');

let dirFiles = fs.readdirSync(path.join(DIR, PROJECT_NAME));

expect(dirFiles.length).toBeGreaterThan(0);
});
5 changes: 5 additions & 0 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ export default {
description:
'Inits a project with a custom package name (Android) and bundle ID (iOS), e.g. com.example.app',
},
{
name: '--platform-name <string>',
description:
'Name of out of tree platform to be used for ex. react-native-macos. This flag is optional as it should be passed automatically by out of tree platform. It needs to match the name of the platform declared in package.json',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @Saadnajmi

Note that macOS and Windows both have separate init packages right now (with the windows one in particular growing quite complicated). I worry naming react-native-macOS here might be jumping ahead a bit, until that init flow has changed (unless it already has).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The init flow stays the same. While working on a new platform (visionos) we want to make things right and easy for any other platform to follow. Today this particular flag is quite virtual, as it's passed from the programmatic CLI usage in core. It's still in flux and once we're more firm on the design, we'll likely contribute some PoC to macOS and hopefully Windows too.

},
],
};
8 changes: 6 additions & 2 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Options = {
version?: string;
packageName?: string;
installPods?: string | boolean;
platformName?: string;
};

interface TemplateOptions {
Expand Down Expand Up @@ -261,14 +262,17 @@ function createTemplateUri(options: Options, version: string): string {
const isTypescriptTemplate =
options.template === 'react-native-template-typescript';

// This allows to correctly retrieve template uri for out of tree platforms.
const platform = options.platformName || 'react-native';

if (isTypescriptTemplate) {
logger.warn(
"Ignoring custom template: 'react-native-template-typescript'. Starting from React Native v0.71 TypeScript is used by default.",
);
return 'react-native';
return platform;
}

return options.template || `react-native@${version}`;
return options.template || `${platform}@${version}`;
}

//remove quotes from object keys to match the linter rules of the template
Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ function attachCommand<C extends Command<boolean>>(
}
}

async function run() {
// Platform name is optional argument passed by out of tree platforms.
async function run(platformName?: string) {
try {
await setupAndRun();
await setupAndRun(platformName);
} catch (e) {
handleError(e as Error);
}
}

async function setupAndRun() {
async function setupAndRun(platformName?: string) {
// Commander is not available yet

// when we run `config`, we don't want to output anything to the console. We
Expand Down Expand Up @@ -210,7 +211,14 @@ async function setupAndRun() {
}
}

program.parse(process.argv);
const argv = [...process.argv];

// If out of tree platform specifices custom platform name by passing it to , we need to pass it to argv array.
if (platformName) {
argv.push('--platform-name', platformName);
}

program.parse(argv);
}

const bin = require.resolve('./bin');
Expand Down