Skip to content

Conversation

okwasniewski
Copy link
Contributor

@okwasniewski okwasniewski commented Nov 17, 2023

Summary:

This PR adds platform-name option to allow for out of tree platforms to leverage init command without (almost) any changes in their repo.

Problem

When initializing project (for OOT platform) with command similar to this: @callstack/react-native-visionos init MyApp current approach would be to pass --template option to specify custom template for this platform. However this requires to maintain separate template (as RN-TvOS is doing: https://www.npmjs.com/package/react-native-template-typescript-tv).

Also CLI has a hardcoded path to retrieve template URI (in createTemplateUri).

Solution

Allow OOT platforms to pass it's name to cli.run() function call which doesn't require to pass any extra flags by user.

Inside of [OutOfTreePlatform]/packages/react-native/cli.js we pass out of tree platform name from the package.json

+ const {name} = require('./package.json');

async function main() {
  if (isNpxRuntime && !process.env.SKIP && currentVersion !== HEAD) {
    try {
      const latest = await getLatestVersion();
      if (latest !== currentVersion) {
        const msg = `
  ${chalk.bold.yellow('WARNING:')} You should run ${chalk.white.bold(
    'npx react-native@latest',
  )} to ensure you're always using the most current version of the CLI. NPX has cached version (${chalk.bold.yellow(
    currentVersion,
  )}) != current release (${chalk.bold.green(latest)})
  `;
        console.warn(msg);
      }
    } catch (_) {
      // Ignore errors, since it's a nice to have warning
    }
  }
+  return cli.run(name);
}

As an extension of this we could later on detect if we are inside of a project and if so just add new platform to this project (instead of initializing new one)

Test Plan:

  • Check if it works
  • Add tests (after the idea is validated)

Checklist

  • Documentation is up to date to reflect these changes.
  • Follows commit message convention described in CONTRIBUTING.md

Copy link
Collaborator

@szymonrybczak szymonrybczak left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Ok, let's do it. This is explicit enough and doesn't require us to make any assumptions about the platform name based on binary name or some node module resolution.

One extra thing I would love to see would be to extend the #### Initializing project with custom template section of docs/init.md with an example or instruction on what to do if you're an OOT platform

@thymikee
Copy link
Member

Thinking about it in the context of your example, if we ported this small diff to the core, we wouldn't need to document it anywhere outside of the code comments

@okwasniewski
Copy link
Contributor Author

@thymikee, @szymonrybczak, @TMisiukiewicz Thanks for review! I've updated the command description and added tests. Let's keep this as an undocumented flag - I will create PR to core after this will be released 👍🏻 That way normal users won't need to pass it

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants