Skip to content

Commit 4db589c

Browse files
committed
refactor: unify module implementation across templates
1 parent a90fca8 commit 4db589c

File tree

20 files changed

+117
-131
lines changed

20 files changed

+117
-131
lines changed

src/create.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const COMMON_FILES = path.resolve(__dirname, '../templates/common');
1616
const NATIVE_FILES = path.resolve(__dirname, '../templates/native-library');
1717
const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
1818
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
19+
const OBJC_FILES = path.resolve(__dirname, '../templates/objc-library');
1920

2021
export default async function create(argv: yargs.Arguments<any>) {
2122
const folder = path.join(process.cwd(), argv.name);
@@ -55,8 +56,8 @@ export default async function create(argv: yargs.Arguments<any>) {
5556
authorEmail,
5657
authorUrl,
5758
githubUrl: repo,
58-
useNative,
59-
useCpp,
59+
native,
60+
cpp,
6061
} = (await inquirer.prompt([
6162
{
6263
type: 'input',
@@ -126,16 +127,16 @@ export default async function create(argv: yargs.Arguments<any>) {
126127
},
127128
{
128129
type: 'confirm',
129-
name: 'useNative',
130+
name: 'native',
130131
message: 'Do you want to use Java or Objective-C code?',
131132
default: true,
132133
},
133134
{
134135
type: 'confirm',
135-
name: 'useCpp',
136+
name: 'cpp',
136137
message: 'Do you want to use C++ code?',
137138
default: false,
138-
when: (response) => response.useNative,
139+
when: (response) => response.native,
139140
},
140141
])) as {
141142
slug: string;
@@ -144,8 +145,8 @@ export default async function create(argv: yargs.Arguments<any>) {
144145
authorEmail: string;
145146
authorUrl: string;
146147
githubUrl: string;
147-
useNative: boolean;
148-
useCpp: boolean;
148+
native: boolean;
149+
cpp: boolean;
149150
};
150151

151152
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
@@ -164,7 +165,8 @@ export default async function create(argv: yargs.Arguments<any>) {
164165
.slice(1)}`,
165166
package: slug.replace(/[^a-z0-9]/g, '').toLowerCase(),
166167
podspec: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
167-
useCpp,
168+
native,
169+
cpp,
168170
},
169171
author: {
170172
name: authorName,
@@ -199,14 +201,16 @@ export default async function create(argv: yargs.Arguments<any>) {
199201

200202
await copyDir(COMMON_FILES, folder);
201203

202-
if (useNative) {
204+
if (native) {
203205
await copyDir(NATIVE_FILES, folder);
204206
} else {
205207
await copyDir(EXPO_FILES, folder);
206208
}
207209

208-
if (useCpp) {
210+
if (cpp) {
209211
await copyDir(CPP_FILES, folder);
212+
} else {
213+
await copyDir(OBJC_FILES, folder);
210214
}
211215

212216
try {
@@ -219,23 +223,29 @@ export default async function create(argv: yargs.Arguments<any>) {
219223
// Ignore error
220224
}
221225

226+
const platforms = {
227+
ios: { name: 'iOS', color: 'cyan' },
228+
android: { name: 'Android', color: 'green' },
229+
...(native ? null : { web: { name: 'Web', color: 'blue' } }),
230+
};
231+
222232
console.log(
223233
dedent(chalk`
224-
Project created successfully at {blue ${argv.name}}!
234+
Project created successfully at {yellow ${argv.name}}!
225235
226236
{magenta {bold Get started} with the project}{gray :}
227237
228238
{gray $} yarn bootstrap
239+
${Object.entries(platforms)
240+
.map(
241+
([script, { name, color }]) => chalk`
242+
{${color} Run the example app on {bold ${name}}}{gray :}
229243
230-
{cyan Run the example app on {bold iOS}}{gray :}
231-
232-
{gray $} yarn example ios
233-
234-
{green Run the example app on {bold Android}}{gray :}
235-
236-
{gray $} yarn example android
244+
{gray $} yarn example ${script}`
245+
)
246+
.join('\n')}
237247
238-
{blue Good luck!}
248+
{yellow Good luck!}
239249
`)
240250
);
241251
}

templates/common/CONTRIBUTING.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ yarn example android
2727
To run the example app on iOS:
2828

2929
```sh
30-
yarn example android
30+
yarn example ios
3131
```
32+
<% if (!project.native) { %>
33+
To run the example app on Web:
3234

35+
```sh
36+
yarn example web
37+
```
38+
<% } %>
3339
Make sure your code passes TypeScript and ESLint. Run the following to verify:
3440

3541
```sh
@@ -48,7 +54,11 @@ Remember to add tests for your change if possible. Run the unit tests by:
4854
```sh
4955
yarn test
5056
```
57+
<% if (project.native) { %>
58+
To edit the Objective-C files, open `example/ios/<%= project.name %>Example.xcworkspace` in XCode and find the source files at `Pods > Development Pods > <%= project.slug %>`.
5159

60+
To edit the Java files, open `example/android` in Android studio and find the source files at `<%= project.package %>` under `Android`.
61+
<% } %>
5262
### Commit message convention
5363

5464
We follow the [conventional commits specification](https://www.conventionalcommits.org/en) for our commit messages:

templates/common/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import <%= project.name %> from "<%= project.slug %>";
1515

1616
// ...
1717

18-
const deviceName = await <%= project.name %>.getDeviceName();
18+
const result = await <%= project.name %>.multiply(3, 7);
1919
```
2020

2121
## Contributing

templates/expo-library/example/src/App.tsx renamed to templates/common/example/src/App.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import { StyleSheet, View, Text } from 'react-native';
33
import <%= project.name %> from '<%= project.slug %>';
44

55
export default function App() {
6+
const [result, setResult] = React.useState<number | undefined>();
7+
8+
React.useEffect(() => {
9+
<%= project.name %>.multiply(3, 7).then(setResult);
10+
}, []);
11+
612
return (
713
<View style={styles.container}>
8-
<Text>{<%= project.name %>}</Text>
14+
<Text>Result: {result}</Text>
915
</View>
1016
);
1117
}

templates/cpp-library/cpp/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "example.h"
22

33
namespace example {
4-
int multiply(int a, int b) {
4+
int multiply(float a, float b) {
55
return a * b;
66
}
77
}

templates/cpp-library/cpp/example.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define EXAMPLE_H
33

44
namespace example {
5-
int multiply(int a, int b);
5+
int multiply(float a, float b);
66
}
77

88
#endif /* EXAMPLE_H */

templates/native-library/ios/<%= project.name %>.h renamed to templates/cpp-library/ios/<%= project.name %>.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#import <React/RCTBridgeModule.h>
22

3-
<%if (project.useCpp==true) {%>
43
#ifdef __cplusplus
54

65
#import "example.h"
76

87
#endif
9-
<%}%>
108

119
@interface <%= project.name %> : NSObject <RCTBridgeModule>
1210

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#import "<%= project.name %>.h"
2+
3+
@implementation <%= project.name %>
4+
5+
RCT_EXPORT_MODULE()
6+
7+
// Example method for C++
8+
RCT_EXPORT_METHOD(multiply:(nonnull NSNumber*)a withB:(nonnull NSNumber*)b
9+
resolver:(RCTPromiseResolveBlock)resolve
10+
reject:(RCTPromiseRejectBlock)reject)
11+
{
12+
NSNumber *result = @(example::multiply([a floatValue], [b floatValue]));
13+
14+
resolve(result);
15+
}
16+
17+
@end

templates/expo-library/src/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export default 'Hello World';
1+
export default {
2+
multiply(a: number, b: number) {
3+
return Promise.resolve(a * b);
4+
},
5+
};

templates/native-library/<%= project.podspec %>.podspec

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ Pod::Spec.new do |s|
1313
s.platforms = { :ios => "9.0" }
1414
s.source = { :git => "<%= repo %>.git", :tag => "#{s.version}" }
1515

16-
s.source_files = "ios/**/*.{h,m,mm}"
17-
18-
<% if(project.useCpp==true){ %>
16+
<% if(project.cpp){ %>
1917
s.source_files = "ios/**/*.{h,m,mm}", "cpp/**/*.{h,cpp}"
20-
<% } else{ %>
18+
<% } else{ %>
2119
s.source_files = "ios/**/*.{h,m,mm}"
2220
<% } %>
21+
2322
s.dependency "React"
2423
end

0 commit comments

Comments
 (0)