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

fix(android): fix JS error when RN version is a github URL #38

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
56 changes: 56 additions & 0 deletions __tests__/__snapshots__/android-kotlin.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,62 @@ class TestModuleModule(reactContext: ReactApplicationContext) : ReactContextBase
"
`;

exports[`Android/Kotlin: Combined creates a TemplateModule.kt with github RN version 1`] = `
"// Created by react-native-create-bridge

package com.testapp.testmodule

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.WritableMap
import com.facebook.react.modules.core.DeviceEventManagerModule

import java.util.Map

class TestModuleModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

init {
// Here we're saving the context we passed into the constructor to a variable so we can emit events
// https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module
reactContext = context
}

override fun getName(): String {
// Tell React the name of the module
// https://facebook.github.io/react-native/docs/native-components-android.html#1-create-the-viewmanager-subclass
return REACT_CLASS
}

override fun getConstants(): Map<String, Any>? {
// Export any constants to be used in your native module
// https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module
val reactConstants = Map<String, Any>()
constants.put(\\"EXAMPLE_CONSTANT\\", \\"example\\")

return constants
}

@ReactMethod
fun exampleMethod () {
// An example native method that you will expose to React
// https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module
}

companion object {
val REACT_CLASS = \\"TestModule\\"
private var reactContext: ReactApplicationContext = null

private fun emitDeviceEvent(eventName: String, eventData: WritableMap?) {
// A method for emitting from the native side to JS
// https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit(eventName, eventData)
}
}
}
"
`;

exports[`Android/Kotlin: Combined creates a TemplatePackage.kt 1`] = `
"// Created by react-native-create-bridge

Expand Down
12 changes: 12 additions & 0 deletions __tests__/android-kotlin.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ describe('Android/Kotlin: Combined', () => {
app: 'testapp',
rnVersion: '0.47.2',
};
const githubConfig = {
templateName,
packageName: templateName.toLowerCase(),
app: 'testapp',
rnVersion: 'https://github.com/facebook/react-native#master',
};

it('creates a TemplateManager.kt', async () => {
const fileData = await readFile('TemplateManager.kt', readDirPath);
Expand All @@ -93,4 +99,10 @@ describe('Android/Kotlin: Combined', () => {
const parsedFile = parseFile(fileData, config);
expect(parsedFile).toMatchSnapshot();
});

it('creates a TemplateModule.kt with github RN version', async () => {
const fileData = await readFile('TemplateModule.kt', readDirPath);
const parsedFile = parseFile(fileData, githubConfig);
expect(parsedFile).toMatchSnapshot();
});
});
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,34 @@
"build:watch": "babel -w src --out-dir build",
"start": "node build/index.js",
"precommit": "lint-staged",
"package:dev":
"npm unlink react-native-create-bridge && rm -rf node_modules && yarn && npm run build && npm link"
"package:dev": "npm unlink react-native-create-bridge && rm -rf node_modules && yarn && npm run build && npm link"
},
"lint-staged": {
"src/*.js": ["prettier --write", "git add"]
"src/*.js": [
"prettier --write",
"git add"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/peggyrayzis/react-native-create-bridge.git"
},
"keywords": ["react", "native"],
"keywords": [
"react",
"native"
],
"author": "Peggy Rayzis & Kurt Kemple",
"license": "ISC",
"bugs": {
"url": "https://github.com/peggyrayzis/react-native-create-bridge/issues"
},
"homepage":
"https://github.com/peggyrayzis/react-native-create-bridge#readme",
"homepage": "https://github.com/peggyrayzis/react-native-create-bridge#readme",
"dependencies": {
"babel-runtime": "^6.20.0",
"compare-versions": "^3.0.1",
"inquirer": "^2.0.0",
"is-valid-path": "^0.1.1",
"jest-cli": "^21.2.1",
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this necessary?

Copy link
Author

Choose a reason for hiding this comment

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

It was needed in my environment.

"log-symbols": "^1.0.2",
"mkdirp-promise": "^5.0.0",
"mz": "^2.6.0"
Expand Down
9 changes: 8 additions & 1 deletion src/file-operations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require("path");
const fs = require("mz/fs");
const compareVersions = require("compare-versions");
const validate = require("compare-versions");
Copy link
Collaborator

Choose a reason for hiding this comment

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

@dlowder-salesforce There looks to be a duplicate import here. This package is being imported above.


const pkg = require(path.join(process.cwd(), "package.json"));

Expand All @@ -17,9 +18,15 @@ function readFile(file, readDirPath) {
function parseFile(fileData, { templateName, packageName, app, rnVersion }) {
let kotlinPackage;
let javaPackage;
var version = rnVersion;
try {
validate(rnVersion);
} catch (e) {
version = null;
}

// TODO: figure out a better way to handle one off breaking changes
if (rnVersion && compareVersions(rnVersion, "0.47.2") < 0) {
if (version && compareVersions(version, "0.47.2") < 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@dlowder-salesforce I am realizing that your version check just validates a github repo as a valid source, however you don't get the actual version from it. How will compareVersions work since I don't see support for github repos in the package.

Copy link
Author

Choose a reason for hiding this comment

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

Actually a github repo string will fail the validation check, so version will be set to null in that case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see...so I am actually wondering if this PR will cover this issue too and just needs a test https://github.com/peggyrayzis/react-native-create-bridge/pull/37/files

Copy link
Author

Choose a reason for hiding this comment

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

Yeah that looks like a similar fix

Copy link
Collaborator

Choose a reason for hiding this comment

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

@dlowder-salesforce Since there are redundancies Im going to add add you test into the other PR and make sure it is still working as expected. I'll add you as a reviewer when it's done.

Copy link
Author

Choose a reason for hiding this comment

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

@jarretmoses sounds good, I'll close this PR then

kotlinPackage = `
override fun createJSModules(): List<Class<out JavaScriptModule>> {
return emptyList()
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ function init() {
.then(result => {
const { environment, bridgeType, templateName, jsPath } = result;

const templateFolder = bridgeType.length > 1
? "combined"
: bridgeType[0] === "Native Module" ? "modules" : "ui-components";
const templateFolder =
bridgeType.length > 1
? "combined"
: bridgeType[0] === "Native Module" ? "modules" : "ui-components";

const promises = environment.map(env =>
environmentMap[env](templateName, templateFolder)
Expand Down
Loading