Skip to content

Commit

Permalink
refactor: rename files
Browse files Browse the repository at this point in the history
  • Loading branch information
atlj committed Jun 12, 2024
1 parent dc7e2f8 commit b805ce9
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 41 deletions.
88 changes: 47 additions & 41 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,70 @@ const NATIVE_COMMON_EXAMPLE_FILES = path.resolve(
);

const NATIVE_FILES = {
view_module_legacy: path.resolve(
'module-legacy': path.resolve(
__dirname,
'../templates/native-library-legacy'
),
'module-new': path.resolve(__dirname, '../templates/native-library-new'),
'module-mixed': path.resolve(__dirname, '../templates/native-library-mixed'),
'view-legacy': path.resolve(__dirname, '../templates/native-view-legacy'),
'view-module-legacy': path.resolve(
__dirname,
'../templates/native-view-library-legacy'
),
view_module_mixed: path.resolve(
'view-module-mixed': path.resolve(
__dirname,
'../templates/native-view-library-mixed'
),
view_module_new: path.resolve(
'view-module-new': path.resolve(
__dirname,
'../templates/native-view-library-new'
),
} as const;

const OBJC_FILES = {
module_common: path.resolve(__dirname, '../templates/objc-library'),
view_module_legacy: path.resolve(
'module-legacy': path.resolve(__dirname, '../templates/objc-library'),
'module-mixed': path.resolve(__dirname, '../templates/objc-library'),
'module-new': path.resolve(__dirname, '../templates/objc-library'),
'view-module-legacy': path.resolve(
__dirname,
'../templates/objc-view-library-legacy'
),
view_module_mixed: path.resolve(
'view-module-mixed': path.resolve(
__dirname,
'../templates/objc-view-library-mixed'
),
view_module_new: path.resolve(
'view-module-new': path.resolve(
__dirname,
'../templates/objc-view-library-new'
),
} as const;

const KOTLIN_FILES = {
view_module_legacy: path.resolve(
'view-module-legacy': path.resolve(
__dirname,
'../templates/kotlin-view-library-legacy'
),
view_module_mixed: path.resolve(
'view-module-mixed': path.resolve(
__dirname,
'../templates/kotlin-view-library-mixed'
),
view_module_new: path.resolve(
'view-module-new': path.resolve(
__dirname,
'../templates/kotlin-view-library-new'
),
'view-legacy': path.resolve(__dirname, '../templates/kotlin-view-legacy'),
'module-legacy': path.resolve(
__dirname,
'../templates/kotlin-library-legacy'
),
'module-new': path.resolve(__dirname, '../templates/kotlin-library-new'),
'module-mixed': path.resolve(__dirname, '../templates/kotlin-library-mixed'),
} as const;

const SWIFT_FILES = {
module_legacy: path.resolve(__dirname, '../templates/swift-library-legacy'),
view_legacy: path.resolve(__dirname, '../templates/swift-view-legacy'),
'module-legacy': path.resolve(__dirname, '../templates/swift-library-legacy'),
'view-legacy': path.resolve(__dirname, '../templates/swift-view-legacy'),
} as const;

type ArgName =
Expand Down Expand Up @@ -549,9 +565,9 @@ async function create(argv: yargs.Arguments<any>) {
version = FALLBACK_BOB_VERSION;
}

const moduleType = type.startsWith('view-module')
? 'view_module'
: type.startsWith('view')
const moduleType = type.startsWith('view-module-')
? 'view-module'
: type.startsWith('view-')
? 'view'
: 'module';
const arch =
Expand Down Expand Up @@ -603,8 +619,8 @@ async function create(argv: yargs.Arguments<any>) {
arch,
cpp: languages === 'cpp',
swift: languages === 'kotlin-swift',
view: moduleType === 'view' || moduleType === 'view_module',
module: moduleType === 'module' || moduleType === 'view_module',
view: moduleType === 'view' || moduleType === 'view-module',
module: moduleType === 'module' || moduleType === 'view-module',
},
author: {
name: authorName,
Expand Down Expand Up @@ -688,7 +704,7 @@ async function create(argv: yargs.Arguments<any>) {
}
}

if (languages === 'js') {
if (type === 'library') {
await copyDir(JS_FILES, folder);
await copyDir(EXPO_FILES, folder);
} else {
Expand All @@ -705,32 +721,22 @@ async function create(argv: yargs.Arguments<any>) {
await copyDir(NATIVE_COMMON_EXAMPLE_FILES, folder);
}

if (arch === 'mixed') {
// View module doesn't have legacy or a new arch only version.
await copyDir(NATIVE_FILES['view_module_mixed'], folder);
await copyDir(OBJC_FILES['view_module_mixed'], folder);
await copyDir(KOTLIN_FILES['view_module_mixed'], folder);
} else {
await copyDir(NATIVE_FILES[`${moduleType}_${arch}`], folder);

if (options.project.swift) {
await copyDir(SWIFT_FILES[`${moduleType}_legacy`], folder);
} else {
if (moduleType === 'module') {
await copyDir(OBJC_FILES[`${moduleType}_common`], folder);
} else {
await copyDir(OBJC_FILES[`view_${arch}`], folder);
}
}
await copyDir(NATIVE_FILES[type], folder);

const templateType = `${moduleType}_${arch}` as const;
if (
type === 'view-legacy' ||
(type === 'module-legacy' && options.project.swift)
) {
await copyDir(SWIFT_FILES[type], folder);
} else {
await copyDir(OBJC_FILES[type], folder);
}

await copyDir(KOTLIN_FILES[templateType], folder);
await copyDir(KOTLIN_FILES[type], folder);

if (options.project.cpp) {
await copyDir(CPP_FILES, folder);
await fs.remove(path.join(folder, 'ios', `${options.project.name}.m`));
}
if (options.project.cpp) {
await copyDir(CPP_FILES, folder);
await fs.remove(path.join(folder, 'ios', `${options.project.name}.m`));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.<%- project.package %>

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise

class <%- project.name -%>Module(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

override fun getName(): String {
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun multiply(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
}

companion object {
const val NAME = "<%- project.name -%>"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.<%- project.package %>

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager


class <%- project.name -%>Package : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(<%- project.name -%>Module(reactContext))
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.<%- project.package %>

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise

class <%- project.name -%>Module internal constructor(context: ReactApplicationContext) :
<%- project.name -%>Spec(context) {

override fun getName(): String {
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
override fun multiply(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
}

companion object {
const val NAME = "<%- project.name -%>"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.<%- project.package %>

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.NativeModule
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.module.model.ReactModuleInfo
import java.util.HashMap

class <%- project.name -%>Package : TurboReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == <%- project.name -%>Module.NAME) {
<%- project.name -%>Module(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
moduleInfos[<%- project.name -%>Module.NAME] = ReactModuleInfo(
<%- project.name -%>Module.NAME,
<%- project.name -%>Module.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
)
moduleInfos
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.<%- project.package %>

import com.facebook.react.bridge.ReactApplicationContext

abstract class <%- project.name -%>Spec internal constructor(context: ReactApplicationContext) :
Native<%- project.name -%>Spec(context) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.<%- project.package %>

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.Promise

abstract class <%- project.name -%>Spec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {

abstract fun multiply(a: Double, b: Double, promise: Promise)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.<%- project.package %>

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule

@ReactModule(name = <%- project.name -%>Module.NAME)
class <%- project.name -%>Module(reactContext: ReactApplicationContext) :
Native<%- project.name -%>Spec(reactContext) {

override fun getName(): String {
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
override fun multiply(a: Double, b: Double): Double {
return a * b
}

companion object {
const val NAME = "<%- project.name -%>"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.<%- project.package %>

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import java.util.HashMap

class <%- project.name -%>Package : TurboReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == <%- project.name -%>Module.NAME) {
<%- project.name -%>Module(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
moduleInfos[<%- project.name -%>Module.NAME] = ReactModuleInfo(
<%- project.name -%>Module.NAME,
<%- project.name -%>Module.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true // isTurboModule
)
moduleInfos
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.<%- project.package %>

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager


class <%- project.name -%>Package : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return emptyList()
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return listOf(<%- project.name -%>ViewManager())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.<%- project.package %>

import android.graphics.Color
import android.view.View
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp

class <%- project.name -%>ViewManager : SimpleViewManager<View>() {
override fun getName() = "<%- project.name -%>View"

override fun createViewInstance(reactContext: ThemedReactContext): View {
return View(reactContext)
}

@ReactProp(name = "color")
fun setColor(view: View, color: String) {
view.setBackgroundColor(Color.parseColor(color))
}
}

0 comments on commit b805ce9

Please sign in to comment.