Skip to content

Commit f87c7d2

Browse files
committed
Initial version
1 parent bdc20ee commit f87c7d2

File tree

9 files changed

+866
-0
lines changed

9 files changed

+866
-0
lines changed

.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extends: airbnb-base

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
3+
example

cli.js

Whitespace-only changes.

lib.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const mkdirp = require('mkdirp');
4+
5+
const templates = require('./templates');
6+
7+
const DEFAULT_PLATFORMS = ['android', 'ios', 'windows'];
8+
9+
const hasPrefix = name =>
10+
name[0].toUpperCase() === name[0] && name[1].toUpperCase() === name[1];
11+
12+
module.exports = ({
13+
name = 'Library',
14+
prefix = 'RN',
15+
packageIdentifier = 'com.reactlibrary',
16+
platforms = DEFAULT_PLATFORMS,
17+
}) => {
18+
if (hasPrefix(name)) {
19+
throw new Error('Please don\'t include the prefix in the name');
20+
}
21+
22+
if (prefix === 'RTC') {
23+
throw new Error(`The "RTC" name prefix is reserved for core React modules.
24+
Please use a different prefix.`);
25+
}
26+
27+
if (prefix === 'RN') {
28+
console.warn('While `RN` is the default prefix, it is recommended to customize the prefix.');
29+
}
30+
31+
templates.filter(template => {
32+
if (template.platform) {
33+
return (platforms.indexOf(template.platform) >= 0);
34+
}
35+
36+
return true;
37+
}).forEach(template => {
38+
if (!template.name) {
39+
return;
40+
}
41+
42+
const args = {
43+
name: `${prefix}${name[0].toUpperCase() + name.slice(1)}`,
44+
moduleName: `react-native-${name.toLowerCase()}`,
45+
packageIdentifier,
46+
};
47+
48+
const filename = template.name(args);
49+
const baseDir = filename.split(path.basename(filename))[0];
50+
51+
if (baseDir) {
52+
mkdirp.sync(baseDir);
53+
}
54+
55+
fs.writeFileSync(
56+
filename,
57+
template.content(args)
58+
);
59+
});
60+
};

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "react-native-create-library",
3+
"version": "1.0.0",
4+
"description": "Tool to create a React Native library with a single command",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/frostney/react-native-create-library.git"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/frostney/react-native-create-library/issues"
18+
},
19+
"homepage": "https://github.com/frostney/react-native-create-library#readme",
20+
"dependencies": {
21+
"minimist": "^1.2.0",
22+
"mkdirp": "^0.5.1"
23+
},
24+
"devDependencies": {
25+
"eslint": "^3.1.0",
26+
"eslint-config-airbnb-base": "^4.0.2",
27+
"eslint-plugin-import": "^1.10.3"
28+
}
29+
}

templates/android.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module.exports = platform => [{
2+
name: () => `${platform}/build.gradle`,
3+
content: () => `
4+
apply plugin: 'com.android.library'
5+
6+
android {
7+
compileSdkVersion 23
8+
buildToolsVersion "23.0.1"
9+
10+
defaultConfig {
11+
minSdkVersion 16
12+
targetSdkVersion 22
13+
versionCode 1
14+
versionName "1.0"
15+
ndk {
16+
abiFilters "armeabi-v7a", "x86"
17+
}
18+
}
19+
lintOptions {
20+
warning 'InvalidPackage'
21+
}
22+
}
23+
24+
dependencies {
25+
compile 'com.facebook.react:react-native:0.20.+'
26+
}
27+
`,
28+
}, {
29+
name: () => `${platform}/src/main/AndroidManifest.xml`,
30+
content: ({ packageIdentifier }) => `
31+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
32+
package="${packageIdentifier}">
33+
34+
</manifest>
35+
`,
36+
}, {
37+
name: ({ packageIdentifier, name }) =>
38+
`${platform}/src/main/java/${packageIdentifier.split('.').join('/')}/${name}Module.java`,
39+
content: ({ packageIdentifier, name }) => `
40+
package ${packageIdentifier};
41+
42+
import com.facebook.react.bridge.ReactApplicationContext;
43+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
44+
import com.facebook.react.bridge.ReactMethod;
45+
import com.facebook.react.bridge.Callback;
46+
47+
public class ${name}Module extends ReactContextBaseJavaModule {
48+
49+
private final ReactApplicationContext reactContext;
50+
51+
public RNShareModule(ReactApplicationContext reactContext) {
52+
super(reactContext);
53+
this.reactContext = reactContext;
54+
}
55+
56+
@Override
57+
public String getName() {
58+
return "${name}";
59+
}
60+
}`,
61+
}, {
62+
name: ({ packageIdentifier, name }) =>
63+
`${platform}/src/main/java/${packageIdentifier.split('.').join('/')}/${name}Package.java`,
64+
content: ({ packageIdentifier, name }) => `
65+
package ${packageIdentifier};
66+
67+
import java.util.Arrays;
68+
import java.util.Collections;
69+
import java.util.List;
70+
71+
import com.facebook.react.ReactPackage;
72+
import com.facebook.react.bridge.NativeModule;
73+
import com.facebook.react.bridge.ReactApplicationContext;
74+
import com.facebook.react.uimanager.ViewManager;
75+
import com.facebook.react.bridge.JavaScriptModule;
76+
public class ${name}Package implements ReactPackage {
77+
@Override
78+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
79+
return Arrays.<NativeModule>asList(new ${name}Module(reactContext));
80+
}
81+
82+
@Override
83+
public List<Class<? extends JavaScriptModule>> createJSModules() {
84+
return Collections.emptyList();
85+
}
86+
87+
@Override
88+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
89+
return Collections.emptyList();
90+
}
91+
}`,
92+
}];

templates/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const android = require('./android')('android');
2+
const ios = require('./ios')('ios');
3+
const windows = require('./windows')('windows');
4+
5+
const general = [{
6+
name: () => 'README.md',
7+
content: () => `
8+
Hello there my old friend
9+
`,
10+
}];
11+
12+
const updatePlatformInFile = platform => file => {
13+
const f = file;
14+
15+
f.platform = platform;
16+
17+
return f;
18+
};
19+
20+
module.exports = [].concat(
21+
general,
22+
android.map(updatePlatformInFile('android')),
23+
ios.map(updatePlatformInFile('ios')),
24+
windows.map(updatePlatformInFile('windows'))
25+
);

0 commit comments

Comments
 (0)