Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why doesn't this CLI let me create a new project in an existing, empty directory? #1418

Closed
Pika-Pool opened this issue May 20, 2021 · 6 comments
Labels
question Further information is requested stale

Comments

@Pika-Pool
Copy link

Ask your Question

When i run this command: npx react-native init --directory . --skip-install awesomeproject, I get the error error Error occurred while trying to create project directory. Note that the '.' directory is completely empty

I checked the code and it seems the cli only checks if the directory already exists, and if it does, error is thrown here

But in create-react-app, you can just specify the directory, the command works, as long as package.json does not exist, even if the directory does.

Is there are particular reason react-native does this? If not, I think the CRA way is better and I would love to implement it in this CLI

@Pika-Pool Pika-Pool added the question Further information is requested label May 20, 2021
@thymikee
Copy link
Member

It's because we get generate Android/iOS package names based on the project name. Feel free to add such a feature to read it from the directory if the project name param is a dot :)

@Pika-Pool
Copy link
Author

Pika-Pool commented May 20, 2021

ok, I'll work on this then. It'll be great if you could clarify a few things for me

  • whats the diff between /package/cli/src/commands/init.ts and /package/cli/src/commands/initCompat.ts?
  • in what order are these files executed?

This is the workflow in my mind.

  • There are 3 scenarios for the command line arguments:

    • projectName is given, --directory is not:
      • treat projectName as a directory path, so something like this:
      const directoryName = path.resolve(projectName);
      projectName = path.basename(root);
    • projectName and --directory both are given:
       const directoryName = path.resolve(directory);
       // projectName remains same as the command line argument passed
       // projectName is also already validated against a regex in the existing code
    • projectName is not given:
      • this already throws an error, so no changes
  • Now, to check if the directoryName is valid, CRA uses function isSafeToCreateProjectIn, which uses a list of valid files that could be already present in the directory.

    • i couldn't find any other conflicting files for the react-native cli, please mention them if there are any

Pika-Pool added a commit to Pika-Pool/cli that referenced this issue May 20, 2021
Refs: react-native-community#1418

* init command doesn't create a new project if directory already exists
* so let user pass a directory path as projectName
* or using --directory option
* if directory doesn't contain any conflicting files, create the project
Pika-Pool added a commit to Pika-Pool/cli that referenced this issue May 20, 2021
Refs: react-native-community#1418

* init command doesn't create a new project if directory already exists
* so let user pass a directory path as projectName
* or using --directory option
* if directory doesn't contain any conflicting files, create the project
@Pika-Pool
Copy link
Author

Please review this commit: 131d0be
I do not know jest, but I'll learn it in a day or two and can write the necessary tests if that's ok.
The program does work correctly

@thymikee
Copy link
Member

Create a PR please :)

@Pika-Pool
Copy link
Author

I'm stuck at writing tests for this. To validate a directory, the code checks the local file system. So should I use something like mock-fs for this?

@github-actions
Copy link

There hasn't been any activity on this issue in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

2 participants