diff --git a/lefthook.yml b/lefthook.yml index 62cd254f..53b06f80 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -6,10 +6,10 @@ pre-commit: - "*.json" - "*.ts" - "*.tsx" - run: bun lint {staged_files} + run: bun lint {staged_files} && git add {staged_files} format: glob: - "*.json" - "*.ts" - "*.tsx" - run: bun format {staged_files} \ No newline at end of file + run: bun format {staged_files} && git add {staged_files} \ No newline at end of file diff --git a/src/create.ts b/src/create.ts index b4e56750..44508e21 100644 --- a/src/create.ts +++ b/src/create.ts @@ -6,7 +6,12 @@ import path from 'path' import projectPackageJsonFile from '../package.json' import { generateInstructions, SUPPORTED_PLATFORMS } from './constants' import { NitroModuleFactory } from './generate-nitro-module' -import { CreateModuleOptions, Nitro, PLATFORM_LANGUAGE_MAP } from './types' +import { + CreateModuleOptions, + ExampleType, + Nitro, + PLATFORM_LANGUAGE_MAP, +} from './types' import { detectPackageManager, dirExist } from './utils' export const createModule = async ( @@ -42,6 +47,7 @@ export const createModule = async ( finalModuleName: 'react-native-' + name.toLowerCase(), skipInstall: options.skipInstall, skipExample: options.skipExample, + exampleType: answers.exampleType, }) await moduleFactory.createNitroModule() @@ -92,6 +98,14 @@ const getUserAnswers = async (name: string, usedPm?: string) => { }, }) + const exampleType = await inquirer.prompt({ + type: 'list', + message: kleur.cyan('What type of module would you like to create?'), + name: 'name', + choices: ['expo', 'cli', 'both'], // Thinking if i should have this? + default: 'cli', + }) + const platforms = await inquirer.prompt({ type: 'checkbox', message: kleur.cyan('🎯 Select target platforms:'), @@ -194,5 +208,7 @@ const getUserAnswers = async (name: string, usedPm?: string) => { pm: pm.name, moduleType: moduleType.name === 'Nitro View' ? Nitro.View : Nitro.Module, + exampleType: + exampleType.name === 'expo' ? ExampleType.Expo : ExampleType.CLI, } } diff --git a/src/generate-nitro-module.ts b/src/generate-nitro-module.ts index 4cd9ec46..e16f67c5 100644 --- a/src/generate-nitro-module.ts +++ b/src/generate-nitro-module.ts @@ -25,6 +25,7 @@ import { CppFileGenerator } from './file-generators/cpp-file-generator' import { IOSFileGenerator } from './file-generators/ios-file-generator' import { JSFileGenerator } from './file-generators/js-file-generator' import { + ExampleType, FileGenerator, GenerateModuleConfig, Nitro, @@ -224,15 +225,24 @@ export class NitroModuleFactory { private async createExampleApp() { const packageManager = this.config.pm === 'bun' ? 'bunx' : 'npx -y' - const args = `${packageManager} \ + // TODO: generate expo example app + const expoCmd = 'npx create-expo-app@latest' + + const cliCmd = `${packageManager} \ @react-native-community/cli@latest init ${toPascalCase(this.config.moduleName)}Example \ --package-name com.${replaceHyphen(this.config.moduleName)}example \ - --directory example --skip-install --skip-git-init --version latest` - - await execAsync(args, { cwd: this.config.cwd }) + --directory example --skip-install --skip-git-init --version 0.78.0` + + let appPath = '' + if (this.config.exampleType === ExampleType.Expo) { + await execAsync(expoCmd, { cwd: this.config.cwd }) + appPath = path.join(this.config.cwd, 'example', 'App.tsx') + } else { + await execAsync(cliCmd, { cwd: this.config.cwd }) + // Setup App.tsx + appPath = path.join(this.config.cwd, 'example', 'App.tsx') + } - // Setup App.tsx - const appPath = path.join(this.config.cwd, 'example', 'App.tsx') await writeFile( appPath, appExampleCode( diff --git a/src/types.ts b/src/types.ts index 9f31f5ea..2fa27561 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,6 +30,11 @@ export enum Nitro { View = 'view', } +export enum ExampleType { + Expo = 'expo', + CLI = 'cli', +} + export type GenerateModuleConfig = { pm: PackageManager cwd: string @@ -41,6 +46,7 @@ export type GenerateModuleConfig = { moduleType: Nitro moduleName: string finalModuleName: string + exampleType: ExampleType } & Omit export interface FileGenerator { @@ -62,4 +68,4 @@ export type InstructionsParams = { pm: string skipInstall?: boolean skipExample?: boolean -} \ No newline at end of file +}