From 03ea65ebd954c12967996cecaa1ad7e77471ff5c Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Thu, 6 Apr 2017 22:41:38 -0700 Subject: [PATCH 1/2] Fix cryptic error when packageName cannot be found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of throwing an “Cannot read property 'replace' of undefined” error when the package name cannot be found (presumably because the wrong manifest was fetched) throw one that makes the error clear and identifies the manifest. --- local-cli/core/android/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/local-cli/core/android/index.js b/local-cli/core/android/index.js index 086fd86a6d82..49bbe7d9abfd 100644 --- a/local-cli/core/android/index.js +++ b/local-cli/core/android/index.js @@ -38,6 +38,11 @@ exports.projectConfig = function projectConfigAndroid(folder, userConfig) { const manifest = readManifest(manifestPath); const packageName = userConfig.packageName || getPackageName(manifest); + + if (!packageName) { + throw new Error(`Package name not found in ${manifestPath}`); + } + const packageFolder = userConfig.packageFolder || packageName.replace(/\./g, path.sep); From 5ccb88297493d9f122cd0fb2dff92d906a3781b0 Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Thu, 6 Apr 2017 23:34:45 -0700 Subject: [PATCH 2/2] Add `manifestPath` userConfig. `react-native link` often fails due to the wrong manifest being used when you use a debug manifest. --- local-cli/core/__fixtures__/android.js | 19 +++++++++++++++++++ .../files/AndroidManifest-debug.xml | 3 +++ .../android/getProjectConfig.spec.js | 13 +++++++++++++ local-cli/core/android/index.js | 8 ++++++-- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 local-cli/core/__fixtures__/files/AndroidManifest-debug.xml diff --git a/local-cli/core/__fixtures__/android.js b/local-cli/core/__fixtures__/android.js index ebecf9b8c902..d7779709845f 100644 --- a/local-cli/core/__fixtures__/android.js +++ b/local-cli/core/__fixtures__/android.js @@ -20,6 +20,25 @@ exports.valid = { }, }; +exports.userConfigManifest = { + src: { + main: { + 'AndroidManifest.xml': manifest, + com: { + some: { + example: { + 'Main.java': mainJavaClass, + 'ReactPackage.java': fs.readFileSync(path.join(__dirname, './files/ReactPackage.java')), + }, + }, + }, + }, + debug: { + 'AndroidManifest.xml': fs.readFileSync(path.join(__dirname, './files/AndroidManifest-debug.xml')), + }, + }, +}; + exports.corrupted = { src: { 'AndroidManifest.xml': manifest, diff --git a/local-cli/core/__fixtures__/files/AndroidManifest-debug.xml b/local-cli/core/__fixtures__/files/AndroidManifest-debug.xml new file mode 100644 index 000000000000..0c29eab6721a --- /dev/null +++ b/local-cli/core/__fixtures__/files/AndroidManifest-debug.xml @@ -0,0 +1,3 @@ + + diff --git a/local-cli/core/__tests__/android/getProjectConfig.spec.js b/local-cli/core/__tests__/android/getProjectConfig.spec.js index 3c0b2f79d2c2..6a5d3d449a55 100644 --- a/local-cli/core/__tests__/android/getProjectConfig.spec.js +++ b/local-cli/core/__tests__/android/getProjectConfig.spec.js @@ -15,6 +15,9 @@ describe('android::getProjectConfig', () => { flat: { android: mocks.valid, }, + multiple: { + android: mocks.userConfigManifest, + }, noManifest: { android: {}, }, @@ -43,6 +46,16 @@ describe('android::getProjectConfig', () => { expect(getProjectConfig(folder, userConfig)).not.toBe(null); expect(typeof getProjectConfig(folder, userConfig)).toBe('object'); }); + + it('multiple', () => { + const userConfig = { + manifestPath: 'src/main/AndroidManifest.xml' + }; + const folder = 'multiple'; + + expect(getProjectConfig(folder, userConfig)).not.toBe(null); + expect(typeof getProjectConfig(folder, userConfig)).toBe('object'); + }); }); it('should return `null` if android project was not found', () => { diff --git a/local-cli/core/android/index.js b/local-cli/core/android/index.js index 49bbe7d9abfd..cc229d298f4f 100644 --- a/local-cli/core/android/index.js +++ b/local-cli/core/android/index.js @@ -29,7 +29,9 @@ exports.projectConfig = function projectConfigAndroid(folder, userConfig) { const sourceDir = path.join(folder, src); const isFlat = sourceDir.indexOf('app') === -1; - const manifestPath = findManifest(sourceDir); + const manifestPath = userConfig.manifestPath + ? path.join(sourceDir, userConfig.manifestPath) + : findManifest(sourceDir); if (!manifestPath) { return null; @@ -97,7 +99,9 @@ exports.dependencyConfig = function dependencyConfigAndroid(folder, userConfig) } const sourceDir = path.join(folder, src); - const manifestPath = findManifest(sourceDir); + const manifestPath = userConfig.manifestPath + ? path.join(sourceDir, userConfig.manifestPath) + : findManifest(sourceDir); if (!manifestPath) { return null;