Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
[expo-cli] Fix 'generate-module' to support latest expo-module-templa…
Browse files Browse the repository at this point in the history
…te (#2510)

* [expo-cli] Fix 'generate-module' to support latest 'expo-module-template@8.4.0'

* Updated CHANGELOG
  • Loading branch information
barthap authored Aug 28, 2020
1 parent 3a68086 commit 800ce3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is the log of notable changes to Expo CLI and related packages.
### 🐛 Bug fixes

- [expo-cli] expo upload:android - fix `--use-submission-service` not resulting in non-zero exit code when upload fails ([#2530](https://github.com/expo/expo-cli/pull/2530) by [@mymattcarroll](https://github.com/mymattcarroll))
- [expo-cli] Fix `generate-module` to support latest `expo-module-template` ([#2510](https://github.com/expo/expo-cli/pull/2510) by [@barthap](https://github.com/barthap))

### 📦 Packages updated

Expand Down
45 changes: 28 additions & 17 deletions packages/expo-cli/src/commands/generate-module/configureModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fse from 'fs-extra';
import walkSync from 'klaw-sync';
import path from 'path';

import CommandError from '../../CommandError';
import { ModuleConfiguration } from './ModuleConfiguration';

const asyncForEach = async <T>(
Expand Down Expand Up @@ -149,6 +150,28 @@ async function configureIOS(
);
}

/**
* Gets path to Android source base dir: android/src/main/[java|kotlin]
* Defaults to Java path if both exist
* @param androidPath path do module android/ directory
* @throws INVALID_TEMPLATE if none exist
*/
function findAndroidSourceDir(androidPath: string) {
const androidSrcPathBase = path.join(androidPath, 'src', 'main');

const javaExists = fse.pathExistsSync(path.join(androidSrcPathBase, 'java'));
const kotlinExists = fse.pathExistsSync(path.join(androidSrcPathBase, 'kotlin'));

if (!javaExists && !kotlinExists) {
throw new CommandError(
'INVALID_TEMPLATE',
`Invalid template. Android source directory not found: ${androidSrcPathBase}/[java|kotlin]`
);
}

return path.join(androidSrcPathBase, javaExists ? 'java' : 'kotlin');
}

/**
* Prepares Android part, mainly by renaming all files and template words in files
* Sets all versions in Gradle to 1.0.0
Expand All @@ -160,22 +183,10 @@ async function configureAndroid(
{ javaPackage, jsPackageName, viewManager }: ModuleConfiguration
) {
const androidPath = path.join(modulePath, 'android');
const sourceFilesPath = path.join(
androidPath,
'src',
'main',
'kotlin',
'expo',
'modules',
'template'
);
const destinationFilesPath = path.join(
androidPath,
'src',
'main',
'kotlin',
...javaPackage.split('.')
);
const androidSrcPath = findAndroidSourceDir(androidPath);

const sourceFilesPath = path.join(androidSrcPath, 'expo', 'modules', 'template');
const destinationFilesPath = path.join(androidSrcPath, ...javaPackage.split('.'));

// remove ViewManager from template
if (!viewManager) {
Expand All @@ -194,7 +205,7 @@ async function configureAndroid(
// Remove leaf directory content
await fse.remove(sourceFilesPath);
// Cleanup all empty subdirs up to provided rootDir
await removeUponEmptyOrOnlyEmptySubdirs(path.join(androidPath, 'src', 'main', 'kotlin', 'expo'));
await removeUponEmptyOrOnlyEmptySubdirs(path.join(androidSrcPath, 'expo'));

const moduleName = jsPackageName.startsWith('Expo') ? jsPackageName.substring(4) : jsPackageName;
await replaceContents(androidPath, singleFileContent =>
Expand Down

0 comments on commit 800ce3a

Please sign in to comment.