Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Create internal frameworks templates for the CLI #570

Open
1 of 2 tasks
MuhammadM1998 opened this issue May 24, 2024 · 1 comment
Open
1 of 2 tasks

[Feature]: Create internal frameworks templates for the CLI #570

MuhammadM1998 opened this issue May 24, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@MuhammadM1998
Copy link
Contributor

MuhammadM1998 commented May 24, 2024

Describe the feature

After #528, the CLI should check in a Nuxt project If shadcn-nuxt injects the file or not and act accordingly. This requires modifying the following code.

https://github.com/radix-vue/shadcn-vue/blob/bd0a8206efa0c0f1fee735f17ab94c63b408e106/packages/cli/src/commands/init.ts#L301-L306

Similarly after #531 need to check if we should install all deps or only a set of them here
https://github.com/radix-vue/shadcn-vue/blob/bd0a8206efa0c0f1fee735f17ab94c63b408e106/packages/cli/src/commands/init.ts#L313-L316

This checking can get messy too quick, and that's for Nuxt-specifics alone. Adding features specific to astro for example would require adding checks for that too.

I propose refactoring the runInit command to the following

cli/src/commands/init.ts

import { runNuxtInitTemplate, runNuxtInitTemplate, runNuxtInitTemplate} from './initTempaltes'

export async function runInit(cwd: string, config: Config) {
  const spinner = ora('Initializing project...')?.start()

  const context = await getProjectInfo() // `context` has `framework` option among other useful keys
  const { framework } = context

  // Shared Inline Code

  // Running the appropriate init template based on framework
  let success, error;
  if (context.framework === 'nuxt')
    { success, error} = runNuxtInitTemplate(context)
  elseif (context.framework === 'vite')
    { success, error} = runVueInitTemplate(context) 
  elseif (context.framework === 'astro')
    { success, error} = runAstroInitTemplate(context) // Just for example. For now I think `Nuxt` and `Vue` templates would be sufficent

  if (error) return consola.error('Error while initializing.', error)
  spinner?.succeed()
}

cli/src/commands/initTempaltes.ts

export function runNuxtInitTemplate(context: ProjectContext) {
  // Specific Nuxt Things
  sharedFunction()
  // Other Specific Nuxt Things
  return { success, error }
}

export function runViteInitTemplate(context: ProjectContext) {
  // Specific Vite Setup
  sharedFunction()
  // Other Specific Vite Things
  return { success, error }
}

export function runAstroInitTemplate(context: ProjectContext) {
  // Specific Astro Setup
  sharedFunction()
  // Other Specific Astro Things
  return { success, error }
}

function sharedFunction(){
  // Stuff
}

This way is more organized and easier to understand & maintain. Also if anyone want to contribute to specific framework he won't need to understand how the other frameworks work and avoids changing unrelated code which can cause mistakes.

I opened the issue for discussion. I'm up to raise a PR once a final structure is agreed 🙏

Additional information

  • I intend to submit a PR for this feature.
  • I have already implemented and/or tested this feature.
@sadeghbarati
Copy link
Collaborator

CC @zernonia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants