frostney / react-native-create-library

:notebook: Command line tool to create a React Native library with a single command
MIT License
1.45k stars 113 forks source link

Adds Option To Generate ReactNative Example project #57

Closed thorbenprimke closed 6 years ago

thorbenprimke commented 6 years ago

Adds the option --generate-example that generate an example project and links the newly created to it.

This addresses #28.

Due to react-native init project_name not executing in a folder that already contains a package.json file, the example is generated first, then all the files are created and last the library is linked to the example.

thorbenprimke commented 6 years ago

@maicki - could you give this a review? It's good to go.

thorbenprimke commented 6 years ago

Verified that both with and without generate parameter it works as expected.

pvh commented 6 years ago

This is a really great feature, but on a totally fresh install it causes Metro to get terribly confused.

I see an "Ambiguous Module Resolution Error" caused by metro picking up both the package.json in the example/package.json directory but ALSO the same package.json a second time when it's installed in node_modules/$MODULE_NAME/example!

I've worked around this by moving the example app out of the tree, but I'm not really sure what a good way to solve this would be at the tool level.

thorbenprimke commented 6 years ago

@pvh - Hmm. I did not run into this and we always have an example folder inside a lib project (for testing purposes and to provide usage examples of a lib).

What are the commands that you are running?

Perhaps we could make the path for the example app configurable. The reason it's inside the project is because one would ideally push both the lib and example to a single repo on github.

maxme commented 6 years ago

This is a really great feature, but on a totally fresh install it causes Metro to get terribly confused.

I see an "Ambiguous Module Resolution Error" caused by metro picking up both the package.json in the example/package.json directory but ALSO the same package.json a second time when it's installed in node_modules/$MODULE_NAME/example!

I get the same problem, steps to reproduce the issue:

$ react-native-create-library --generate-example mytest
$ cd mytest/
$ yarn install
$ cd example
$ react-native run-android

Metro bundler fails with the error:

Loading dependency graph, done.
error: bundling failed: ambiguous resolution: module `/private/tmp/mytest/example/index.js` tries to require `react-native`, but there are several files providing this module. You can delete or fix them:

  * `/private/tmp/mytest/example/node_modules/react-native-mytest/example/node_modules/react-native/package.json`
  * `/private/tmp/mytest/example/node_modules/react-native/package.json`
maxme commented 6 years ago

I tried to add a metro bundler configuration file to exclude the directory mytest/example/node_modules/ from the resolution map but I ran into other issues after that. I finally moved the library in its own directory and now I have something like:

.
├── example
│   ├── android
│   ├── ios
│   ├── node_modules
│   ├── App.js
│   ├── app.json
│   ├── index.js
│   └── package.json
├── library
│   ├── android
│   ├── ios
│   ├── node_modules
│   ├── windows
│   ├── RNMytest.podspec
│   ├── index.js
│   └── package.json
└── README.md
thorbenprimke commented 6 years ago

@maxme - thanks for the steps. I'll try to repro this later.

I see where my workflow differs. Instead of your last command react-native run-android, I would normally just do

yarn install
yarn start

which theoretically should do the same.

And I built out of AS or XCode.

maxme commented 6 years ago

FWIW, I also tried:

react-native-create-library --generate-example mytest \
 && cd mytest/ \
 && yarn install \
 && cd example \
 && pushd android \
 && ./gradlew installDebug \
 && popd \
 && adb shell am start -n com.example/.MainActivity \
 && yarn start

And it fails with the same error.

I'm far from being an RN veteran so I'm not sure what's the best way to setup the library and example directories. Having the example directory as a library sub directory seems to be a source of issues.

iamcxa commented 6 years ago

that's great! it will save lot's time when making a native module at the beginning. when will this merged version will be sent to npm? because now still has no this feature in npm version yet.